Junior — Middle
What library do you use to create mock objects in tests?
sobes.tech AI
Answer from AI
Mockito is a library often used for creating mock objects in Java tests. It is easy to use, integrates well with JUnit, and allows creating mocks, setting behaviors, and verifying method calls.
Example of using Mockito:
import static org.mockito.Mockito.*;
// Creating a mock
List<String> mockedList = mock(List.class);
// Setting behavior
when(mockedList.get(0)).thenReturn("first element");
// Using
String element = mockedList.get(0); // will return "first element"
// Verifying method call
verify(mockedList).get(0);
Mockito is the de facto standard for mocking in Java projects.