In Spring testing, @MockBean replaced a bean in the test ApplicationContext with a Mockito mock, so the class under test talks to a fake instead of a real database, queue or remote service. It is deprecated since Spring Boot 3.4 and removed in Spring Boot 4. Its replacement is @MockitoBean from Spring Framework 6.2 and later.
Use it when a test needs the Spring wiring (controllers, filters, validation, serialisation) but not every dependency behind it.
Spring tests range from plain unit tests to full application tests. A context mock belongs in the middle:
| Test type | Annotation | Spring context | Mocks via |
|---|---|---|---|
| Unit test | @ExtendWith(MockitoExtension.class) | None | @Mock, @InjectMocks |
| Web slice | @WebMvcTest | Web layer only | @MockitoBean |
| Data slice | @DataJpaTest | JPA layer only | Rarely needed |
| Full test | @SpringBootTest | Whole application | @MockitoBean for external systems |
If the code under test has no need for Spring features, a plain @Mock is faster and simpler.
A @WebMvcTest loads controllers but not @Service or @Repository beans, so every collaborator the controller needs must be supplied. That is the typical use case.
The test checks routing, JSON output and exception mapping in the controller layer without a database. Older code uses @MockBean in exactly the same place, imported from org.springframework.boot.test.mock.mockito.
In a @SpringBootTest the real beans exist, and the mock overrides the one of the same type (or name). This is useful for external systems such as a payment gateway:
If several beans match the type, qualify by name: @MockitoBean(name = "primaryGateway").
@MockitoSpyBean (formerly @SpyBean) wraps the real bean, so real methods run unless stubbed. Use doReturn(...).when(spy) for stubbing so the real method is not called while setting up.
Mocks are reset after each test method by default. Stubbing in one test therefore does not affect the next, but it does mean you cannot rely on state left over from a previous test.
Mock declarations are part of the context cache key. Two test classes with different sets of mocked beans create two application contexts, and context start-up is usually the slowest part of a Spring suite.
@WebMvcTest and @DataJpaTest over @SpringBootTest when they are enough.Not in new code. It was deprecated in Spring Boot 3.4 and removed in Spring Boot 4. Switch to @MockitoBean.
It adds a new bean of that type to the context. If a matching bean exists, it is replaced.
No. It changes beans in the context, which are then injected normally. For fields outside Spring, use @Mock with @InjectMocks.
Use normal Mockito verification, such as verify(productService).find(7L).
A context mock swaps a real bean for a Mockito mock inside the Spring test context. It suits web slice tests and integration tests that must avoid external systems. Use @MockitoBean and @MockitoSpyBean today, share mock sets between test classes for context caching, and use plain Mockito when Spring is not needed.