In Spring, what is the difference between @Mock and @MockBean annotations?

@Mock creates a plain Mockito mock for use in isolated unit tests outside any Spring context, while @MockBean creates a mock and injects it into the live Spring application context, replacing the real bean.

Key Points

Example

Testing a UserService class's validation logic alone uses @Mock for its UserRepository dependency and MockitoExtension, with no Spring context at all; testing a UserController's HTTP behavior with @WebMvcTest uses @MockBean to replace the real UserService bean inside the loaded web layer context.

// Pure unit test
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
    @Mock UserRepository repository;
    @InjectMocks UserService service;
}

// Spring context test
@WebMvcTest(UserController.class)
class UserControllerTest {
    @MockBean UserService userService;
}

Interview Tip

A concise interview answer is

@Mock is a plain Mockito mock used in isolated unit tests with no Spring context involved. @MockBean does the same mocking but registers the mock into the actual Spring ApplicationContext, replacing the real bean, which is what I use in @WebMvcTest or @SpringBootTest-style integration tests.

Last updated: 16 July 2026

Previous question: Describe the process of creating a custom Spring Boot starter. Why might this be useful?

Next question: Explain the role of the @Async annotation in Spring Framework.

More Spring Boot - Level IV (Advanced) interview questions

All Spring Boot - Level IV (Advanced) questions