Quick revision
The short answer
Mockito throws InvalidUseOfMatchersException because the call mixes a matcher, anyString(), with a raw value, 10. When any argument of a stubbed or verified call uses a matcher, every argument must use one. The fix is when(repo.find(anyString(), eq(10))).thenReturn(user).
What to remember
In this answer, we saw that InvalidUseOfMatchersException comes from mixing matchers and raw values in one call. Matchers such as anyString() record themselves in Mockito's per-thread state and return placeholders, so the mock can only use them if every argument is a matcher. Wrapping 10 in eq() fixes the stub. We also saw that typed matchers like anyString() do not match null since Mockito 2, while any() does, and that a stray matcher can trigger the error on a later line.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Mockito throws InvalidUseOfMatchersException because the call mixes a matcher, anyString(), with a raw value, 10. When any argument of a stubbed or verified call uses a matcher, every argument must use one. The fix is when(repo.find(anyString(), eq(10))).thenReturn(user).
Why this question is asked
This exception is one of the first walls people hit with Mockito, and the message can look strange the first time: the code compiles and looks reasonable. Interviewers ask about it because the reason behind it reveals how Mockito matchers work under the hood, and that knowledge also explains other odd errors, such as matchers used outside a stub. In this answer, we will reproduce the error, see how matchers record their state, apply the fix, and then look at a related trap: which matchers accept null.
1Reproducing the Error
Say we have a repository with a method that takes a name and a minimum age, and we want it to return a user for any name, as long as the age is 10. A natural first try is to put anyString() for the name and the plain number 10 for the age.
Notice the important line in the message: two matchers expected, one recorded. Mockito counted two arguments in the call but found only one matcher. To understand why it counts at all, we need to look at what anyString() really does.
2How Matchers Record Their State
Java evaluates method arguments before it calls the method. So when this line runs, anyString() runs first, then the literal 10 is ready, and only then is repo.find(...) called on the mock. anyString() cannot pass a special 'match anything' object to find(), because find() expects a String. So it does something clever: it pushes a matcher onto a stack that Mockito keeps for the current thread, and returns a harmless placeholder value, an empty string.
Then the mock's find() method runs. It receives the placeholder string and the number 10. It cannot tell which of the two was a matcher and which was a real value, because both are just plain values. So it looks at the matcher stack. If the stack holds exactly as many matchers as the method has arguments, Mockito uses them in order. If it holds none, Mockito treats all arguments as exact values compared with equals(). Anything in between is ambiguous, and Mockito refuses it with this exception.
So the rule is not an arbitrary style choice. Matchers are side effects recorded in Mockito's state, and raw values leave no record, so the two cannot be mixed in one call.
- anyString() runspushes a matcher, returns ""
- 10 is evaluateda plain int, nothing recorded
- repo.find("", 10) runs on the mockthe mock sees two plain values
- Matchers on the stack equal arguments?YesUse the matchers in orderNoZero matchers?
- Zero matchers recorded?YesCompare every argument with equals()NoThrow InvalidUseOfMatchersException
5 steps in order. Play them, or step through them one at a time.
3The Fix: Wrap the Raw Value in eq()
The fix is to turn the raw value into a matcher too. Mockito gives us eq() for this: it matches an argument equal to the given value, and it records a matcher like the others do.
Now two matchers are recorded for two arguments, so Mockito can build the stub. The same rule applies to verify(). verify(repo).find(anyString(), 10) fails in the same way, and verify(repo).find(anyString(), eq(10)) is the correct form.
For primitive parameters, eq() has overloads for int, long and the rest, so there is no boxing surprise. And if we do not care about a value at all, anyInt() works just as well as eq() for keeping the count right.
4A Related Trap: Matchers and null
Once we are using matchers, we should know which ones match null, because a null argument quietly breaks stubs. Since Mockito 2, the typed matchers such as anyString(), anyInt() and any(Foo.class) do not match null. anyString() means 'any non-null String'. The untyped any() does match null.
| Matcher | Matches "x" | Matches null |
|---|---|---|
anyString() | yes | no |
any(String.class) | yes | no |
any() | yes | yes |
isNull() | no | yes |
eq("x") | yes | no |
The difference matters in real tests. If the code under test passes null by mistake, a stub with anyString() will not match, the mock returns its default null, and the test fails somewhere far away with a NullPointerException. That failure is actually useful, because it reveals the bug. If we really do want to accept null, we say so explicitly with any() or isNull().
5Matchers Outside a Stub or Verify
The same recording mechanism explains one more error. If we call a matcher somewhere that is not an argument of a mock call, for example assigning anyString() to a variable or passing it to a real object, the matcher stays on the stack. Mockito notices it at the next mock interaction and reports misplaced or invalid use of matchers, sometimes in a different line from the one that caused it. When the error points at an innocent line, we look at the line just before it for a stray matcher.
6Follow-up questions
Can I mix a captor with a raw value?
No. captor.capture() is also a matcher, so verify(repo).find(captor.capture(), 10) has the same problem. Write verify(repo).find(captor.capture(), eq(10)).
Why does the exception sometimes appear in the next test?
Because a stray matcher stays recorded for the thread until the next interaction with Mockito. MockitoExtension validates usage after each test, which usually reports it in the right test, but code that leaves a matcher half-used can still surface in the next one.
Is eq() needed for objects as well as numbers?
Yes, whenever another argument in the same call uses a matcher. eq(someObject) compares with equals(), just like the plain value would have been compared if no matchers were used at all.
7Conclusion
In this answer, we saw that InvalidUseOfMatchersException comes from mixing matchers and raw values in one call. Matchers such as anyString() record themselves in Mockito's per-thread state and return placeholders, so the mock can only use them if every argument is a matcher. Wrapping 10 in eq() fixes the stub. We also saw that typed matchers like anyString() do not match null since Mockito 2, while any() does, and that a stray matcher can trigger the error on a later line.
Open with
The call mixes a matcher with a raw value. anyString() is a matcher and 10 is a literal, and Mockito requires that if one argument uses a matcher, all of them do.
Then, if they want more
The reason is how matchers work: Java evaluates the arguments before calling the mock, so anyString() cannot pass a special object into a String parameter. Instead it records a matcher in Mockito's thread-local state and returns a placeholder. When the mock is called, it sees only plain values and looks at how many matchers were recorded.
Two arguments and one matcher is ambiguous, so it throws. The fix is to wrap the value in eq: when(repo.find(anyString(), eq(10))).thenReturn(user). The same applies in verify and with captor.capture().
One more thing I would mention: since Mockito 2, anyString() and the other typed matchers do not match null, while any() does, so if the code passes a null name, this stub will not match and the mock returns null. If null is a valid input, I would use any() or isNull() on purpose.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why does this": https://scrivio-interview-library.netlify.app/java/mocking/74dec96ca1/