mockito/mockito · error · MockitoException
Only void methods can doNothing()! Example of correct use of
Error message
Only void methods can doNothing()!
Example of correct use of doNothing():
doNothing().
doThrow(new RuntimeException())
.when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called What it means
Error "Only void methods can doNothing()! Example of correct use of doNothing(): doNothing(). doThrow(new RuntimeException()) .when(mock).someVoidMethod(); Above means: someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called" thrown in mockito/mockito.
Source
Thrown at mockito-core/src/main/java/org/mockito/internal/stubbing/answers/DoesNothing.java:35
private static final long serialVersionUID = 4840880517740698416L;
private static final DoesNothing SINGLETON = new DoesNothing();
private DoesNothing() {}
public static DoesNothing doesNothing() {
return SINGLETON;
}
@Override
public Object answer(InvocationOnMock invocation) {
return null;
}
@Override
public void validateFor(InvocationOnMock invocation) {
if (!new InvocationInfo(invocation).isVoid()) {
throw onlyVoidMethodsCanBeSetToDoNothing();
}
}
}
View on GitHub (pinned to 5a676bcd9e)
Solutions
- Apply doNothing() only to void methods: doNothing().when(mock).someVoidMethod()
- For non-void methods, use doReturn(value).when(mock).method() instead
- Check for overloaded methods to ensure you are stubbing the intended signature
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at mockito-core/src/main/java/org/mockito/internal/stubbing/answers/DoesNothing.java:35 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of mockito/mockito@5a676bcd9e (2026-09-05).
Data as JSON: /api/errors/eaba0f6972edb123.
Report an issue: GitHub.