Search code examples
spring-bootunit-testingmockitojunit-jupiter

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 3 recorded


I know that it has been asked multiple times on this platform. I also checked the solution provided that either use all raw parameters or all Matcher arguments. In my case Argument Matcher(any, anyString) has been used for all parameters but still getting the error.

Code:

Mockito.when(service.createReq(Mockito.any(RequestDto.class))).thenReturn(Mockito.any(TermReq.class));
Mockito.when(utils.sendPOSTRequest(Mockito.anyString(),Mockito.anyString())).thenReturn(Mockito.anyString());

Error on the above line is:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 3 recorded:

Can somebody point out what may be the issue.

Thanks,


Solution

  • You can't return an argument matcher from a mocked method like thenReturn(Mockito.any(TermReq.class)). This's an invalid use.

    You should return an actual value or an argument captor.