Joseph
Joseph

Reputation: 81

android - accessing test application assets

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

Upvotes: 8

Views: 6038

Answers (2)

Mike Ellery
Mike Ellery

Reputation: 2094

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.

Upvotes: 26

Damian Kołakowski
Damian Kołakowski

Reputation: 2741

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.

Upvotes: 2

Related Questions