mockito/mockito · error · MockitoException
For {type.getName()}, static mocking is already registered i
Error message
For {type.getName()}, static mocking is already registered in the current thread
To create a new mock, the existing static mock registration must be deregistered What it means
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.
Source
Thrown at mockito-core/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java:800
Map<Class<?>, MockMethodInterceptor> interceptors,
MockCreationSettings<T> settings,
MockHandler<?> handler) {
this.type = type;
this.interceptors = interceptors;
this.settings = settings;
this.handler = handler;
}
@Override
public Class<T> getType() {
return type;
}
@Override
public void enable() {
if (interceptors.putIfAbsent(type, new MockMethodInterceptor(handler, settings))
!= null) {
throw new MockitoException(
join(
"For "
+ type.getName()
+ ", static mocking is already registered in the current thread",
"",
"To create a new mock, the existing static mock registration must be deregistered"));
}
}
@Override
public void disable() {
if (interceptors.remove(type) == null) {
throw new MockitoException(
join(
"Could not deregister "
+ type.getName()
+ " as a static mock since it is not currently registered",
"",View on GitHub (pinned to 5a676bcd9e)
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
Example fix
// before
MockedStatic<Foo> m1 = Mockito.mockStatic(Foo.class);
MockedStatic<Foo> m2 = Mockito.mockStatic(Foo.class); // throws
// after
try (MockedStatic<Foo> m = Mockito.mockStatic(Foo.class)) {
// use static mock
}
// or close m1 before creating m2 Defensive patterns
Strategy: validation
Validate before calling
// Always scope static mocks with try-with-resources and never nest same-type registrations
try (MockedStatic<Foo> m = Mockito.mockStatic(Foo.class)) {
// test code
} Try / catch
try {
MockedStatic<Foo> m = Mockito.mockStatic(Foo.class);
// ...
m.close();
} catch (org.mockito.exceptions.base.MockitoException e) {
// static mock already registered: find and close the leaked registration
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The singleton instance {instance.getClass().getName()} is al
- Cannot create static mock of primitive type <classToMock>
- Cannot specify additional interfaces for static mock of <cla
- Cannot specify spied instance for static mock of <classToMoc
- Could not deregister {type.getName()} as a static mock since
AI-assisted analysis of mockito/mockito@5a676bcd9e (2026-09-05).
Data as JSON: /api/errors/6c9a7a94bc050118.
Report an issue: GitHub.