Mockito Interview Guide
1. What is Mockito?
Mockito is a mocking framework for Java that allows developers to create and configure mock objects for unit
testing. It is commonly used with JUnit to isolate dependencies and verify interactions.
- Provides a fluent API to create mock objects
- Supports stubbing methods and verification of interactions
- Enables testing in isolation without requiring real implementations
2. Core Concepts
- **Mock**: A simulated object that mimics the behavior of real objects in controlled ways
- **Stub**: Predefined behavior of a mock when a certain method is called
- **Verify**: Check if certain methods were called on the mock
- **Spy**: A real object wrapped so that specific methods can be stubbed, while others remain real
- **Annotations**: @Mock, @Spy, @InjectMocks, and @Captor help simplify creation and injection of mocks
3. Setup and Annotations
- Add Mockito dependency:
Maven:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
Mockito Interview Guide
- @Mock: Create mock instance for a field
- @Spy: Create a spy instance for a field
- @InjectMocks: Automatically inject mocks into tested object
- @Captor: Create ArgumentCaptor instances
- Initialize mocks:
- MockitoAnnotations.openMocks(this) in @BeforeEach
- Or use @ExtendWith(MockitoExtension.class) with JUnit 5
4. Stubbing Methods
Basic stubbing syntax:
when(mock.method(args)).thenReturn(value);
when(mock.method(args)).thenThrow(new Exception());
- Example:
when(userService.getUser(1)).thenReturn(new User(1, "John"));
- Use doReturn(), doThrow() when mocking void methods:
doThrow(new RuntimeException()).when(mock).voidMethod();
5. Verification
- Verify method calls:
verify(mock).method(args);
- Verify number of invocations:
verify(mock, times(2)).method(args);
- Other verifications:
verify(mock, never()).method(args);
verify(mock, atLeastOnce()).method(args);
Mockito Interview Guide
verifyNoMoreInteractions(mock);
6. Argument Captors
- Capture arguments passed to mock methods:
@Captor
ArgumentCaptor<User> userCaptor;
verify(userService).save(userCaptor.capture());
User captured = userCaptor.getValue();
7. Spies vs Mocks
- **Mock**: Default behavior is to return default values (null, 0, false) unless stubbed
- **Spy**: Wraps an actual instance, so real methods execute unless stubbed
- Use cases:
- Spy: When you need to test partial behavior of real object
- Mock: When you only care about interactions and not actual logic
8. Common Mockito Interview Questions
- What is the difference between @Mock and @Spy?
- How do you stub void methods?
- How to verify invocation order of methods?
- What is ArgumentCaptor and when to use it?
- How do you handle exceptions with Mockito?
Mockito Interview Guide
- How do you inject mocks into the object being tested?
- Differences between JUnit 4 and JUnit 5 setup for Mockito?
9. Best Practices
- Prefer annotations (@Mock, @InjectMocks) over manual mock creation for readability
- Keep tests independent and focused on one behavior
- Avoid stubbing methods you do not use in tests
- Use BDDMockito.when() for Behavior-Driven style
- Reset mocks only when necessary, avoid overuse