@MockBean was a Spring Boot test annotation that added a Mockito mock to the Spring ApplicationContext, replacing any existing bean of the same type or registering a new one. It let you test a slice of an application, for example a controller, without its real database or HTTP dependencies.
It is deprecated. Spring Boot 3.4 deprecated @MockBean and @SpyBean in favour of Spring Framework's @MockitoBean and @MockitoSpyBean, and Spring Boot 4 removed the old annotations. New code should use the replacements.
When the test context started, Spring Boot's test support looked at each @MockBean field. It created a Mockito mock of that type and put it in the context, then injected it wherever the type was needed, including inside your controllers and services.
This is the legacy form, shown so you can recognise it in older code.
Spring Framework 6.2 introduced bean overriding for tests. @MockitoBean and @MockitoSpyBean live in org.springframework.test.context.bean.override.mockito and work in any Spring test, not only Boot ones.
Migration is mostly an import change. Differences to know about:
| Topic | @MockBean / @SpyBean | @MockitoBean / @MockitoSpyBean |
|---|---|---|
| Package | org.springframework.boot.test.mock.mockito | org.springframework.test.context.bean.override.mockito |
| Provided by | Spring Boot | Spring Framework 6.2+ |
| Allowed on | Fields and test classes | Fields (and test classes with types) |
Allowed in @Configuration classes | Yes | No |
If you used @MockBean on a configuration class, move the declarations to the test class or a shared base or interface.
Each distinct set of mocks changes the cache key of the Spring test context. Two test classes with different @MockitoBean fields cannot share a cached context, so Spring starts the application context again. With many such classes the suite gets slow.
To limit this:
@WebMvcTest that load only the web layer.@Mock with MockitoExtension for unit tests that do not need Spring at all.Mocks created by @MockitoBean are reset after each test method by default, so stubbings do not leak between tests. You can verify calls as with any Mockito mock:
To wrap a real bean instead of replacing it, use @MockitoSpyBean. Stub spies with doReturn(...).when(spy) to avoid calling the real method during setup.
Yes. It was deprecated in Spring Boot 3.4 and removed in Spring Boot 4. Use @MockitoBean from Spring Framework 6.2 or later.
@Mock creates a standalone Mockito mock. @MockBean created a mock and registered it in the Spring context, replacing a real bean.
Yes. It is part of the Spring TestContext Framework in Spring Framework 6.2, so it works in plain Spring tests.
A different set of mock or override declarations makes a different context cache key. Share mock declarations across test classes or use slice tests.
@MockBean replaced or added a bean with a Mockito mock in the Spring context. It is deprecated and gone in Spring Boot 4, so migrate to @MockitoBean and @MockitoSpyBean, keep mock sets consistent across test classes to preserve context caching, and use plain @Mock when no context is needed.