CheckOps home

Tools directory

M

CheckOps

Mockito

Mockito is the MIT-licensed, most popular mocking framework for Java, used to create mocks, stubs and spies with a clean API; Mockito 5 uses the inline mock maker by default and requires Java 11.

Visit website

Latest public signals

Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.

Visit Website

Introduction

Mockito describes itself as the most popular mocking framework for Java. It lets you create test doubles for dependencies, stub their behaviour and verify interactions, so a unit test can focus on one class without real databases, HTTP clients or message queues.

The README summarises the version history. Mockito 2 modernised the API, Mockito 3 required Java 8, Mockito 4 removed deprecated API, and Mockito 5 switched the default mock maker to mockito-inline and now requires Java 11. Only one major version is supported at a time, and changes are not backported. Mockito publishes snapshots for every change and a new minor or patch release to Maven Central roughly once a month; v5.24.0 was released on 23 September 2026.

The inline mock maker matters because it allows mocking of final classes and methods, and static methods via mockStatic, which previously needed extra configuration or tools such as PowerMock. This made Mockito much easier to use with Kotlin, whose classes are final by default, although many Kotlin teams prefer MockK.

How it compares: EasyMock uses a record/replay style and Spock includes its own mocking in Groovy specifications, while Mockito’s “arrange, act, verify” style with when(...).thenReturn(...) and verify(...) reads naturally inside JUnit or TestNG tests. Mockito also integrates with JUnit 5 through MockitoExtension, and Spring Boot’s test support includes it by default, which explains its reach.

In the quality loop, Mockito supports fast unit tests and focused component tests by isolating code from slow or unreliable collaborators.

The source code is at mockito/mockito on GitHub under the MIT licence, with about 15,400 stars; the most recent commit was on 5 November 2026.

Mockito also ships useful helpers beyond basic mocks. BDD-style aliases (given(...).willReturn(...) and then(mock).should()) suit teams that write tests in given/when/then form. @MockitoSettings(strictness = ...) tunes how strict stubbing should be per test class. mockConstruction intercepts objects created with new inside code you cannot easily refactor. For Android, Mockito works with local JVM unit tests and Robolectric, and there are separate artefacts for instrumented tests. The project’s wiki and Javadoc are the primary documentation, and the Javadoc of the Mockito class doubles as a complete, example-led user guide.

Features

  1. Mocks and stubs: mock(Foo.class) with when(...).thenReturn(...), thenThrow and thenAnswer for flexible behaviour.
  2. Verification: verify(mock).method(args), with times, never, atLeast and in-order verification.
  3. Argument matchers and captors: any(), eq(), custom matchers and ArgumentCaptor to inspect what was passed.
  4. Spies: spy(realObject) wraps real objects so you can stub only selected methods.
  5. Inline mock maker: Default in Mockito 5; supports final classes, final methods and static mocking.
  6. Annotations: @Mock, @Spy, @Captor and @InjectMocks reduce boilerplate.
  7. JUnit 5 extension: @ExtendWith(MockitoExtension.class) initialises mocks and reports unnecessary stubbing.
  8. Strict stubs: Detects unused stubs and argument mismatches to keep tests clean.

Pros

  1. Readable tests: The fluent API reads close to plain English.
  2. Everywhere in Java: Default in Spring Boot test starters and supported by every major IDE and build tool.
  3. Free to use: Open source under the MIT licence, so the core tool costs nothing to use.
  4. Actively maintained: The repository had commits as recent as 5 November 2026.
  5. Community: About 15,400 GitHub stars.

Cons

  1. Easy to over-mock: Mocking everything can produce brittle tests that mirror implementation details.
  2. Static and constructor mocking is a smell: It is possible, but often signals code that would benefit from refactoring.
  3. Only one supported major: Teams on older majors do not receive fixes.
  4. Java 11 baseline: Mockito 5 requires Java 11; Java 8 projects must stay on Mockito 4.

Add the dependency (Maven):

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-junit-jupiter</artifactId>
  <version>5.24.0</version>
  <scope>test</scope>
</dependency>

A JUnit 5 test with a mock and verification:

@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
  @Mock PaymentGateway gateway;
  @InjectMocks OrderService service;

  @Test
  void chargesTheCustomer() {
    when(gateway.charge("ana", 4200)).thenReturn(Receipt.ok());

    service.checkout("ana", 4200);

    verify(gateway).charge("ana", 4200);
    verifyNoMoreInteractions(gateway);
  }
}

Capture an argument to assert on it:

ArgumentCaptor<Email> captor = ArgumentCaptor.forClass(Email.class);
verify(mailer).send(captor.capture());
assertThat(captor.getValue().subject()).contains("Order confirmed");

Practical tips:

  1. Mock roles, not values: mock collaborators such as gateways and repositories, and use real objects for simple data.
  2. Prefer state-based assertions on results; use verify for interactions that genuinely matter.
  3. Keep strict stubs on so unused stubbing is reported.

Resources:

Pricing

Mockito is free and open source under the MIT licence.

  1. Java and Spring developers writing unit tests.
  2. Android developers writing local JVM unit tests, with or without Robolectric.
  3. Teams isolating slow dependencies such as databases, HTTP clients and queues.
  4. Kotlin teams on mixed Java/Kotlin codebases (MockK is an alternative for idiomatic Kotlin).
  5. Not a fit for integration tests where real dependencies (for example via Testcontainers) give more confidence.

Conclusion

Mockito is the standard mocking library for Java, with a readable API, strong JUnit 5 integration and modern capabilities such as final and static mocking by default.

Used with restraint, alongside real-object tests and integration tests, it keeps Java unit tests fast and focused.

Alternatives

Other tools on CheckOps that cover similar ground. Open a page, or compare one with Mockito side by side.