我试过
@Test
public void getXYZ_Success() throws Exception {
Response result=abc.XYZ(exampleHeader);
Response response=new Response();
response.setMessage(null);
response.setStatusCode("01");
response.setStatus("Failure");
List<ExampleFilterLkp> exampleFilterList=new ArrayList<exampleFilterLkp>();
exampleFilterLkp exampleFilterLkp=new exampleFilterLkp();
examplFilterLkp.setexampleKey("1");
exampleList.add(exampleFilterLkp);
doReturn(response).when(result);
}
我得到以下错误,如何解决这个问题,请帮助我
org.mockito.exceptions.missing.NotAMockException:传递给when()的参数不是mock!正确除根示例:doThrow(new RuntimeException()).when(mock).someMethod();网址:com.firstdata.mpl.manager.exampleTest.getexampleFilter_NullFailure(FuelManagerTest.java:184)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:497)网址:org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)网址:org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)位于org.junit.runners.model.FrameworkMethod.invokeExplorery(FrameworkMethod.java:44)位于org.junit.internal.runners.statements.InvokeMethod.eevaluate(InvokeMethod.java:17)网址:org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)网址:org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)网址:org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)网址:org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)网址:org.junit.runners.ParentRunner$1schedule(ParentRunner.java:63)网址:org.junit.runners.ParentRunner.runChildren(ParentRunnr.java:236)网址:org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)网址:org.junit.runners.ParentRunner$2.eevaluate(ParentRunner.java:229)网址:org.junit.runners.ParentRunner.run(ParentRunner:309)网址:org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TtestReference.java:86)网址:org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)网址:org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
异常消息表示传递给when()的参数不是mock。
在这种情况下,它是result
doReturn(response).when(result);
异常消息甚至说明了正确调用应该是什么样子:
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
从这个问题中还不清楚这里正在测试什么。如果abc
是一个mock对象,并且您希望在XYZ
调用上返回response
,那么应该可以使用类似的方法:
doReturn(response).when(abc).XYZ(exampleHeader);
否则,你需要解释你到底在努力实现什么。
result
对象不是mock对象。当(mock)时,when
需要一个mock对象作为arg-->