@MockBean adds a Mockito mock to the Spring ApplicationContext of a test, replacing any bean of the same type. It is significant because Spring tests run against a real context, and this annotation is how you swap a database, remote client or other slow collaborator for a controllable fake. It was deprecated in Spring Boot 3.4 and removed in Spring Boot 4.0, where @MockitoBean from Spring Framework takes its place.
The idea applies to every Spring test style: slice tests, full @SpringBootTest tests and plain Spring TestContext tests.
A unit test of a plain class can build the object by hand and pass in a Mockito.mock(). In a Spring test the framework builds the objects, wires dependencies by type and applies proxies, so the mock has to live inside the container to be injected. Bean overrides do exactly that. They keep the real wiring for everything else, so you still test configuration, request mapping and transactions while one dependency stays under your control.
For each field annotated with @MockBean, the Spring Boot test support does the following before the test instance runs:
AFTER).No network call is made, and the test verifies how CheckoutService reacts to a declined payment.
Spring Framework 6.2 added the Bean Override feature. The annotations live in org.springframework.test.context.bean.override.mockito.
| Old annotation | New annotation |
|---|---|
@MockBean | @MockitoBean |
@SpyBean | @MockitoSpyBean |
You can also declare overrides at class level with the types attribute, which is useful in base classes. For beans you want to replace with a hand-written fake instead of a mock, use @TestBean, which points to a static factory method in the test class.
@WebMvcTest loads controllers and MVC infrastructure but no @Service or @Repository beans, so controller tests usually need a mock for each collaborator.
This asserts the HTTP contract (status code, JSON, headers) without starting a database.
Each different combination of mocks creates a new cached context, because the override set is part of the cache key. A suite with fifty test classes and fifty different mock sets can start fifty contexts.
@WebMvcTest or @DataJpaTest to @SpringBootTest when only one layer matters.@ExtendWith(MockitoExtension.class) and constructor injection, with no Spring at all.Mocking everything makes tests brittle. For repository code, a Testcontainers database gives more trustworthy results than a mocked repository. For HTTP clients, a local stub server (for example WireMock) checks real serialization. Use mocks for edges you do not own or that are slow, nondeterministic or hard to trigger.
It is deprecated since Spring Boot 3.4 and removed in 4.0. On Boot 3.x it still works with a warning, but new code should use @MockitoBean.
Yes. The default reset mode clears stubbing and invocations after each test method, so every test starts with a clean mock.
Yes. Both @MockBean(name = "...") and @MockitoBean(name = "...") target a specific bean name, which helps when several beans share a type.
Different mock combinations produce different context cache keys. Consolidate overrides in shared base classes to reuse contexts.
@MockBean swapped a real bean for a Mockito mock inside the Spring test context. The same job is now done by @MockitoBean and @MockitoSpyBean. Mock only the collaborators you must, keep override sets consistent across classes so contexts are cached, and use plain Mockito for tests that do not need Spring.