{"record":{"id":"f71adfa00bd48604","repo":"mockito/mockito","slug":"argument-passed-to-when-is-not-a-mock-example-o","errorCode":null,"errorMessage":"Argument passed to when() is not a mock!\nExample of correct stubbing:\n    doThrow(new RuntimeException()).when(mock).someMethod();","messagePattern":"Argument passed to when\\(\\) is not a mock!\nExample of correct stubbing:\n    doThrow\\(new RuntimeException\\(\\)\\)\\.when\\(mock\\)\\.someMethod\\(\\);","errorType":"exception","errorClass":"NotAMockException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/stubbing/StubberImpl.java","lineNumber":45,"sourceCode":"\n    private final Strictness strictness;\n\n    public StubberImpl(Strictness strictness) {\n        this.strictness = strictness;\n    }\n\n    private final List<Answer<?>> answers = new LinkedList<>();\n\n    @Override\n    public <T> T when(T mock) {\n        if (mock == null) {\n            mockingProgress().reset();\n            throw nullPassedToWhenMethod();\n        }\n\n        if (!isMock(mock)) {\n            mockingProgress().reset();\n            throw notAMockPassedToWhenMethod();\n        }\n\n        MockUtil.getInvocationContainer(mock).setAnswersForStubbing(answers, strictness);\n\n        return mock;\n    }\n\n    @Override\n    public Stubber doReturn(Object toBeReturned) {\n        return doReturnValues(toBeReturned);\n    }\n\n    @Override\n    public Stubber doReturn(Object toBeReturned, Object... nextToBeReturned) {\n        return doReturnValues(toBeReturned).doReturnValues(nextToBeReturned);\n    }\n\n    private StubberImpl doReturnValues(Object... toBeReturned) {","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/stubbing/StubberImpl.java#L27-L63","documentation":"StubberImpl.when(mock) received a non-null object that is not a Mockito mock (or spy). isMock(mock) fails, state is reset, and notAMockPassedToWhenMethod() is thrown. The doXxx().when(obj) form only works on mocks/spies created by Mockito.","triggerScenarios":"doThrow(x).when(realObject).method(); passing a stub/fake implementation, a builder-produced object, or an interface implementation hand-written in the test; passing a Mockito-mocked class but a different wrapped instance (e.g. after deserialization or a proxy wrapper).","commonSituations":"Mixing hand-rolled fakes with Mockito stubbing; accidentally stubbing the system-under-test instead of its collaborator; Spy on an object later re-wrapped; using a mock created by another mocking framework (EasyMock) with Mockito APIs.","solutions":["Pass a mock created via Mockito.mock(...), @Mock, or Mockito.spy(...) — not a plain instance.","If you need partial stubbing on a real object, wrap it with Mockito.spy(realObject) first.","Verify the object identity: it must be the same instance that is injected into the code under test."],"exampleFix":"// before\nUserRepository real = new InMemoryUserRepository();\ndoThrow(new IOException()).when(real).save(any());\n// after\nUserRepository repo = spy(new InMemoryUserRepository());\ndoThrow(new IOException()).when(repo).save(any());","handlingStrategy":"type-guard","validationCode":"if (!Mockito.mockingDetails(candidate).isMock()) throw new IllegalArgumentException(\"expected a Mockito mock or spy\");","typeGuard":"static boolean isMockitoMock(Object o) { return o != null && Mockito.mockingDetails(o).isMock(); }","tryCatchPattern":null,"preventionTips":["Only pass objects from Mockito.mock/@Mock/Mockito.spy into doXxx(...).when(...).","Use spy(realObject) for partial stubbing of real instances."],"tags":["mockito","stubbing","not-a-mock","testing"],"backgroundTag":"mockito-not-a-mock","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"}