Difference between Mock, MockBean and Mockito.mock
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with unit testing in Java applications, particularly those that use Spring Framework and Mockito, developers frequently encounter @Mock, @MockBean, and Mockito.mock(). Each of these has its own use cases and implications. Understanding the differences can streamline the testing process, making it more effective and precise.
Overview of @Mock, @MockBean, and Mockito.mock()
@Mock
@Mock is an annotation from the Mockito library used for creating mock objects. It is typically utilized in conjunction with @RunWith(MockitoJUnitRunner.class) or by initializing the Mockito annotations with MockitoAnnotations.initMocks(this) in the setup method.
- Purpose: To create a mock object for a given class or interface, enabling isolation from external dependencies during testing.
- Usage:
- Environment: Purely Mockito/Java based, without dependency on Spring.
@MockBean
@MockBean is specific to the Spring Boot testing framework. It is a Spring-specific variant that facilitates mocking in a Spring application context, ensuring that the mock objects are injected into the Spring context.
- Purpose: To create a mock bean within the Spring ApplicationContext, primarily for use in integration tests or when you want to control the behavior of a bean within a Spring-managed test.
- Usage:
- Environment: Requires Spring context, specifically useful within a
@SpringBootTest.
Mockito.mock()
Mockito.mock() is a static method from the Mockito library that provides an alternative to the @Mock annotation for creating mock objects programmatically within test methods.
- Purpose: To create mock objects with a specific setup that may not necessarily require an annotation-driven approach.
- Usage:
- Environment: Mockito/Java based, no dependency on Spring or additional annotations.
Differences at a Glance
| Feature/Aspect | @Mock | @MockBean | Mockito.mock() |
| Library | Mockito | Spring Boot + Mockito | Mockito |
| Application Context | Not required | Required | Not required |
| Purpose | Isolate unit tests | Mock beans in a Spring context | Programmatically create mocks |
| Initialization | @RunWith or initMocks() | Automatically managed by Spring | Direct method call |
| Usage Scenario | Unit tests | Spring integration tests | Any testing scenario |
| Flexibility | Annotation-based, less flexible | Spring-dependant | Flexible method-based |
Considerations for Use
- Use
@MockorMockito.mock()for Unit Testing - When the goal is to isolate the unit test from external dependencies without involving the Spring ecosystem,@MockandMockito.mock()are preferable.@Mockis annotation-driven and suits tests leveragingMockitoJUnitRunner.Mockito.mock(), on the other hand, is straightforward for smaller tests whereinitMocks()feels excessive. - Use
@MockBeanfor Integration Tests within Spring Context - When writing tests that deal with Spring's application context or require the application bootstrapping provided by Spring Boot (e.g.,@SpringBootTest),@MockBeanis essential. It allows replacing parts of the context that are otherwise integrated, such as data repositories or service beans, with controlled mock instances. - Mock Configuration - While all three approaches mock behavior in a similar fashion, the way they are configured and the environments they target vary significantly. Understanding the testing context (unit vs. integration) greatly guides which approach to use.
By selecting the appropriate mocking strategy based on the testing scenario and environment setup, developers can ensure more maintainable, reliable, and efficient tests.
Related reading
- Difference between null and empty Java String
- Difference between on-heap and off-heap
- Difference between OneToMany and ElementCollection?
- Difference between OpenJDK and Adoptium/AdoptOpenJDK
- Difference between setUp and setUpBeforeClass
- Difference between using MockMvc with SpringBootTest and Using WebMvcTest
- Difference between Optional.orElse() and Optional.orElseGet()
- Difference between parseInt() and valueOf() in Java?

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.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.