Junior — Middle
What experience do you have with using Mockito for testing mock objects?
sobes.tech AI
Answer from AI
Mockito is a popular library for creating mock objects in Java, which helps isolate the code under test from dependencies.
The main usage experience includes:
- Creating mocks using
Mockito.mock()or the@Mockannotation. - Configuring the behavior of mock methods with
when(...).thenReturn(...). - Verifying method calls with
verify(). - Using
ArgumentCaptorto capture and verify call arguments.
Example:
import static org.mockito.Mockito.*;
List<String> mockedList = mock(List.class);
when(mockedList.get(0)).thenReturn("first");
System.out.println(mockedList.get(0)); // outputs "first"
verify(mockedList).get(0);
This approach allows testing logic without real dependencies, speeding up and simplifying test writing.