android - accessing test application assets

有些话、适合烂在心里 提交于 2019-12-06 22:45:15

问题


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


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/3901342/android-accessing-test-application-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!