[JAVA] Org.mockito.exceptions.misusing.InvalidUseOfMatchersException in Mockito

In a test with Mockito

when(hoge.fuga("aaa",0)).thenReturn(1)

When you want to write something like. I want to use the same return value for the second argument 0 no matter what is entered, so I want to use ʻanyInt () of ʻorg.mockito.Matchers, so I will fix it like this

when(hoge.fuga("aaa",anyInt())).thenReturn(1)

Then

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

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

Get angry with an error.

when(hoge.fuga(eq("aaa"),anyInt())).thenReturn(1)

This will solve the problem.

If you want to use a matcher such as ʻanyInt () `, you need to write all the arguments in the matcher method.

I have no idea what's going on inside, but I want to know why. .. ..

Recommended Posts

Org.mockito.exceptions.misusing.InvalidUseOfMatchersException in Mockito
Changes in Mockito 2
Mock static methods in Mockito 3.4
Try Mockito