@MockBean tells Spring Boot's test framework to create a Mockito mock and register it in the ApplicationContext, replacing any existing bean of the same type. It lets a slice or full-context test isolate the class under test from collaborators such as repositories, HTTP clients or message publishers. In current projects it is deprecated: Spring Boot 3.4 deprecated @MockBean and @SpyBean in favour of Spring Framework's @MockitoBean and @MockitoSpyBean, and Spring Boot 4.0 removed them.
The behaviour is the same idea in both generations, only the package and the lifecycle integration changed. This article shows how it works, how to migrate and where it goes wrong.
Spring tests load a real ApplicationContext, so a test that wants to control one dependency cannot simply call Mockito.mock() and hand it in. @MockBean solves this by modifying the context before the test runs:
This is why it appears most often with @WebMvcTest, where the controller is real but the service layer is not loaded at all.
The following example uses the legacy annotation, which still compiles on Spring Boot 3.x with a deprecation warning.
@WebMvcTest only loads web-layer beans, so OrderService would be missing and the context would fail to start. The mock fills that gap and lets you stub and verify calls with the normal Mockito API (when, verify, ArgumentCaptor).
Spring Framework 6.2 introduced @MockitoBean and @MockitoSpyBean in org.springframework.test.context.bean.override.mockito. They use the Bean Override mechanism of the Spring TestContext Framework instead of Boot-specific code.
| Legacy (Spring Boot) | Current (Spring Framework) |
|---|---|
@MockBean | @MockitoBean |
@SpyBean | @MockitoSpyBean |
org.springframework.boot.test.mock.mockito | org.springframework.test.context.bean.override.mockito |
| Field or class level | Field or class level; can also be used on test-class declarations and meta-annotations |
Migration is mostly an import and annotation rename. Run the whole suite after renaming, because the new mechanism has its own lifecycle and a few edge cases differ from the old Boot implementation (for example how overrides are applied to beans created in nested configuration).
When you want the real implementation with selective stubbing, use @SpyBean (legacy) or @MockitoSpyBean (current).
Use doReturn(...).when(spy) for spies so the real method is not called during stubbing.
Spring caches application contexts between test classes, keyed by configuration. Every distinct set of bean overrides produces a different key, so a different context. If each test class declares its own mocks, the suite builds many contexts and slows down.
@TestConfiguration so classes reuse one context.@WebMvcTest, @DataJpaTest) over @SpringBootTest where possible.Mockito with @ExtendWith(MockitoExtension.class) when no Spring context is needed at all. This is the fastest option for service unit tests.@BeforeAll method is lost.@Mock instead of a bean override in a Spring test, which leaves the context with the real bean.final classes without checking that your Mockito version supports it (Mockito 5 uses the inline mock maker by default).Yes. It was deprecated in Spring Boot 3.4 and removed in Spring Boot 4.0. Use @MockitoBean from Spring Framework instead.
@Mock creates a plain Mockito mock that is not known to Spring. @MockBean and @MockitoBean create a mock and register it in the application context, so other beans receive it.
Yes. It is part of the Spring Framework TestContext Framework, so it works with @SpringJUnitConfig and other non-Boot Spring tests.
Yes, it works with the Spring extension that @SpringBootTest and the slice annotations register automatically. No extra @ExtendWith is needed.
@MockBean replaced or added a Mockito mock in the Spring context so tests could isolate one component. It is now deprecated and removed in newer Boot versions, so write new tests with @MockitoBean and @MockitoSpyBean, keep the set of overrides small to preserve context caching, and use plain Mockito when no context is required.