【发布时间】:2012-11-27 19:49:40
【问题描述】:
我有一个Runnable,类似于:
public void run() {
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
//more stuff here
}
catch (Exception e) {
//simplified for reading
}
finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}
}
}
如何测试inputStream.close() 是否被调用?我目前正在使用 Mockito 和 JUnit。我知道注入inputStream 是一个想法,但我不希望在调用run?() 之前使用这些资源,因此它是一个局部变量。那么如何重新设计我的代码以允许我测试是否调用了 close 呢?
【问题讨论】:
标签: java unit-testing junit mockito