我的场景非常简单。根据最后一个答案和Mockito本身的文档,试图使用部分mock。我的测试是:
@Test
public void test() {
ClassUnderTest realObject = new ClassUnderTest();
ClassUnderTest spy = spy(realObject);
when(spy.methodB()).thenThrow(new Exception("Testing"));
spy.methodA();
}
测试的类别为:
import org.apache.commons.lang3.NotImplementedException;
public class ClassUnderTest {
int methodB(){
throw new NotImplementedException("Not implemented");
}
public int methodA(){
methodB();
return 0;
}
}
我希望我的间谍对象会调用引发"测试"异常的方法B,而实际上真正的方法被称为抛出"未实现"异常。它的行为就像我没有部分模拟行为
为什么?我错过了什么?
编辑:正如RPresle所建议的,我尝试使用语法
doThrow(new Exception("Testing")).when(spy.methodB());
然而,我得到了一个未完成的StubbingException:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at SimpleTest.test(SimpleTest.java:15)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at SimpleTest.test(SimpleTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
有人能提供更多指导吗?
参考本教程,您可以看到Mockito确实调用了原始方法。
这个语法只有一个警告。实际的rule.createFileTemplate()方法将被调用一次。这可能会有很多副作用,甚至可能无法抛出异常(通常是NPE)。为了解决这个问题,你可以(应该?)使用这种替代Mockito语法:
doReturn(mockFileTemplate).when(rule).createFileTemplate();
为了避免这种情况,您应该使用Mockito doReturn().when()
的其他语法
doThrow(new Exception("Testing")).when(spy).methodB();
希望得到帮助。
编辑:
请注意,结构与when().thenReturn()
有点不同。
doThrow(new Exception("Testing"))
.when(spy)
.methodB();