Mockito mock objects library core API and implementation for comprehensive Java unit testing
npx @tessl/cli install tessl/maven-org-mockito--mockito-core@5.19.0Mockito Core is the most popular mocking framework for Java. It enables developers to create mock objects, verify interactions, and stub method behaviors for comprehensive unit testing. The library offers a fluent API for creating mocks, spies, and stubs with features including argument matchers, verification methods, stubbing capabilities, and behavioral verification.
Package Name: mockito-core
Package Type: maven
Language: Java
Installation:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.19.0</version>
</dependency>Gradle:
testImplementation 'org.mockito:mockito-core:5.19.0'import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.*;
import org.mockito.Mock;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;For BDD-style testing:
import static org.mockito.BDDMockito.*;import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.*;
import org.junit.jupiter.api.Test;
class BasicMockitoExample {
@Test
void basicMocking() {
// Create mock
List<String> mockList = mock(List.class);
// Stub method behavior
when(mockList.get(0)).thenReturn("first");
when(mockList.size()).thenReturn(1);
// Use mock
System.out.println(mockList.get(0)); // prints "first"
System.out.println(mockList.size()); // prints 1
// Verify interactions
verify(mockList).get(0);
verify(mockList).size();
}
@Test
void verificationExample() {
List<String> mockList = mock(List.class);
// Use mock
mockList.add("one");
mockList.add("two");
mockList.clear();
// Verify specific interactions
verify(mockList).add("one");
verify(mockList).add("two");
verify(mockList).clear();
verify(mockList, times(2)).add(anyString());
}
}Mockito Core consists of several key components:
Mockito class provides static methods for creating mocks and spiesOngoingStubbing and Stubber interfaces provide fluent APIs for defining mock behaviorArgumentMatchers enable sophisticated stubbing and verification@Mock, @Spy, @InjectMocks annotations simplify test setupCore functionality for creating and managing mock objects.
public static <T> T mock(Class<T> classToMock)
public static <T> T mock(Class<T> classToMock, String name)
public static <T> T mock(Class<T> classToMock, Answer defaultAnswer)
public static <T> T spy(T object)
public static <T> T spy(Class<T> classToSpy)
public static MockSettings withSettings()Mock Creation and Configuration
Configure mock object behavior with flexible stubbing options.
public static <T> OngoingStubbing<T> when(T methodCall)
public static Stubber doReturn(Object toBeReturned)
public static Stubber doThrow(Throwable... toBeThrown)
public static Stubber doNothing()
public static Stubber doAnswer(Answer answer)
public static Stubber doCallRealMethod()Verify method invocations with flexible verification modes.
public static <T> T verify(T mock)
public static <T> T verify(T mock, VerificationMode mode)
public static InOrder inOrder(Object... mocks)
public static void verifyNoMoreInteractions(Object... mocks)
public static void verifyNoInteractions(Object... mocks)Flexible argument matchers for stubbing and verification.
public static <T> T any()
public static <T> T any(Class<T> type)
public static String anyString()
public static int anyInt()
public static boolean anyBoolean()
public static <T> T eq(T value)
public static <T> T argThat(ArgumentMatcher<T> matcher)Annotation-based mock creation and dependency injection.
@Mock
@Spy
@InjectMocks
@Captor
public static AutoCloseable openMocks(Object testClass)Mock static methods and object construction (since Mockito 3.4.0+).
public static <T> MockedStatic<T> mockStatic(Class<T> classToMock)
public static <T> MockedConstruction<T> mockConstruction(Class<T> classToMock)Static and Construction Mocking
Behavior-driven development style API with given/when/then syntax.
public static <T> BDDMyOngoingStubbing<T> given(T methodCall)
public static <T> Then<T> then(T mock)
public static Stubber willReturn(Object value)
public static Stubber willThrow(Throwable... throwables)Advanced argument matchers for complex matching scenarios including numerical comparisons, array equality, and logical combinations.
public static <T extends Comparable<T>> T geq(T value)
public static <T extends Comparable<T>> T leq(T value)
public static <T extends Comparable<T>> T gt(T value)
public static <T extends Comparable<T>> T lt(T value)
public static String find(String regex)
public static <T> T[] aryEq(T[] value)
public static <T> T and(T first, T second)
public static <T> T or(T first, T second)
public static <T> T not(T value)Advanced stubbing behaviors including argument-based returns, call delegation, and functional interface answers.
public static <T> Answer<T> returnsFirstArg()
public static <T> Answer<T> returnsSecondArg()
public static <T> Answer<T> returnsLastArg()
public static <T> Answer<T> returnsArgAt(int position)
public static <T> Answer<T> delegatesTo(Object delegate)
public static <T> Answer<T> returnsElementsOf(Collection<?> elements)
public static <T> Answer<T> answersWithDelay(long sleepyTime, Answer<T> answer)Custom answers, session management, and framework integration.
public static MockitoSession mockitoSession()
public static MockitoFramework framework()
public static void validateMockitoUsage()
public static void reset(Object... mocks)Advanced Features and Integration
interface MockSettings {
MockSettings name(String name);
MockSettings defaultAnswer(Answer defaultAnswer);
MockSettings extraInterfaces(Class<?>... interfaces);
MockSettings serializable();
MockSettings strictness(Strictness strictness);
MockSettings lenient();
}
interface OngoingStubbing<T> {
OngoingStubbing<T> thenReturn(T value);
OngoingStubbing<T> thenReturn(T value, T... values);
OngoingStubbing<T> thenThrow(Throwable... throwables);
OngoingStubbing<T> thenAnswer(Answer<?> answer);
OngoingStubbing<T> thenCallRealMethod();
}
interface VerificationMode {
void verify(VerificationData data);
VerificationMode description(String description);
}
interface Answer<T> {
T answer(InvocationOnMock invocation) throws Throwable;
}
interface InOrder {
<T> T verify(T mock);
<T> T verify(T mock, VerificationMode mode);
void verifyNoMoreInteractions();
}