Quick revision
The short answer
@MockitoBean comes from Spring Framework 6.2, in the package org.springframework.test.context.bean.override.mockito, and replaces a bean in the test's application context with a Mockito mock; @MockitoSpyBean does the same for spies and replaces @SpyBean. Spring Boot deprecated @MockBean and @SpyBean in 3.4 and removed them in 4.0. When migrating, watch out that @MockitoBean is not supported inside @Configuration classes, and that every distinct set of overridden beans gives a different context cache key.
What to remember
In this answer, we saw that @MockitoBean and @MockitoSpyBean come from Spring Framework 6.2's bean override support and replace a context bean with a Mockito mock or spy. Spring Boot deprecated @MockBean and @SpyBean in 3.4 and removed them in 4.0. When migrating, we move any declarations out of @Configuration classes, where @MockitoBean is ignored, into test classes, base classes or interfaces, and remember that every different set of overrides creates a separate cached context.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@MockitoBean comes from Spring Framework 6.2, in the package org.springframework.test.context.bean.override.mockito, and replaces a bean in the test's application context with a Mockito mock; @MockitoSpyBean does the same for spies and replaces @SpyBean. Spring Boot deprecated @MockBean and @SpyBean in 3.4 and removed them in 4.0. When migrating, watch out that @MockitoBean is not supported inside @Configuration classes, and that every distinct set of overridden beans gives a different context cache key.
Why this question is asked
Almost every Spring Boot test suite uses @MockBean, so its deprecation touches hundreds of files in a typical service, and the upgrade to Spring Boot 4 makes the migration mandatory. Most of it is a search and replace, but a few uses do not translate directly and fail in confusing ways. Interviewers ask this to see whether we keep up with the platform and understand what bean overriding does to the test context. In this answer, we will recap what @MockBean did, see where @MockitoBean comes from and why it moved, migrate typical uses, and cover the cases that need more than a rename.
1A Quick Recap of Mocking Beans in a Test Context
In a Spring test, the application context holds the real beans. Sometimes a test needs one of them replaced, for example a PaymentClient that would call an external API. A Mockito mock, an object that records calls and returns whatever we stub, takes its place, and every bean that depends on PaymentClient receives the mock instead.
For years Spring Boot provided this with @MockBean and @SpyBean. A spy wraps the real bean, so calls go to the real code unless we stub them, and we can verify what was called.
2Where @MockitoBean Comes From
Spring Framework 6.2 added a general bean override mechanism to the TestContext framework itself, so overriding beans no longer depends on Spring Boot. @MockitoBean and @MockitoSpyBean are built on it, in org.springframework.test.context.bean.override.mockito. The same mechanism powers @TestBean, which replaces a bean with the result of a static factory method.
Spring Boot 3.4 then deprecated its own @MockBean and @SpyBean in favour of the framework annotations, and Spring Boot 4.0 removed them. So on Boot 4, code with @MockBean no longer compiles.
For field use in a test class, that is the whole migration. Stubbing and verifying work exactly as before, and the mock is reset after each test method by default, just like @MockBean.
3Watch Out 1: No @MockitoBean in @Configuration Classes
@MockBean could be placed on fields of a @Configuration or @TestConfiguration class, and some teams used that to share mocks. @MockitoBean is only processed on test classes, their superclasses and enclosing classes, and on interfaces they implement. In a configuration class it is silently ignored, so the real bean is used, and tests suddenly call the real API.
Newer Spring Framework versions also allow @MockitoBean at type level on a test class, with a types attribute naming the beans to mock, and as a meta-annotation, which is another way to share a mock set; check your version for type-level support.
4Watch Out 2: Context Caching
Bean overrides are part of the context cache key. Two test classes share a cached context only if they override exactly the same beans in the same way. This was also true of @MockBean, but a migration is a good moment to notice it: if one class mocks PaymentClient and another mocks PaymentClient and EmailSender, they start two contexts.
| Test class | Overridden beans | Context |
|---|---|---|
OrderApiTest | @MockitoBean PaymentClient | Context A |
RefundApiTest | @MockitoBean PaymentClient | Context A (cache hit) |
InvoiceApiTest | PaymentClient and EmailSender | Context B (new startup) |
AuditApiTest | @MockitoSpyBean AuditLog | Context C (new startup) |
5Other Differences and Migration Steps
A few smaller differences can surface. By default @MockitoBean replaces a bean of the field's type, and creates one if none exists; when several beans share the type, we name the one we mean with the name attribute or a qualifier. @MockitoSpyBean requires an existing bean to wrap. Some rarely used attributes of @MockBean have no direct equivalent, so check your version if a test relied on one.
The migration itself is mechanical for most files. Rewrite the imports and annotation names with an OpenRewrite recipe or a search and replace, run the suite, then search for any @MockitoBean left inside configuration classes and move it to a base class or interface. Finally, look at the cache statistics to see whether the migration is a good moment to standardise mock sets.
6Follow-up questions
Does @MockitoBean work with @WebMvcTest and other slices?
Yes. It works with any test that uses the Spring TestContext framework, including all of Boot's slices, because the override mechanism lives in Spring Framework rather than in Boot.
When would you use @TestBean instead?
When the replacement is a real object rather than a mock, such as an in-memory implementation or a fixed Clock. @TestBean calls a static factory method we write and registers its result in place of the bean.
Are mocks reset between tests?
Yes, by default after each test method, which matches the old behaviour. The reset attribute can change this to before each test or none, but changing it is rarely a good idea because state then leaks between tests.
7Conclusion
In this answer, we saw that @MockitoBean and @MockitoSpyBean come from Spring Framework 6.2's bean override support and replace a context bean with a Mockito mock or spy. Spring Boot deprecated @MockBean and @SpyBean in 3.4 and removed them in 4.0. When migrating, we move any declarations out of @Configuration classes, where @MockitoBean is ignored, into test classes, base classes or interfaces, and remember that every different set of overrides creates a separate cached context.
Open with
@MockitoBean is the replacement for Spring Boot's @MockBean. It comes from Spring Framework 6.2, in the package org.springframework.test.context.bean.override.mockito, where the TestContext framework gained a general bean override mechanism.
Then, if they want more
It replaces a bean of the field's type in the test's application context with a Mockito mock, so every bean that depends on it gets the mock, and it is reset after each test by default. @MockitoSpyBean does the same with a spy and replaces @SpyBean.
Spring Boot 3.4 deprecated @MockBean and @SpyBean, and Boot 4.0 removed them, so the migration is required for Boot 4. For fields on test classes it is just a change of import and name. The thing to watch is that @MockitoBean is not processed in @Configuration or @TestConfiguration classes.
If a team shared mocks that way, the annotation is silently ignored and the real bean runs, so I would move those declarations to a base test class or an interface. Newer versions also allow type-level declarations, which I would check for our version.
And I would remind the team that each distinct set of overridden beans is a different context cache key, so standardising mock sets keeps the suite fast.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6, Spring Boot 4, Spring Boot 3.3, Spring Boot 3.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference, Testing
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Spring Boot 3.4 deprecated @MockBean in favour of @MockitoBean. What does @MockitoBean...": https://scrivio-interview-library.netlify.app/spring-boot/spring-boot-test/e3c4270ea9/