1

NOTE: I already tried some other solutions such as; Mockito: InvalidUseOfMatchersException

Mockito - Invalid use of argument matchers

Invalid use of argument matchers

I have the following unit test;

@Test(expectedExceptions = BadRequestException.class)
public void shouldThrowHttpClientErrorExceptionWhenHeadForHeaders()
{
    final String command = "any/command";
    final RoutingInfo routingInfo = new RoutingInfo(GET, "anyservice", command);
    when(restTemplate.headForHeaders("https://anyservice/any/command"))
            .thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));

    underTest.headers(routingInfo);

}

When I run it in Debug mode, my code passes the test, but when I run the test as normal (run mode) it does not pass. The exception thrown is:

Invalid use of argument matchers! 4 matchers expected, 2 recorded:

And here is my actual class;

public <T> ResponseEntity<T> forward(final RoutingInfo routingInfo, final HttpEntity<?> request, final Class<T> responseType)
{
    final HttpMethod method = routingInfo.getMethod();
    final String url = routingInfo.getURL(HTTPS_URI_SCHEME);
    LOG.info("Routing request: {} {}", method, url);
    try
    {
        return restTemplate.exchange(url, method, request, responseType);
    }
    catch (final HttpClientErrorException exc)
    {
        LOG.error("Failed to forward request: {}", exc.getMessage());
        if (exc.getStatusCode().is4xxClientError())
        {
            throw new BadRequestException(exc.getMessage());
        }

        throw new InternalServerErrorException("Failed to forward request!");
    }
}

I really stuck in these. request, responseType looks null when i debug this code. I am not really sure how to fix that, assuming I am right in my identification of the cause.

nolines
  • 150
  • 1
  • 15
  • 2
    Ain't no matchers in that code.. Please post a [mcve]. Maybe the posted code for the test is not the good one (seing the "actual" code). –  Jun 06 '17 at 15:54
  • I already saw these solutions and non of them are working to me. Also, I implemented test class according to this doc. https://static.javadoc.io/org.mockito/mockito-core/2.8.9/org/mockito/ArgumentMatchers.html#nullable(java.lang.Class) – nolines Jun 06 '17 at 15:59
  • 2
    Look I don't want to be rude, but you have a pretty explicit message that tells you what to change with an example. My guess is that you have somewhere `when(restTemplate.exchange("something", Method.GET, any(Object.class), any(Class.class))` and that is incorrect it should be `when(restTemplate.exchange(eq("something"), eq(Method.GET), any(Object.class), any(Class.class))` as explained by the error message. So re-read the duplicates, try to understand the answers and you should be able to fix your specific issue. –  Jun 06 '17 at 16:19

0 Answers0