I am using InstrumentationTestCase
to unit test a component of my application.
The component persists data to the internal storage and uses Context::fileList();
to retrieve the persisted files.
I experience the following problem: Using this method in the app (on the device) works perfectly fine. But when I try to (Android-)Unit-Test (also on the Device) with use of InstrumentationTestCase
I get a NullPointerException
inside the fileList()
method. I digged into the android source and found out that getFilesDir()
(see source here) returns null and causes this error.
The code to reproduce is the following:
public class MyTestCase extends InstrumentationTestCase
{
public void testExample() throws Exception
{
assertNotNull(getInstrumentation().getContext().getFilesDir()); // Fails
}
}
My questions are: Is this behaviour intended? What can I do to circumvent this issue? Am I using InstrumentationTestCase
right or should I use something different?
I found this question but I'm not sure if this covers the same problem I have.