Mockito Argument Matchers | Java Fresher Interview
Fast recall
Quick revision
Core answer
any() matches any reference value, including null; type-specific matchers such as anyString() and any(Order.class) match non-null values of that type. eq(value) matches one value. If one parameter uses a matcher, every parameter in that invocation must use a matcher.Interview-ready guide
Interview answer
Argument matchers let a Mockito stub or verification accept a range of arguments instead of one hard-coded value. For example, anyLong() can accept any non-null Long, while eq("PAID") requires the exact status. The important rule is that all parameters in the same invocation must use matchers once any one parameter does. So verify(audit).record(anyLong(), "CREATED") is invalid; the second argument should be eq("CREATED"). any() is broader than the type-specific matchers because it also accepts null. When null matters, I use isNull(), notNull(), or nullable(Type.class) explicitly. Matchers are helpful for generated IDs or timestamps, but exact values should still be checked when they are part of the behavior being tested.
Mockito Argument Matchers | Java Fresher Interview
Direct Answer
Argument matchers let Mockito accept a group of argument values instead of one exact value. any() matches any reference value, including null; type-specific matchers such as anyString() and any(Order.class) match non-null values of that type. eq(value) matches one value. If one parameter uses a matcher, every parameter in that invocation must use a matcher.
Code Example
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock
private OrderRepository repository;
@InjectMocks
private OrderService service;
@Test
void shouldCreateOrderForAnyValidUser() {
// Stub: accept any Long userId and any Order object
when(repository.save(any(Order.class)))
.thenReturn(new Order(1L, "Laptop", 1200.0));
var result = service.createOrder(42L, "Laptop", 1200.0);
assertThat(result.id()).isEqualTo(1L);
verify(repository).save(any(Order.class)); // Verify with matcher
}
@Test
void shouldApplyDiscountForSpecificCategory() {
// Mix matchers: both arguments must use matchers
when(repository.findByCategory(eq("Electronics")))
.thenReturn(List.of(new Order(1L, "Phone", 800.0)));
var orders = service.getOrdersByCategory("Electronics");
assertThat(orders).hasSize(1);
}
}
Common Mistakes & Pitfalls
- Mixing matchers and literals: Writing
when(service.process(any(), "ACTIVE"))throws InvalidUseOfMatchersException because the second argument is a bare String literal. Fix:when(service.process(any(), eq("ACTIVE"))). - Overusing any() in verify: Writing
verify(emailService).sendEmail(any(), any(), any())passes even if wrong values are sent—use specific matchers or argThat when arguments matter. - Forgetting anyString() versus any(): Using
any()when you meananyString()loses type safety—if the method signature changes to accept Integer,any()still passes butanyString()fails fast, catching the bug.
Check your understanding
Answer these without looking back at the article.
- What is argThat() and when do you use it?
- How do you match null arguments in Mockito?
- What causes MissingMethodInvocationException with matchers?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.