mockito/mockito · error · ArgumentsAreDifferent
Argument(s) are different! Wanted: ${wanted} ${location} Ac
Error message
Argument(s) are different! Wanted:
${wanted}
${location}
Actual invocations have different arguments${ at position(s) ${indexes}}:
${actualCalls with locations}
What it means
MissingInvocationChecker throws ArgumentsAreDifferent (an AssertionError with this message) when a similar invocation exists (same method) but with different arguments. It computes suspiciously non-matching argument indexes, builds a SmartPrinter diff of wanted vs actual arguments, and prints the actual calls with their locations, so the developer sees exactly which arguments diverged.
Source
Thrown at mockito-core/src/main/java/org/mockito/internal/verification/checkers/MissingInvocationChecker.java:54
return;
}
Invocation similar = findSimilarInvocation(invocations, wanted);
if (similar == null) {
throw wantedButNotInvoked(wanted, invocations);
}
Integer[] indexesOfSuspiciousArgs =
getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(), similar.getArguments());
Set<String> classesWithSameSimpleName =
getNotMatchingArgsWithSameName(wanted.getMatchers(), similar.getArguments());
SmartPrinter smartPrinter =
new SmartPrinter(
wanted, invocations, indexesOfSuspiciousArgs, classesWithSameSimpleName);
List<Location> actualLocations =
invocations.stream().map(Invocation::getLocation).collect(Collectors.toList());
throw argumentsAreDifferent(
similar,
wanted,
smartPrinter.getWanted(),
smartPrinter.getActuals(),
actualLocations);
}
public static void checkMissingInvocation(
List<Invocation> invocations, MatchableInvocation wanted, InOrderContext context) {
List<Invocation> chunk = findAllMatchingUnverifiedChunks(invocations, wanted, context);
if (!chunk.isEmpty()) {
return;
}
Invocation previousInOrder = findPreviousVerifiedInOrder(invocations, context);
if (previousInOrder != null) {
throw wantedButNotInvokedInOrder(wanted, previousInOrder);View on GitHub (pinned to 5a676bcd9e)
Solutions
- Use ArgumentCaptor to capture the actual argument and assert fields explicitly
- Use any()/any(Class) matchers or argThat(...) with a field-wise predicate instead of equals
- Implement equals/hashCode on the compared type, or compare with same(...)/refEq semantics
- Print/log the actual arguments shown in the message and correct the expected values
Example fix
// before
verify(repo).save(new User("id-1")); // fails: actual id-2, no equals()
// after
ArgumentCaptor<User> cap = ArgumentCaptor.forClass(User.class);
verify(repo).save(cap.capture());
assertEquals("id-2", cap.getValue().getId()); Defensive patterns
Strategy: try-catch
Validate before calling
boolean sameArgs = Mockito.mockingDetails(mock).getInvocations().stream().anyMatch(i -> i.getMethod().getName().equals("save") && Objects.equals(i.getArguments()[0], expected)); if (!sameArgs) throw new AssertionError("args differ from expected"); Type guard
<T> boolean invokedWith(Object mock, String method, java.util.function.Predicate<T> argMatch) { return Mockito.mockingDetails(mock).getInvocations().stream().anyMatch(i -> i.getMethod().getName().equals(method) && argMatch.test((T) i.getArguments()[0])); } Try / catch
try { verify(mock).save(expected); } catch (ArgumentsAreDifferent e) { fail("argument mismatch: " + e.getMessage()); } Prevention
- Implement equals/hashCode on argument types used in verify
- Use ArgumentCaptor for objects with identity semantics
- Avoid volatile values (timestamps, random IDs) in expected args
- Compare captured arguments field-by-field instead of whole-object equals
When it happens
Trigger: verify(mock).m(expectedArg) while the mock was actually called with a different value for one or more parameters, e.g. different string content, different object instance without matching equals, or null vs non-null.
Common situations: ]Comparing objects without equals/hashCode overridden (reference inequality); IDs or timestamps generated at runtime differ from test constants; mutable objects changed after the call; wrong hardcoded expected value after behavior change; equals vs same-instance matcher confusion.
Related errors
- Invalid use of argument matchers! ${expectedCount} matchers
- No argument value was captured! You might have forgotten to
- No interactions wanted here: ${location} But found these int
- No interactions wanted here: ${location} But found this inte
- No interactions wanted here: ${location} But found this inte
AI-assisted analysis of mockito/mockito@5a676bcd9e (2026-09-05).
Data as JSON: /api/errors/7f3be8729aae8ce2.
Report an issue: GitHub.