Home » Android » xml – android – accessing test application assets

xml – android – accessing test application assets

Posted by: admin June 15, 2020 Leave a comment

Questions:

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

How to&Answers:

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.

Answer:

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.