vendredi 4 septembre 2015

Mocking Android AssetManager

I have a piece of code that accepts Context and passes this context to a private method. The private method calls the getAssets().open() to read a file present in the assets folder of my app.

public void methodA(Context ctx) throws IOException{
     // do some stuff here...
     Object data[] = getFileContents(ctx);
     // use the data[] returned here...

}

private Object[] getFileContents(Context ctx) throws IOException{
     Object[] data;
     BufferedInputStream is = new BufferedInputStream(context.getAssets().open("test.txt"));
     // parse file and create array of Objects[]
     return data[];
}

I am writing Unit tests to test the methodA() using Mockito so that i can test passing junk data or throw exceptions in my testcases.

The problem is that i cannot mock AssetManager class in android (it being Final).

I tried to use InstrumentationTestCase to inject real and test context, but that only works for few scenarios. How do I control the BufferedInputStream so that I can provide it any input I want (using mocks or otherwise) ?

Aucun commentaire:

Enregistrer un commentaire