我有一个非常简单的方法,我试图单元测试:的JUnit /的Mockito试验失败的离奇原因
public class MyAntTask extends org.apache.tools.ant.Task {
public void execute() {
fire();
}
public void fire() {
// Do stuff
}
}
我只想写一个单元测试,确认调用总是调用fire()
,所以我写了这个:
@Test
public void executeCallsFire() {
//GIVEN
MyAntTask myTask = Mockito.mock(MyAntTask.class);
// Configure the mock to throw an exception if the fire() method
// is called.
Mockito.doThrow(new RuntimeException("fired")).when(myTask).fire();
// WHEN
try {
// Execute the execute() method.
myTask.execute();
// We should never get here; HOWEVER this is the fail() that's
// being executed by JUnit and causing the test to fail.
Assert.fail();
}
catch(Exception exc) {
// THEN
// The fire() method should have been called.
if(!exc.getMessage().equals("fired"))
Assert.fail();
}
}
我想(我决不是一个专家)通常的Mockito不能返回void
,但是这是一个解决办法模拟的方法。你基本上说“用一个Mock
包装我的对象,每当一个特定的方法即将被执行时,它总会返回一个特定的RuntimeException
”。因此,Mockito不是实际执行fire()
,而是看到它即将执行并引发异常。执行已验证?检查。
而不是通过,它在第一个Assert.fail()
失败,正好在myTask.execute()
的呼叫之下。
对于我的生活,我无法弄清楚为什么。下面是巨大的堆栈跟踪的JUnit的第一个10或-所以线是给我的失败:
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:92)
at org.junit.Assert.fail(Assert.java:100)
at net.myproj.ant.tasks.MyAntTaskUnitTest.executeCallsFire(MyAntTaskUnitTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
任何这里的想法,你们StackOverflow的的大师的Mockito?提前致谢!
虽然fgb的答案是完全正确的,但我想知道你为什么要单元测试一行长,没有逻辑的方法。有一点,单元测试不再是你的时间经济用途。我从来没有找到一个很好的理由来编写一个像这个一样简单的方法的单元测试。 –