我的場景很簡單。根據this和the documentation of Mockito本身的最後一個回答試圖使用部分模擬。我的測試是: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)
誰能提供更多的指導?
你做'。當(spy.methodB());'而RPresle寫道'。當(間諜).methodB();' – QBrute
你是對的。非常感謝你。你認爲我應該刪除我的編輯,或者我的「錯字」對未來的讀者有用嗎? –