2

I have a method below:

public Response getAbcExample(Double a, Double b, Integer c, String d, RequestHeader requestHeader)
            throws Exception  {
        Map<String, String> logMap = new HashMap<>();
        Response response = new Response();
        String jsonString = _getAB_Exampe(a, b, c, requestHeader);
    }

My mockito method is below

@Test
public void getabc_Example_Success() throws IOException{
    Response response=new Response();
    RequestHeader requestHeaders=new RequestHeader();
    response.setMessage("Success");
    response.setStatusCode("200");          
when(abc.getabc_Example_Success(anyDouble(),anyDouble(),anyInt(),anyString(),requestHeaders)).thenReturn(response);
        Mockito.verify(abc,Mockito.times(1)).getabcExable(Mockito.any(Double.class),Mockito.any(Double.class),Mockito.any(Integer.class),Mockito.eq(""),Mockito.any(RequestHeader.class));      
  }

but am getting:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException.

whether my code is correct to test the actual method or i need to change?. I want cover the test cased all the lines.

T-Heron
  • 5,385
  • 7
  • 26
  • 52
Karthick
  • 123
  • 2
  • 4
  • 11
  • `requestHeaders` is no valid matcher for your `when` call. So, do what the exception message suggests to you. – Tom Feb 04 '17 at 13:15
  • i tried below doNothing().when(abc).getAbcExample(Mockito.any(Double.class),Mockito.any(Double.class),Mockito.any(Integer.class),Mockito.eq(""),Mockito.any(RequestHeader.class)); but getting MockitoException and only void method use doNothing – Karthick Feb 04 '17 at 14:02

1 Answers1

2

I did some test based on your code and here are some insights..

1) You do not need to use the doReturn().when().. syntax.. In your case the when().thenReturn() is fine (although you still can stick to the other version, up to you).

2) Not sure how do you instantiate the 'abc'object.. but it has to be a @Mock or a @Spy, otherwise you cannot use the when().thenReturn() feature on it.

3) In the when().. you are using the any() in combination with a real object without a matcher (the requstHeaders object):

when(abc.getabc_Example_Success(anyDouble(),anyDouble()
            ,anyInt(),anyString(),requestHeaders));

You cannot do that and Mockito would throw an error:

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

You have make it either Mockito.any(RequestHeaders.class) or Mockito.eq(requestHeaders)

Working Solution

So based on the above your test class should look more or less like the following:

@Spy
private Abc abc;

    @Before
    public void init(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void tst() throws Exception{
        Response response= mock(Response.class);
        RequestHeader requestHeaders=mock(RequestHeader.class);
        when(a.getAbcExample(anyDouble(),anyDouble(),anyInt()
        ,anyString(),Mockito.eq(requestHeaders))).thenReturn(response);

        abc.getAbcExample(1.0, 1.0, 1, "", requestHeaders); // invoke the mocked method

        verify(a, times(1)).getAbcExample(Mockito.any(Double.class)
                 ,Mockito.any(Double.class),Mockito.any(Integer.class)
                 ,Mockito.eq(""),Mockito.any(RequestHeader.class));


    }
}

Alternatively:

  • You can use @Mock instead of @Spy
  • The last param in the verify.. Mockito.any(RequestHeader.class). This can be replaced for the actual requestHeaders object and still will work.

Try it out.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63