java org.mockito.exceptions.misusing.NotAMockException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42001163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:17:34  来源:igfitidea点击:

org.mockito.exceptions.misusing.NotAMockException

javamockito

提问by Karthick

I have tried below lines

我试过下面几行

@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);
}

I'm getting below error, how to solve this issue, please help me

我收到以下错误,如何解决此问题,请帮助我

org.mockito.exceptions.misusing.NotAMockException: Argument passed to when() is not a mock! Example of correct stubbing: doThrow(new RuntimeException()).when(mock).someMethod(); at com.firstdata.mpl.manager.exampleTest.getexampleFilter_NullFailure(FuelManagerTest.java:184) 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:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 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:670) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

org.mockito.exceptions.misusing.NotAMockException:传递给 when() 的参数不是模拟!正确存根的示例: doThrow(new RuntimeException()).when(mock).someMethod(); 在 com.firstdata.mpl.manager.exampleTest.getexampleFilter_NullFailure(FuelManagerTest.java:184) 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:47) at org .junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod。

采纳答案by default locale

Exception message says that argument passed to when() is not a mock.

异常消息表示传递给 when() 的参数不是模拟。

In this case, it's a result

在这种情况下,它是一个 result

doReturn(response).when(result);

Exception message even says what a correct invocation should look like:

异常消息甚至说明了正确的调用应该是什么样的:

Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();

It isn't clear from the question what is being tested here. If abcis a mock object and you want to return the responseon XYZcall then something like this should work:

从问题中不清楚这里正在测试什么。如果abc是一个模拟对象并且您想返回responseon XYZcall 那么这样的事情应该可以工作:

doReturn(response).when(abc).XYZ(exampleHeader);

Otherwise, you need to explain what exactly are you trying to achieve.

否则,您需要解释您到底要达到什么目的。

回答by Akash Yellappa

resultobject is not a mock object. whenneeds a mock object as an arg --> when(mock)

result对象不是模拟对象。when需要一个模拟对象作为参数 --> when(mock)