4

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)

default locale
  • 13,035
  • 13
  • 56
  • 62
Karthick
  • 123
  • 2
  • 4
  • 11

2 Answers2

4

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.

default locale
  • 13,035
  • 13
  • 56
  • 62
  • I tried doReturn(response).when(abc).XYZ(exampleHeader); but throwing error not a mock object – Karthick Feb 02 '17 at 12:32
  • Can you include `abc` initialization in your question? Also, please, add some explanation: what exactly are you trying to mock/test? – default locale Feb 02 '17 at 12:34
  • I put @InjectMock annotation for the abc, but no use – Karthick Feb 02 '17 at 12:46
  • @InjectMocks private ABC abc; is the abc initialization – Karthick Feb 02 '17 at 12:47
  • 1
    @Karthick probably, you should use `@Mock` annotation. Check out this question: https://stackoverflow.com/questions/16467685/difference-between-mock-and-injectmocks – default locale Feb 02 '17 at 12:50
  • @Mock private ABC abc; is resolved my issue thanks for your help default locale – Karthick Feb 02 '17 at 12:52
  • Once again thanks for your help , it resolved my issue, thank you "default locale" :-) – Karthick Feb 02 '17 at 12:54
  • @Karthick You're welcome. Don't forget [to accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – default locale Feb 02 '17 at 12:55
  • I have a method below public Response getAbcExample(Double a, Double b, Integer c, String d, RequestHeader requestHeader) throws { Map logMap = new HashMap<>(); Response response = new Response(); String jsonString = _getAB_Exampe(a, b, c, requestHeader); } my mockito lines are below Mockito.verify(abc, Mockito.times(1)).getAbcExample(Mockito.any(Double.class),Mockito.any(Double.class),Mockito.any(Integer.class),Mockito.eq(""),Mockito.any(RequestHeader.class)); but am getting InvalidUseOfMatchersException please help me – Karthick Feb 04 '17 at 13:00
1

result object is not a mock object. when needs a mock object as an arg --> when(mock)

Akash Yellappa
  • 2,126
  • 28
  • 21