【发布时间】:2015-12-01 03:36:56
【问题描述】:
我有一段代码接受 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 以便我可以提供任何我想要的输入(使用模拟或其他方式)?
【问题讨论】:
标签: android unit-testing mockito