mockito/mockito · error · MockitoException
Negative value is not allowed here
Error message
Negative value is not allowed here
What it means
Mockito's Times verification mode requires a non-negative invocation count. The Times constructor throws this MockitoException when given a negative number, since 'verify exactly -N times' is meaningless.
Source
Thrown at mockito-core/src/main/java/org/mockito/internal/verification/Times.java:26
import static org.mockito.internal.verification.checkers.NumberOfInvocationsChecker.checkNumberOfInvocations;
import java.util.List;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.internal.verification.api.VerificationDataInOrder;
import org.mockito.internal.verification.api.VerificationInOrderMode;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.MatchableInvocation;
import org.mockito.verification.VerificationMode;
public class Times implements VerificationInOrderMode, VerificationMode {
final int wantedCount;
public Times(int wantedNumberOfInvocations) {
if (wantedNumberOfInvocations < 0) {
throw new MockitoException("Negative value is not allowed here");
}
this.wantedCount = wantedNumberOfInvocations;
}
@Override
public void verify(VerificationData data) {
List<Invocation> invocations = data.getAllInvocations();
MatchableInvocation wanted = data.getTarget();
if (wantedCount > 0) {
checkMissingInvocation(data.getAllInvocations(), data.getTarget());
}
checkNumberOfInvocations(invocations, wanted, wantedCount);
}
@Override
public void verifyInOrder(VerificationDataInOrder data) {
List<Invocation> allInvocations = data.getAllInvocations();View on GitHub (pinned to 5a676bcd9e)
Solutions
- Ensure the count passed to times() is >= 0; clamp or assert before use
- Use never() for zero expected invocations instead of times(0)/negative values
- Check the arithmetic producing the count (e.g. Math.max(0, expected))
Example fix
// before verify(mock, times(items.size() - removed)); // can be negative // after verify(mock, times(Math.max(0, items.size() - removed)));
Defensive patterns
Strategy: validation
Validate before calling
int expected = computeCount();
if (expected < 0) throw new IllegalArgumentException("times() count must be >= 0, got " + expected);
verify(mock, times(expected)); Prevention
- Clamp computed counts with Math.max(0, n)
- Use never() for zero-invocation checks
- Assert intermediate data sizes before deriving expected counts
When it happens
Trigger: Calling Mockito.times(-1) or verify(mock, times(negativeCount)) with a hardcoded negative or a computed count that went negative (e.g. list.size()-k when k > size).
Common situations: Computing expected counts from dynamic data (collections sizes, counters) that can go negative; typos in test constants; off-by-one arithmetic in parameterized tests.
Related errors
- Argument should be a mock, but is: MOCK_GETCLASS
- Thread sleep has been interrupted
- verificationMode.getClass().getSimpleName() + " is not imple
- nullPassedToVerify()
- notAMockPassedToVerify(mock.getClass())
AI-assisted analysis of mockito/mockito@5a676bcd9e (2026-09-05).
Data as JSON: /api/errors/a48a510cde60f3df.
Report an issue: GitHub.