or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

additional-answers.mdadditional-matchers.mdadvanced-features.mdannotations.mdargument-matching.mdbdd-testing.mdindex.mdmock-creation.mdstatic-mocking.mdstubbing.mdverification.md
tile.json

tessl/maven-org-mockito--mockito-core

Mockito mock objects library core API and implementation for comprehensive Java unit testing

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.mockito/mockito-core@5.19.x

To install, run

npx @tessl/cli install tessl/maven-org-mockito--mockito-core@5.19.0

index.mddocs/

Mockito Core

Mockito 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 Information

  • 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'

Core Imports

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.*;

Basic Usage

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());
    }
}

Architecture

Mockito Core consists of several key components:

  • Mock Creation: The Mockito class provides static methods for creating mocks and spies
  • Stubbing: OngoingStubbing and Stubber interfaces provide fluent APIs for defining mock behavior
  • Verification: Built-in verification modes support different assertion patterns
  • Argument Matching: Flexible matchers in ArgumentMatchers enable sophisticated stubbing and verification
  • Annotations: @Mock, @Spy, @InjectMocks annotations simplify test setup
  • Advanced Features: Static mocking, construction mocking, and custom answers for complex scenarios

Capabilities

Mock Creation and Basic Operations

Core 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

Method Stubbing

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()

Method Stubbing and Behavior

Verification and Assertions

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)

Verification and Assertions

Argument Matching

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)

Argument Matching

Annotations and Test Setup

Annotation-based mock creation and dependency injection.

@Mock
@Spy  
@InjectMocks
@Captor
public static AutoCloseable openMocks(Object testClass)

Annotations and Test Setup

Static and Construction Mocking

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

BDD-Style Testing

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)

BDD-Style Testing

Additional Argument Matchers

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)

Additional Argument Matchers

Additional Answer Behaviors

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)

Additional Answer Behaviors

Advanced Features

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

Core Types

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();
}