Junior — Middle
Do you use the Mockito library for writing tests?
sobes.tech AI
Answer from AI
Yes, the Mockito library is widely used for writing unit tests in Java. It allows creating mock objects — simulations of real classes, to isolate the tested code from dependencies.
Main features of Mockito:
- Creating mock objects.
- Configuring the behavior of mock object methods.
- Verifying method calls (verify).
Example of usage:
import static org.mockito.Mockito.*;
// Creating a mock
List<String> mockedList = mock(List.class);
// Configuring behavior
when(mockedList.get(0)).thenReturn("First element");
// Using
System.out.println(mockedList.get(0)); // Will output "First element"
// Verifying call
verify(mockedList).get(0);
Mockito helps write clean and independent tests, especially when the code depends on external services or complex objects.