A comprehensive Java mocking framework that enables developers to create test doubles for unit testing.
—
Mockito provides several ways to create mock objects and spies for testing. Mocks are fake objects that record interactions, while spies are partial mocks that wrap real objects.
Create a mock of any class or interface:
public static <T> T mock(Class<T> classToMock);Usage Example:
List<String> mockedList = mock(List.class);
Map<String, Integer> mockedMap = mock(Map.class);
UserService mockedService = mock(UserService.class);Create mocks with custom names for better debugging:
public static <T> T mock(Class<T> classToMock, String name);Usage Example:
List<String> mockedList = mock(List.class, "userList");
// Error messages will show "userList" instead of generic mock nameCreate mocks with predefined behavior for unstubbed methods:
public static <T> T mock(Class<T> classToMock, Answer defaultAnswer);Usage Example:
List<String> mockedList = mock(List.class, RETURNS_SMART_NULLS);
UserService service = mock(UserService.class, CALLS_REAL_METHODS);Create mocks with advanced configuration options:
public static <T> T mock(Class<T> classToMock, MockSettings mockSettings);Usage Example:
UserService service = mock(UserService.class,
withSettings()
.name("userService")
.defaultAnswer(RETURNS_SMART_NULLS)
.extraInterfaces(Serializable.class)
.serializable());@Deprecated
public static <T> T mock(Class<T> classToMock, ReturnValues returnValues);Create a spy that wraps a real object:
public static <T> T spy(T object);Usage Example:
List<String> realList = new ArrayList<>();
List<String> spyList = spy(realList);
// Real method is called
spyList.add("item");
assertEquals(1, spyList.size());
// Can stub methods
when(spyList.size()).thenReturn(100);
assertEquals(100, spyList.size());Create a spy of a class without providing an instance:
public static <T> T spy(Class<T> classToSpy);Usage Example:
UserService spyService = spy(UserService.class);
// Mockito creates an instance using default constructorConfigure advanced mock behavior:
public static MockSettings withSettings();
public interface MockSettings {
MockSettings name(String name);
MockSettings defaultAnswer(Answer defaultAnswer);
MockSettings extraInterfaces(Class<?>... interfaces);
MockSettings serializable();
MockSettings verboseLogging();
MockSettings invocationListeners(InvocationListener... listeners);
MockSettings spiedInstance(Object spiedInstance);
}Usage Examples:
// Mock with multiple interfaces
Runnable mock = mock(Runnable.class,
withSettings().extraInterfaces(Serializable.class, Cloneable.class));
// Serializable mock
List<String> serializableMock = mock(List.class,
withSettings().serializable());
// Mock with verbose logging
UserService verboseService = mock(UserService.class,
withSettings().verboseLogging());
// Mock with custom default answer
Calculator calc = mock(Calculator.class,
withSettings().defaultAnswer(RETURNS_SMART_NULLS));Control how unstubbed methods behave:
public static final Answer<Object> RETURNS_DEFAULTS;
public static final Answer<Object> RETURNS_SMART_NULLS;
public static final Answer<Object> RETURNS_MOCKS;
public static final Answer<Object> RETURNS_DEEP_STUBS;
public static final Answer<Object> CALLS_REAL_METHODS;mock.getFoo().getBar().getBaz() works without explicit stubbingUse Mocks when:
Use Spies when:
// WRONG - real method is called during stubbing
List<String> spy = spy(new ArrayList<>());
when(spy.get(0)).thenReturn("foo"); // IndexOutOfBoundsException!
// CORRECT - use doReturn family for spies
doReturn("foo").when(spy).get(0);// Good - descriptive names for debugging
UserService userService = mock(UserService.class, "userService");
// Good - appropriate default answers
HttpClient client = mock(HttpClient.class, RETURNS_SMART_NULLS);
// Good - extra interfaces when needed
Runnable task = mock(Runnable.class,
withSettings().extraInterfaces(Serializable.class));Install with Tessl CLI
npx tessl i tessl/maven-org-mockito--mockito-all