{"record":{"id":"a48a510cde60f3df","repo":"mockito/mockito","slug":"negative-value-is-not-allowed-here","errorCode":null,"errorMessage":"Negative value is not allowed here","messagePattern":"Negative value is not allowed here","errorType":"exception","errorClass":"MockitoException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/verification/Times.java","lineNumber":26,"sourceCode":"import static org.mockito.internal.verification.checkers.NumberOfInvocationsChecker.checkNumberOfInvocations;\n\nimport java.util.List;\n\nimport org.mockito.exceptions.base.MockitoException;\nimport org.mockito.internal.verification.api.VerificationData;\nimport org.mockito.internal.verification.api.VerificationDataInOrder;\nimport org.mockito.internal.verification.api.VerificationInOrderMode;\nimport org.mockito.invocation.Invocation;\nimport org.mockito.invocation.MatchableInvocation;\nimport org.mockito.verification.VerificationMode;\n\npublic class Times implements VerificationInOrderMode, VerificationMode {\n\n    final int wantedCount;\n\n    public Times(int wantedNumberOfInvocations) {\n        if (wantedNumberOfInvocations < 0) {\n            throw new MockitoException(\"Negative value is not allowed here\");\n        }\n        this.wantedCount = wantedNumberOfInvocations;\n    }\n\n    @Override\n    public void verify(VerificationData data) {\n        List<Invocation> invocations = data.getAllInvocations();\n        MatchableInvocation wanted = data.getTarget();\n\n        if (wantedCount > 0) {\n            checkMissingInvocation(data.getAllInvocations(), data.getTarget());\n        }\n        checkNumberOfInvocations(invocations, wanted, wantedCount);\n    }\n\n    @Override\n    public void verifyInOrder(VerificationDataInOrder data) {\n        List<Invocation> allInvocations = data.getAllInvocations();","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/verification/Times.java#L8-L44","documentation":"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.","triggerScenarios":"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).","commonSituations":"Computing expected counts from dynamic data (collections sizes, counters) that can go negative; typos in test constants; off-by-one arithmetic in parameterized tests.","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))"],"exampleFix":"// before\nverify(mock, times(items.size() - removed)); // can be negative\n// after\nverify(mock, times(Math.max(0, items.size() - removed)));","handlingStrategy":"validation","validationCode":"int expected = computeCount();\nif (expected < 0) throw new IllegalArgumentException(\"times() count must be >= 0, got \" + expected);\nverify(mock, times(expected));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed counts with Math.max(0, n)","Use never() for zero-invocation checks","Assert intermediate data sizes before deriving expected counts"],"tags":["mockito","verification","argument-validation"],"backgroundTag":"negative-count-not-allowed","analyzedSha":"5a676bcd9ef9a10db46e1dc34e190eb46faa2687","analyzedAt":"2026-09-05T19:32:58.607Z","contentChangedAt":"2026-09-05T19:32:58.607Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}