@MockitoBean Annotation in Spring Boot

🚀 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 @MockitoBean improves 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! 🚀

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare