【发布时间】:2014-05-12 15:36:43
【问题描述】:
我正在尝试使用 EasyMock 来测试方法是否运行了特定次数,但我不断收到 IllegalStateException 错误,我不明白为什么。我是 EasyMock 和 JUnit 的新手,不太熟悉如何使用它们,所以我不确定自己做错了什么。
我的代码是:
FileOutputStream mockWriter;
Numbers mockByte;
@Test
public void testNumbers() throws IOException{
mockWriter = createMock(FileOutputStream.class);
mockByte = new Numbers(mockWriter);
mockByte.initByte();
expect(mockByte.generate()).times(10000);
replay(mockWriter);
}
这些是 initByte 和从我的 Numbers 类生成的方法:
public void initByte() throws IOException{
File outFile = new File("NumbersOutput.txt");
FileOutputStream f = new FileOutputStream(outFile);
for(int i = 0; i < 10000; i++){
int b = generate();
f.write(b);
}
f.flush();
f.close();
}
public int generate(){
return rand.nextInt(100001);
}
【问题讨论】:
标签: java junit easymock illegalstateexception