8

I have a problem with mockito. I'm getting a freaky error, which I'm not able to resolve.

Error:

[error] Invalid use of argument matchers!
[error] 1 matchers expected, 3 recorded:
[error] -> at service.ServiceTest.test(ServiceTest.java:149)
[error] -> at service.ServiceTest.test(ServiceTest.java:149)
[error] -> at service.ServiceTest.test(ServiceTest.java:149)

and the test method which is next to this test method throws:

[error] Unfinished stubbing detected here:
[error] -> at service.ServiceTest.test(ServiceTest.java:149)

Method code:

List<String> method(final Map<String, String> numbersMap, final Map<String, String> invalidNumbersMap, final String filename) {
    //do something
}

protected List<User> run(Map<String, String> numbersMap, final String filename, final Map<String, String> invalidNumbersMap) {
    //do something
    List<String> processedUsedNumbers = method(numbersMap, invalidNumbersMap, filename);
    //do something
    return new ArrayList<>();
}

Test code (which pointing this exception)

@Test
public void test() {
    doReturn(new ArrayList<String>()).when(service).method(anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), anyString()); //line 149
    // rest of mocks

    Map<String, String> inputMap = getExampleMap(3); //generate test data
    List<User> result = service.run(inputMap, "file.csv", new HashMap<>());

    // other checks

    verify(service, times(1)).method(eq(inputMap), anyMapOf(String.class, String.class), eq("file.csv"));
}

Mocking method 'method' in that way:

    when(service.method(anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), anyString())).thenReturn(new ArrayList<String>());

make not difference and i'm still getting the same exception from mockito.

I have no idea what is wrong. Imports looks fine, used params also, so where I did a mistake? Anyone?

UPDATE / SOLUTION

Ok, I found the reason of this error. After change method access modifier from default to protected the whole test working fine. Now, the method in class looks like this one:

protected List<String> method(final Map<String, String> numbersMap, final Map<String, String> invalidNumbersMap, final String filename) {
    //do something
}

But why Mockito throws this kind of exception. This thing is completely irrelevant to exception message output.

Krzysiek
  • 615
  • 8
  • 19
  • 1
    I was using a verify method similar to verify(classUnderTest).someMethod(any(ClassA.class),any(ClassB.class)); ...and was getting org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 1 matchers expected, 2 recorded: The method was package private and changing it to protected solved it - thanks! – alexy Sep 02 '16 at 10:00
  • The following answer adds some explanation to your self-answer, i.e. the access level must be atleast protected for the subclassing to work. But the question why Mockito doesn't highlight the access level problem instead remains unanswered: https://stackoverflow.com/a/57302399/1297789 – Omnibyte Jun 14 '22 at 12:13
  • 1
    You could have answered your own question (as answer not by editing the question). Currently it is listed as unanswered. – Sascha Doerdelmann Aug 10 '22 at 11:20

0 Answers0