Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the internal storage from a test project?

I have a instrumentation test that uses test data. I have included this test data as an asset file in the test project, and it works well.

I would like to speed up re-running the test with different test data by just pushing the test data file to the internal storage of the test app and then let the test read the test data from the internal storage if it exists.

However, I am unable to access the internal storage from the test app. I am using a subclass of android.test.InstrumentationTestRunner, and all calls to getContext().getFilesDir() return null.

Are test apps not able to access the internal storage?

Any help is greatly appreciated.

like image 482
donV Avatar asked Jul 26 '11 19:07

donV


1 Answers

If you happen to use Roboguice with your tests, make sure you implement the constructor of your application class like this:

public MyApplication(Instrumentation instrumentation) {
    this();
    attachBaseContext(instrumentation.getTargetContext());

    //this does not work:
    //attachBaseContext(instrumentation.getContext());
}
like image 153
Ilya Shinkarenko Avatar answered Sep 23 '22 02:09

Ilya Shinkarenko