🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
🚀 Introduction: Why @MockBean is Deprecated?
In Spring Boot unit testing, @MockBean has been widely used to mock dependencies in test cases. However, starting from Spring Boot 3.2, @MockBean is deprecated and replaced with @MockitoBean.
🔴 What’s the Issue with @MockBean?
- It relies on internal Spring mechanisms that have become inefficient.
- It creates beans at runtime, which slows down test execution.
- The new
@MockitoBeanimproves integration with Mockito while reducing test complexity.
✅ Solution:
Use @MockitoBean instead of @MockBean in Spring Boot 3.2+ for unit testing.
1️⃣ What is @MockitoBean?
@MockitoBean is an annotation introduced in Spring Boot 3.2 to replace @MockBean. It helps in injecting Mockito mocks into the Spring application context for testing.
✔ Provides better integration with Spring Boot’s test framework.
✔ Optimized for Spring Boot 3+.
✔ Reduces test execution overhead.
2️⃣ How to Use @MockitoBean in Spring Boot Tests
Let’s replace @MockBean with @MockitoBean in a Spring Boot JUnit test case.
🔹 Example: Testing a UserService Class
📌 We have a UserService that calls a UserRepository. We want to mock UserRepository in our unit test.
✅ Step 1: Define UserService (Actual Implementation)
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User getUserById(Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
}
}
❌ Step 2: Old Approach Using @MockBean (Deprecated)
@SpringBootTest
class UserServiceTest {
@MockBean // ❌ Deprecated in Spring Boot 3.2+
private UserRepository userRepository;
@Autowired
private UserService userService;
@Test
void testGetUserById() {
User mockUser = new User(1L, "John Doe");
Mockito.when(userRepository.findById(1L)).thenReturn(Optional.of(mockUser));
User result = userService.getUserById(1L);
assertEquals("John Doe", result.getName());
}
}
📌 ❌ This approach is no longer recommended!
✅ Step 3: New Approach Using @MockitoBean
@SpringBootTest
class UserServiceTest {
@MockitoBean // ✅ New Recommended Approach
private UserRepository userRepository;
@Autowired
private UserService userService;
@Test
void testGetUserById() {
User mockUser = new User(1L, "John Doe");
Mockito.when(userRepository.findById(1L)).thenReturn(Optional.of(mockUser));
User result = userService.getUserById(1L);
assertEquals("John Doe", result.getName());
}
}
✅ This is the new recommended way to mock dependencies in Spring Boot 3.2+.
3️⃣ Key Differences: @MockBean vs. @MockitoBean
| Feature | @MockBean (Deprecated) |
@MockitoBean (Recommended) |
|---|---|---|
| Spring Boot Version | Before 3.2 | 3.2+ |
| Performance | Slower test execution | Faster and optimized |
| Mockito Integration | Uses internal Spring proxies | Directly integrates with Mockito |
| Recommendation | ❌ Deprecated | ✅ Use this |
4️⃣ When to Use @MockitoBean?
✔ For Unit Tests – When you need to mock a dependency inside a Spring Boot test.
✔ For Testing Service Layers – When a service depends on a repository or external API.
✔ For Reducing Test Execution Time – Improves performance in large test suites.
🚀 Best practice: Use @MockitoBean in Spring Boot 3.2+ tests instead of @MockBean.
🎯 Conclusion: Why Switch to @MockitoBean?
By replacing @MockBean with @MockitoBean, you get:
✅ Faster test execution.
✅ Better integration with Spring Boot 3.2+.
✅ More efficient mocking with Mockito.
🚀 Are you using @MockitoBean in your Spring Boot 3+ projects? Comment below!
🔗 Share this guide with developers to help them migrate from @MockBean to @MockitoBean! 🚀
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment