0

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,

Manish
  • 1,274
  • 3
  • 22
  • 59

2 Answers2

1

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.

Mohammad Al Alwa
  • 702
  • 4
  • 14
0

If you desire mock the return value of a method, you should decide what object will return after the when clause. You can not return a new argument matcher. I advise to return new mocking object in your test scenario like this one:

TermReq termReq = Mockito.mock(TermReq.class);  // create mock
Mockito.when(termReq.getId()).thenReturn(1L) // mock required get methods
Mockito.when(service.createReq(Mockito.any(RequestDto.class))).thenReturn(termReq); // then return in case of the service method call

With this style of usage you can handle or compare the results with mock objects.

oguzhan00
  • 499
  • 8
  • 16