android - accessing test application assets
Asked Answered
P

2

8

I've an XML file in the assets directory of my test application. I want to access this file from my suite method of the test class.

ie.,

public static TestSuite suite(){ InputStream stream = // Some code which returns the asset }

Any idea how I can do this? I tried with Resources.Resources.getSystem().getAssets() but no luck :-(. help please.

Thanx in adv, Joseph

Primrose answered 10/10, 2010 at 17:31 Comment(0)
E
26

I encountered the same problem with my test app. Unfortunately, it looks like getContext() in a test case class actually returns the context of the application under test. In order to get the context of the test application itself, you have to inherit from InstrumentationTestCase and then call getInstrumentation().getContext() to get the context of the test app.

Evoy answered 28/10, 2011 at 16:27 Comment(2)
Thx mikes, I didn't catch that before your post !Flowerpot
Can you provide a sample? getInstrumentation always returns null for me.Skydive
S
2
final AssetManager assetMgr = context.getResources().getAssets();

final InputStream fileIn = assetMgr.open("my_file.txt", AssetManager.ACCESS_STREAMING);

We can use also this method link text for xml-file.

Slingshot answered 10/10, 2010 at 21:17 Comment(3)
Hi Damian, Thanx for the response.Primrose
Hi Damian, Thanx for the response. </br> How do we get the context object here? because the suite() is a static method, and the class do not inherit a context.Primrose
Try to use android test, where you have TestActivity. Evert Activity extends Context.Ivanovo

© 2022 - 2024 — McMap. All rights reserved.