What is the replacement for the deprecated MockBeans in SpringBoot 3.4.0?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
In Spring Boot 3.4, @MockBean and the container annotation @MockBeans were deprecated in favor of Spring Framework's new bean-override testing support. The replacement is @MockitoBean for mocks and @MockitoSpyBean for spies, both from Spring Framework 6.2.
What Changed In Spring Boot 3.4
Historically, Spring Boot provided Mockito test annotations in org.springframework.boot.test.mock.mockito, including:
- '
@MockBean' - '
@MockBeans' - '
@SpyBean'
In Spring Boot 3.4, that package was deprecated for removal in favor of the new Spring Framework bean-override annotations.
The practical replacement for @MockBeans is not "find another container annotation in Boot." The normal replacement is to use one or more @MockitoBean declarations from Spring Framework.
Use @MockitoBean Instead
Field-level usage is the most direct migration path:
That covers the common use case where @MockBeans used to group several mock declarations on a test class.
The benefit is that the annotation now comes from Spring Framework's unified testing support instead of a Boot-specific layer.
Type-Level Replacement For Grouped Mocking
If you prefer type-level declarations, @MockitoBean also supports that style.
This is the closest conceptual replacement for @MockBeans: multiple repeatable mock annotations on the class.
In other words, the replacement is usually repeated @MockitoBean, not another one-off Boot container wrapper.
Use @MockitoSpyBean For Spies
If your old tests used @SpyBean, the matching replacement is @MockitoSpyBean.
This is a separate concern from @MockBeans, but it is part of the same migration path away from Boot's deprecated Mockito test annotations.
Migration Example
Old style:
New style:
Or field-based:
Pick the style that best matches how your team writes tests.
Why The Change Happened
The move is part of a broader cleanup in which Spring Framework now owns the test bean-override abstraction directly. That reduces duplication between Boot and Framework testing features and gives the mocking model a more consistent home.
From a test author's perspective, the main consequence is import changes and annotation migration, not a completely different mental model.
You are still overriding beans in the application context with Mockito-managed test doubles. The package and annotation names changed, and the older Boot ones are on a removal path.
Common Pitfalls
The biggest mistake is replacing @MockBeans with repeated @MockBean and assuming the migration is complete. In Spring Boot 3.4, @MockBean itself is also deprecated.
Another mistake is forgetting to update the imports. The replacement annotations come from org.springframework.test.context.bean.override.mockito, not the old Boot package.
People also confuse mocks and spies during migration. @MockitoBean replaces mock semantics, while @MockitoSpyBean replaces spy semantics.
Finally, if you migrate type-level annotations, check the attribute names carefully. @MockitoBean uses attributes that differ from some older Boot examples.
Summary
- In Spring Boot 3.4,
@MockBeansis deprecated along with@MockBean. - The replacement is Spring Framework's
@MockitoBean, usually repeated for multiple mocks. - Use
@MockitoSpyBeanwhen the old test used spy behavior. - The new annotations live in
org.springframework.test.context.bean.override.mockito. - The migration is mostly about changing imports and annotations, not about changing the underlying testing strategy.
Related reading
- What's the correct alternative to static method inheritance?
- What's the difference between an Algorithm and a Design Pattern
- When and why would you seal a class?
- When do Java generics require ? extends T instead of T and is there any downside of switching?
- What is the significance of load factor in HashMap?
- What is the simplest way to convert a Java string from all caps words separated by underscores to CamelCase no word separators?
- What would be the fastest method to test for primality in Java?
- What's a quick way to test to see a file exists?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.