mockito/mockito · error · MockitoException

Cannot reset <mock> which is not currently registered as a s

Error message

Cannot reset <mock> which is not currently registered as a static mock

What it means

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.

Source

Thrown at mockito-core/src/main/java/org/mockito/internal/creation/bytebuddy/InlineDelegateByteBuddyMockMaker.java:546

                }
            }
        }
        if (interceptor == null) {
            return null;
        } else {
            return interceptor.getMockHandler();
        }
    }

    @Override
    public void resetMock(
            Object mock, MockHandler<?> newHandler, MockCreationSettings<?> settings) {
        MockMethodInterceptor mockMethodInterceptor =
                new MockMethodInterceptor(newHandler, settings);
        if (mock instanceof Class<?>) {
            Map<Class<?>, MockMethodInterceptor> interceptors = mockedStatics.get();
            if (interceptors == null || !interceptors.containsKey(mock)) {
                throw new MockitoException(
                        "Cannot reset "
                                + mock
                                + " which is not currently registered as a static mock");
            }
            interceptors.put((Class<?>) mock, mockMethodInterceptor);
        } else {
            // Check for singleton mocks first
            Map<Object, MockMethodInterceptor> singletonInterceptors = mockedSingletons.get();
            if (singletonInterceptors != null && singletonInterceptors.containsKey(mock)) {
                singletonInterceptors.put(mock, mockMethodInterceptor);
                return;
            }

            if (!mocks.containsKey(mock)) {
                throw new MockitoException(
                        "Cannot reset " + mock + " which is not currently registered as a mock");
            }
            mocks.put(mock, mockMethodInterceptor);

View on GitHub (pinned to 5a676bcd9e)

Solutions

  1. Keep the reset inside the active mockStatic scope: perform resetMock/re-stubbing before the MockedStatic is closed
  2. If you need a fresh static mock, call mockStatic(Class) again instead of resetting a deregistered one
  3. Ensure the MockedStatic handle is not closed early (check try-with-resources blocks and finally clauses)
  4. Ensure the static mock and reset run on the same thread, since the interceptor map is thread-local

Example fix

// before
MockedStatic<Foo> ms = Mockito.mockStatic(Foo.class);
ms.close();
Mockito.reset(Foo.class); // wrong: no longer registered
// after
try (MockedStatic<Foo> ms = Mockito.mockStatic(Foo.class)) {
    ms.when(Foo::bar).thenReturn(1);
    // use / reset within the open scope
}
Defensive patterns

Strategy: validation

Validate before calling

// ensure the static mock is still open before resetting
if (activeMockedStatic == null || activeMockedStatic.isClosed()) {
    activeMockedStatic = Mockito.mockStatic(Foo.class); // re-register instead of resetting
}

Type guard

static boolean isStaticMockRegistered(Class<?> type) {
    // a static mock exists only while its MockedStatic scope is open
    return activeScopes.containsKey(type); // track open MockedStatic handles yourself
}

Try / catch

try {
    Mockito.reset(Foo.class);
} catch (org.mockito.exceptions.base.MockitoException e) {
    if (e.getMessage().contains("not currently registered as a static mock")) {
        mockedStatic = Mockito.mockStatic(Foo.class); // re-open scope
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mockito/mockito@5a676bcd9e (2026-09-05). Data as JSON: /api/errors/7a42adc340013886. Report an issue: GitHub.