{"record":{"id":"7a42adc340013886","repo":"mockito/mockito","slug":"cannot-reset-mock-which-is-not-currently-registe","errorCode":null,"errorMessage":"Cannot reset <mock> which is not currently registered as a static mock","messagePattern":"Cannot reset <mock> which is not currently registered as a static mock","errorType":"exception","errorClass":"MockitoException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java","lineNumber":546,"sourceCode":"                }\n            }\n        }\n        if (interceptor == null) {\n            return null;\n        } else {\n            return interceptor.getMockHandler();\n        }\n    }\n\n    @Override\n    public void resetMock(\n            Object mock, MockHandler<?> newHandler, MockCreationSettings<?> settings) {\n        MockMethodInterceptor mockMethodInterceptor =\n                new MockMethodInterceptor(newHandler, settings);\n        if (mock instanceof Class<?>) {\n            Map<Class<?>, MockMethodInterceptor> interceptors = mockedStatics.get();\n            if (interceptors == null || !interceptors.containsKey(mock)) {\n                throw new MockitoException(\n                        \"Cannot reset \"\n                                + mock\n                                + \" which is not currently registered as a static mock\");\n            }\n            interceptors.put((Class<?>) mock, mockMethodInterceptor);\n        } else {\n            // Check for singleton mocks first\n            Map<Object, MockMethodInterceptor> singletonInterceptors = mockedSingletons.get();\n            if (singletonInterceptors != null && singletonInterceptors.containsKey(mock)) {\n                singletonInterceptors.put(mock, mockMethodInterceptor);\n                return;\n            }\n\n            if (!mocks.containsKey(mock)) {\n                throw new MockitoException(\n                        \"Cannot reset \" + mock + \" which is not currently registered as a mock\");\n            }\n            mocks.put(mock, mockMethodInterceptor);","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java#L528-L564","documentation":"resetMock was asked to swap the handler of a static mock (a `Class` object), but the class is not present in the current thread's registered static-mock interceptors map. This means there is no active mockStatic for that class to reset — the registration happens only inside an active mockStatic scope. Mockito throws MockitoException to fail fast rather than silently registering a new interceptor.","triggerScenarios":"Calling MockMaker.resetMock with a `Class<?>` argument for a class that was never passed to mockStatic(), or whose mockStatic scope has already been closed (try-with-resources block exited) or was created on a different thread.","commonSituations":"Trying to reset/re-stub a static mock after the `try (MockedStatic<X> m = mockStatic(X.class)) {…}` block ended; forgetting to keep the MockedStatic handle open; framework code resetting mocks while the static mock was already deregistered.","solutions":["Keep the reset inside the active mockStatic scope: perform resetMock/re-stubbing before the MockedStatic is closed","If you need a fresh static mock, call mockStatic(Class) again instead of resetting a deregistered one","Ensure the MockedStatic handle is not closed early (check try-with-resources blocks and finally clauses)","Ensure the static mock and reset run on the same thread, since the interceptor map is thread-local"],"exampleFix":"// before\nMockedStatic<Foo> ms = Mockito.mockStatic(Foo.class);\nms.close();\nMockito.reset(Foo.class); // wrong: no longer registered\n// after\ntry (MockedStatic<Foo> ms = Mockito.mockStatic(Foo.class)) {\n    ms.when(Foo::bar).thenReturn(1);\n    // use / reset within the open scope\n}","handlingStrategy":"validation","validationCode":"// ensure the static mock is still open before resetting\nif (activeMockedStatic == null || activeMockedStatic.isClosed()) {\n    activeMockedStatic = Mockito.mockStatic(Foo.class); // re-register instead of resetting\n}","typeGuard":"static boolean isStaticMockRegistered(Class<?> type) {\n    // a static mock exists only while its MockedStatic scope is open\n    return activeScopes.containsKey(type); // track open MockedStatic handles yourself\n}","tryCatchPattern":"try {\n    Mockito.reset(Foo.class);\n} catch (org.mockito.exceptions.base.MockitoException e) {\n    if (e.getMessage().contains(\"not currently registered as a static mock\")) {\n        mockedStatic = Mockito.mockStatic(Foo.class); // re-open scope\n    } else {\n        throw e;\n    }\n}","preventionTips":["Keep all static-mock stubbing/reset inside the try-with-resources mockStatic scope","Never close MockedStatic before you are done with it","Run static mocks and their resets on the same thread","Track MockedStatic handles in your test framework to avoid resetting closed scopes"],"tags":["mockito","mockstatic","static-mock","lifecycle"],"backgroundTag":"mock-not-registered","analyzedSha":"5a676bcd9ef9a10db46e1dc34e190eb46faa2687","analyzedAt":"2026-09-05T19:32:58.607Z","contentChangedAt":"2026-09-05T19:32:58.607Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}