1

Everything sounds correct but I get org.mockito.exceptions.misusing.InvalidUseOfMatchersException, when I try to mock a protected method.How Can I solve it ?

private Service service;
private System system;

@BeforeMethod
  public void setupMocks() throws Exception {
     service = powerMock.mock(Service.class);
     system = powerMock.mock(System.class);

}

public void sample_Test() {
     PowerMockito.doReturn(system).when(service, "getValidatedDto",  
     Matchers.any(Long.class), Matchers.any(Date.class));
    // some code
}
juherr
  • 5,640
  • 1
  • 21
  • 63
h.tester
  • 11
  • 2
  • Your test looks fline; often a _different test_ than the one you've posted is responsible for the problem, [because Matchers use global state](https://stackoverflow.com/q/22822512/1426891). Please post the text of your Exception message, along with other tests that use Matchers. You might also consider adding an `@AfterMethod` that calls [`Mockito.validateMockitoUsage()`](https://static.javadoc.io/org.mockito/mockito-core/2.8.47/org/mockito/Mockito.html#validateMockitoUsage()), which will cause a test to explicitly fail if it misuses Mockito. – Jeff Bowman Jul 31 '17 at 17:04

1 Answers1

0

I suspect you are seeing this exception:

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

With this additional context:

This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String");

If so, then this is because you are mixing parameters in the form of matchers (Matchers.any(Long.class), Matchers.any(Date.class)) with parameters in the form of raw values (service, "getValidatedDto").

The signature of the method under test is unclear to me but I think it might be something like

System getValidatedDto(Long aLong, Date aDate);

If so, then the correct invocation would be:

PowerMockito.doReturn(system).when(service).getValidatedDto(  
    Matchers.any(Long.class), Matchers.any(Date.class));
glytching
  • 44,936
  • 9
  • 114
  • 120
  • Yes. I got that exception, but your solution doesn't work. only all parameters must be in Matchers form if we used one of the parameters with matchers. Why did you use Matchers for method name and mock object ? – h.tester Jul 31 '17 at 09:25
  • Could you perhaps clarify your last comment by posting your updated test method and the exception message. – glytching Jul 31 '17 at 09:27
  • I have updated the answer. If you post again could you perhaps show me (a) the signature of the method you are trying to test, (b) your updated test method and (c) the exception message. – glytching Jul 31 '17 at 09:32
  • Though this is a good place to start looking, [PowerMockito explicitly has a `when` call that takes an `Object` and a `String`](https://static.javadoc.io/org.powermock/powermock-api-mockito/1.6.4/org/powermock/api/mockito/PowerMockito.html#when-java.lang.Object-java.lang.String-java.lang.Object...-), along with arguments that may be Matchers. Though it could be failing because that `when` is designed for _private_ methods, or if Java is picking the wrong overload, I'm not convinced your inference here is accurate: The use of Matchers could very well be fine. – Jeff Bowman Jul 31 '17 at 17:09