我有一段代码,它接受Context并将此Context传递给一个私有方法。这个私有方法调用getAssets().open()来读取我应用程序中assets文件夹中存在的文件。
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[];
}
我正在使用Mockito编写单元测试,以测试methodA()方法,以便在测试用例中传递垃圾数据或引发异常。
问题在于,我无法在Android中模拟AssetManager类(它是Final的)。
我尝试使用InstrumentationTestCase来注入真实和测试上下文,但这仅适用于一些场景。如何控制BufferedInputStream,以便我可以提供任何所需输入(使用模拟或其他方式)?