Mockito any() vs any(Class) | Java Fresher Interview
Fast recall
Quick revision
Core answer
any() matches any reference value, including null. any(SomeType.class) performs a runtime type check and therefore matches non-null instances assignable to that type, not null. Use isNull(), notNull(), or nullable(SomeType.class) when null behavior matters. Generic method signatures may still make any(SomeType.class) useful for type inference even though any() is generic.Interview-ready guide
Interview answer
any() and any(Type.class) are not identical around null. The generic any() matcher accepts any reference value, including null. any(Order.class) performs a runtime type check, so it accepts non-null values assignable to Order and does not match null.
If a method is allowed to receive either an Order or null, nullable(Order.class) states that directly. If the test specifically expects null, use isNull(). If null must be rejected, use notNull() or the typed matcher as appropriate. Type-specific matchers such as anyString() also exclude null.
For example, verify(repository).save(any(Order.class)) proves a non-null Order was passed. verify(repository).save(isNull()) proves the opposite. Choosing the narrow matcher makes the test document the method's null contract instead of matching more than intended.
Mockito any() vs any(Class) | Java Fresher Interview
Direct Answer
any() matches any reference value, including null. any(SomeType.class) performs a runtime type check and therefore matches non-null instances assignable to that type, not null. Use isNull(), notNull(), or nullable(SomeType.class) when null behavior matters. Generic method signatures may still make any(SomeType.class) useful for type inference even though any() is generic.
Code Example
// Generic any() accepts a reference value, including null.
when(repo.findByName(any())).thenReturn(user);
// any(String.class) accepts non-null String values.
when(repo.findByName(any(String.class))).thenReturn(user);
// nullable(String.class) accepts either a String or null.
when(repo.findByName(nullable(String.class))).thenReturn(user);
verify(repo).save(any(Order.class)); // a non-null Order
Common Mistakes & Pitfalls
- Claiming generic
any()excludes null: it accepts reference values including null; typedany(Type.class)excludes null. - Using a matcher as a return value:
nullable(User.class)describes an argument position, not a value forthenReturn. - Choosing a broader matcher than the contract: use typed, null, equality, or predicate matchers when those details matter.
- Mixing matchers and raw values in one invocation: when one argument uses a matcher, wrap the other expected values with matchers such as
eq.
Check your understanding
Answer these without looking back at the article.
- How do you stub a method to handle both null and non-null arguments?
- What is nullable() in Mockito?
- How does Mockito 3's any() behavior differ from Mockito 2?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.