# Spring Test "@MockBean state leaks between tests" - Fix in CI

> Fix Spring Boot "@MockBean stubs leak between tests" in CI - stale stubbing or recorded interactions carry into the next test because the context was reused and the mock was not reset.

Source: https://latchkey.dev/learn/java-jvm/spring-mockbean-not-reset-in-ci  
Updated: 2026-06-26

A `@MockBean` is reset by Spring only when the test framework resets it around each method; if the cached application context is shared and reset is bypassed, stubs and verified interactions from an earlier test bleed into a later one and cause order-dependent failures.

## Diagnose it: resolve the effective POM first

Maven merges parent POMs, profiles, and settings before it builds anything. The configuration causing your failure is frequently inherited or activated by a profile that is on locally and off in CI.

```Terminal
# the fully resolved configuration Maven will actually use
mvn help:effective-pom | head -60

# which profiles are active here vs on your machine?
mvn help:active-profiles

# full error, offline-safe, no colour codes to confuse the log
mvn -B -e -X <goal> 2>&1 | tail -60
```

> Always pass `-B` (batch mode) in CI. Without it Maven emits interactive progress output that bloats logs and can hang on a prompt.

## FAQ

### What causes Spring test "@MockBean state leaks between tests"?

There are 3 common causes: context reuse without per-method reset, manual mocks instead of @mockbean, and stubbing in a static/shared field. Spring caches the context across test classes.

### How do I fix Spring test "@MockBean state leaks between tests"?

There are 3 fixes depending on which cause you have: use @mockbean, not a manual @bean mock, re-stub per test, not in @beforeall, and do not disable the mockito listener. Work through them in order, since the first is the most common.

### What does Spring test "@MockBean state leaks between tests" actually mean?

A test passes alone but fails when run after another that stubbed the same @MockBean, or a verify(...) counts interactions from a previous test.

### How do I stop Spring test "@MockBean state leaks between tests" happening again?

Always use @MockBean/@SpyBean rather than placing manual mocks in the context. The prevention section lists 3 changes that keep it from recurring.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
