Mockito. Verify method arguments
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Mockito verification is about more than checking whether a method was called. In many tests, the important part is whether the method was called with the right arguments. Mockito supports several levels of strictness here, from exact value matching to flexible matchers to full argument capture for later assertions.
Exact Argument Verification
The simplest case is verifying exact values with verify.
This is a good fit when the expected values are simple and deterministic.
Use Matchers When Only Part of the Input Matters
Sometimes you care about one argument but not the others. Mockito matchers such as anyString(), eq(), and isNull() help express that.
The important rule is consistency: once you use a matcher for one argument, use matchers for all arguments in that method call.
This is wrong:
This is correct:
Capture Arguments for Deeper Assertions
If the code under test builds a complex object and passes it into a dependency, exact verification can become awkward. ArgumentCaptor lets you inspect the real object after the call.
Use a captor when the object has several fields and you want precise assertions on the constructed value.
Use argThat for Custom Matching
If the argument matters, but you only need to test a condition, argThat can be more direct than a captor.
This is concise and keeps the verification close to the condition you actually care about.
Choose the Smallest Verification That Proves the Behavior
Good tests verify behavior without becoming brittle.
Use exact values when:
- the arguments are simple
- every value matters
Use matchers when:
- only some values matter
- the rest are noise for this test
Use captors when:
- the argument is complex
- you need multiple assertions on it
Use argThat when:
- a boolean predicate explains the requirement clearly
Picking the smallest sufficient tool helps keep tests readable and maintainable.
Common Pitfalls
The biggest Mockito mistake here is mixing raw values and matchers in the same call. Mockito rejects that because it cannot interpret the invocation consistently.
Another problem is over-verifying. If a test checks every field of every call, small refactors can break the test even though behavior is still correct. Verify what matters to the contract, not every incidental detail.
Mutable arguments are another trap. If the code passes an object to a mock and mutates it later, a captor sees the mutated object state. In that case, either verify earlier or make the argument immutable.
Finally, do not use captors everywhere. They are powerful, but exact verification or a matcher is often simpler and communicates intent better.
Summary
- '
verify(mock).method(...)is the basic way to check method arguments in Mockito.' - Use matchers such as
eq,any, andisNullwhen only part of the argument list matters. - Use
ArgumentCaptorwhen you need detailed assertions on a passed object. - Use
argThatfor concise custom conditions. - Avoid mixing raw values with matchers, and verify only the behavior that matters to the test.

