{"record":{"id":"6c9a7a94bc050118","repo":"mockito/mockito","slug":"for-type-getname-static-mocking-is-already-re","errorCode":null,"errorMessage":"For {type.getName()}, static mocking is already registered in the current thread\n\nTo create a new mock, the existing static mock registration must be deregistered","messagePattern":"For (.+?), static mocking is already registered in the current thread\n\nTo create a new mock, the existing static mock registration must be deregistered","errorType":"exception","errorClass":"MockitoException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java","lineNumber":800,"sourceCode":"                Map<Class<?>, MockMethodInterceptor> interceptors,\n                MockCreationSettings<T> settings,\n                MockHandler<?> handler) {\n            this.type = type;\n            this.interceptors = interceptors;\n            this.settings = settings;\n            this.handler = handler;\n        }\n\n        @Override\n        public Class<T> getType() {\n            return type;\n        }\n\n        @Override\n        public void enable() {\n            if (interceptors.putIfAbsent(type, new MockMethodInterceptor(handler, settings))\n                    != null) {\n                throw new MockitoException(\n                        join(\n                                \"For \"\n                                        + type.getName()\n                                        + \", static mocking is already registered in the current thread\",\n                                \"\",\n                                \"To create a new mock, the existing static mock registration must be deregistered\"));\n            }\n        }\n\n        @Override\n        public void disable() {\n            if (interceptors.remove(type) == null) {\n                throw new MockitoException(\n                        join(\n                                \"Could not deregister \"\n                                        + type.getName()\n                                        + \" as a static mock since it is not currently registered\",\n                                \"\",","sourceCodeStart":782,"sourceCodeEnd":818,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java#L782-L818","documentation":"Thrown when mockStatic() is called for a type that already has a static mock registered in the current thread. Mockito keeps one static-mock interceptor per type per thread (ThreadLocal map); putIfAbsent returning non-null means a previous registration was never closed. Registrations must be strictly paired with deregistration.","triggerScenarios":"Calling Mockito.mockStatic(Foo.class) twice in the same thread without closing the first (try-with-resources not used or close() not called); a prior test failed without closing its static mock; nested mockStatic on the same class.","commonSituations":"Forgetting try-with-resources around MockedStatic; a JUnit extension (MockitoExtension/@ExtendWith with mockito-inline static handling) not closing mocks after failed tests; parallel tests reusing a thread pool where a previous task leaked a registration; manually calling mockStatic in @BeforeAll without cleanup in @AfterAll.","solutions":["Use try-with-resources: try (MockedStatic<Foo> m = Mockito.mockStatic(Foo.class)) { ... } so close() always runs","Ensure close() is called in a finally block or @AfterEach if mockStatic is invoked manually","Call mockedStatic.close() for every mockStatic before creating a new one for the same type","Check for nested/duplicate mockStatic calls on the same class in the test","If a prior test leaked the registration, fix that test's cleanup rather than swallowing this error"],"exampleFix":"// before\nMockedStatic<Foo> m1 = Mockito.mockStatic(Foo.class);\nMockedStatic<Foo> m2 = Mockito.mockStatic(Foo.class); // throws\n// after\ntry (MockedStatic<Foo> m = Mockito.mockStatic(Foo.class)) {\n    // use static mock\n}\n// or close m1 before creating m2","handlingStrategy":"validation","validationCode":"// Always scope static mocks with try-with-resources and never nest same-type registrations\ntry (MockedStatic<Foo> m = Mockito.mockStatic(Foo.class)) {\n    // test code\n}","typeGuard":null,"tryCatchPattern":"try {\n    MockedStatic<Foo> m = Mockito.mockStatic(Foo.class);\n    // ...\n    m.close();\n} catch (org.mockito.exceptions.base.MockitoException e) {\n    // static mock already registered: find and close the leaked registration\n}","preventionTips":["Always use try-with-resources for MockedStatic","Never create two static mocks for the same type in one thread simultaneously","Close static mocks in @AfterEach if created in setup","Watch for thread-pool tests leaking registrations across tasks"],"tags":["mockito","static-mocking","threadlocal","duplicate-registration"],"backgroundTag":"static-mock-already-registered","analyzedSha":"5a676bcd9ef9a10db46e1dc34e190eb46faa2687","analyzedAt":"2026-09-05T19:32:58.607Z","contentChangedAt":"2026-09-05T19:32:58.607Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}