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.
Latest public signals
Not refreshed yet. Refresh reads the public site and, when the entry has a GitHub repository, the latest release.
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
- Mocks and stubs:
mock(Foo.class)withwhen(...).thenReturn(...),thenThrowandthenAnswerfor flexible behaviour. - Verification:
verify(mock).method(args), withtimes,never,atLeastand in-order verification. - Argument matchers and captors:
any(),eq(), custom matchers andArgumentCaptorto inspect what was passed. - Spies:
spy(realObject)wraps real objects so you can stub only selected methods. - Inline mock maker: Default in Mockito 5; supports final classes, final methods and static mocking.
- Annotations:
@Mock,@Spy,@Captorand@InjectMocksreduce boilerplate. - JUnit 5 extension:
@ExtendWith(MockitoExtension.class)initialises mocks and reports unnecessary stubbing. - Strict stubs: Detects unused stubs and argument mismatches to keep tests clean.
Pros
- Readable tests: The fluent API reads close to plain English.
- Everywhere in Java: Default in Spring Boot test starters and supported by every major IDE and build tool.
- Free to use: Open source under the MIT licence, so the core tool costs nothing to use.
- Actively maintained: The repository had commits as recent as 5 November 2026.
- Community: About 15,400 GitHub stars.
Cons
- Easy to over-mock: Mocking everything can produce brittle tests that mirror implementation details.
- Static and constructor mocking is a smell: It is possible, but often signals code that would benefit from refactoring.
- Only one supported major: Teams on older majors do not receive fixes.
- Java 11 baseline: Mockito 5 requires Java 11; Java 8 projects must stay on Mockito 4.
Usage and Links (Example)
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:
- Mock roles, not values: mock collaborators such as gateways and repositories, and use real objects for simple data.
- Prefer state-based assertions on results; use
verifyfor interactions that genuinely matter. - Keep strict stubs on so unused stubbing is reported.
Resources:
Pricing
Mockito is free and open source under the MIT licence.
Recommended for?
- Java and Spring developers writing unit tests.
- Android developers writing local JVM unit tests, with or without Robolectric.
- Teams isolating slow dependencies such as databases, HTTP clients and queues.
- Kotlin teams on mixed Java/Kotlin codebases (MockK is an alternative for idiomatic Kotlin).
- 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.
MMockKMockK is an Apache-2.0 mocking library built for Kotlin, supporting final classes, objects, coroutines, extension functions and top-level functions with a concise DSL.Compare with Mockito
Jjqwikjqwik is an EPL-2.0 property-based testing engine for the JUnit Platform, letting Java and Kotlin developers write properties with generated, shrinking inputs alongside regular JUnit 5 tests.Compare with Mockito
JJUnit 5JUnit 5 is the current JUnit for Java and the JVM: Jupiter for new tests, Platform to launch them, Vintage to run JUnit 4.Compare with Mockito
TTestNGTestNG is an Apache-2.0 testing framework for Java, inspired by JUnit and NUnit, with annotations, groups, dependencies, data providers, parallel execution and XML suite configuration; it requires Java 11+.Compare with Mockito
AAssertJAssertJ is an Apache-2.0 fluent assertions library for Java, with rich, discoverable assertions for core types, collections, exceptions, dates and more, plus soft assertions and custom assertion generation.Compare with Mockito
SSpock FrameworkSpock is an Apache-2.0 BDD-style testing and specification framework for Java and Groovy applications, with given/when/then blocks, powerful data-driven tests and built-in mocking, running on the JUnit Platform.Compare with Mockito