我的场景非常简单.根据对此的最后答案和Mockito本身的文档,尝试使用部分嘲笑.我的测试是:
@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());
但是,我得到一个UnfinishedStubbingException:
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的本教程,您可以看到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();