我有一个非常简单的文件观察器类,它每2秒检查一次文件是否发生了变化,如果是,则调用onChange
方法(void)。 有没有简单的方法来检查在单元测试中onChange
方法是否被调用?JUnit:检查一个无效方法是否被调用
代码:
public class PropertyFileWatcher extends TimerTask {
private long timeStamp;
private File file;
public PropertyFileWatcher(File file) {
this.file = file;
this.timeStamp = file.lastModified();
}
public final void run() {
long timeStamp = file.lastModified();
if (this.timeStamp != timeStamp) {
this.timeStamp = timeStamp;
onChange(file);
}
}
protected void onChange(File file) {
System.out.println("Property file has changed");
}
}
测试:
@Test
public void testPropertyFileWatcher() throws Exception {
File file = new File("testfile");
file.createNewFile();
PropertyFileWatcher propertyFileWatcher = new PropertyFileWatcher(file);
Timer timer = new Timer();
timer.schedule(propertyFileWatcher, 2000);
FileWriter fw = new FileWriter(file);
fw.write("blah");
fw.close();
Thread.sleep(8000);
// check if propertyFileWatcher.onChange was called
file.delete();
}
您可以使用任何模拟框架而不是仅仅的Mockito。看看EasyMock或jMock并选择你喜欢的东西。编写单元测试的经验法则是,你只应该模拟你可以控制的对象。换句话说,模拟对象应该使用构造函数参数/设置器或参数传递给被测方法。通过这个逻辑,你不能嘲笑在被测试的类的方法内创建的静态调用,最终的或者私有的,或者“新的”对象。 – Kartik 2010-05-11 23:35:41
任何想法如何在EasyMock中做到这一点?我发现缺乏这方面的文件。当我为PropertyFileWatcher创建一个模拟像这样:PropertyFileWatcher propertyFileWatcher = createMockBuilder(PropertyFileWatcher.class).withConstructor(file).createMock();并将预期的调用记录到onChange并重播:propertyFileWatcher.onChange(file);重放(propertyFileWatcher); onChnage方法立即被调用,并且信息被打印到sysout中,但是我只想验证IF是否调用了该方法 – nkr1pt 2010-05-12 18:26:26
不要使用mockito能够执行AtomicBoolean解决方案。 – 2012-08-10 08:21:27