Android testing module cannot find file in resource directory

In Android Studio (v0.4.4), I have a project with two modules, one for application code, the other for test code. The application module is an Android library project, and the test module was created as an Android application.

My unit test, which expands InstrumentationTestCase, tries to read test data from an XML file stored in a src/main/assets/sample.xmltest module. However, I keep getting errors FileNotFoundExceptionwhen opening the file (second line below).

AssetManager assetMgr = this.getInstrumentation().getContext().getAssets();
InputStream stream = assetMgr.open("sample.xml");

When I try to list the contents of a directory assets, String[]returned from assetMgr.list(), empty.

Other unit tests in the same test module that do not read the same test data are executed normally and successfully.

Why can't this unit test read a file from its own resource directory?

Thank!

+3
source share

All Articles