Java – org.mockito.exceptions.misusing.NotAMockException

javamockito

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)

Best Answer

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

In this case, it's a 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 abc is a mock object and you want to return the response on XYZ call then something like this should work:

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

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

Related Topic