- Home
- Unit Testing Frameworks
- Mockito
Mockito
What it is
Mockito is a free Java mocking framework for writing unit tests. It lets developers mock interfaces and concrete classes, define mock behavior before execution, and verify chosen interactions afterward. Verification options include ordered checks, exact or minimum call counts, and flexible argument matching. Spies support partial mocking: real methods can run while still being stubbed and verified. The API also includes @InjectMocks for injecting mock or spy fields. The recommended installation is the mockito-core dependency through a build system; the project provides setup directions for Maven Central and Gradle and refers users to other build systems. Mockito can be used on Android with Dexmaker. Mockito 5 requires Java 11, and mockito-android tests require an Android P (API 28) or newer device or emulator. The project is MIT licensed and lists its plan at no cost. Help is available through Stack Overflow, a moderated mailing list, and GitHub issues. Commercial support and maintenance are available through a Tidelift subscription. The project advises against mocking types developers do not own or value objects, and against indiscriminate mocking.
For and against
For
- Mocks interfaces and concrete classes
- Supports ordered and count-based verification
- Spies allow partial mocking
- MIT licensed and free
- Provides Maven Central and Gradle setup instructions
Against
- Mockito 5 requires Java 11
- Android tests require API 28 or newer
- Android use requires Dexmaker
The verdict
Mockito provides a focused set of mocking, stubbing, and verification tools for Java unit tests. Check the Java and Android requirements and follow the project’s cautions about what to mock.
Who it suits
Mockito suits Java developers writing unit tests who need to control dependencies and check interactions. Android developers can use it with Dexmaker, subject to the stated version requirements.
Plans and prices
Mockito
Open-source Java mocking framework
Read 9 Oct 2026 · site.mockito.org
On the record
- Android
- The site says Mockito can be used on Android with Dexmaker.site.mockito.org · read 9 Oct 2026
- Android requirement
- The v5.23.0 release notes state that mockito-android tests require an Android P (API 28) or newer device or emulator.github.com · read 9 Oct 2026
- Argument matching
- Mockito supports flexible argument matching for verification and stubbing.github.com · read 9 Oct 2026
- Build tools
- The site gives Maven Central and Gradle dependency setup instructions and refers users to other build systems.site.mockito.org · read 9 Oct 2026
- Distribution
- The recommended installation method is adding the mockito-core dependency through a build system.site.mockito.org · read 9 Oct 2026
- Enterprise support
- The repository says commercial support and maintenance are available through a Tidelift subscription.github.com · read 9 Oct 2026
- Help
- The project points users to Stack Overflow, a moderated mailing list, and its GitHub issue tracker for help.github.com · read 9 Oct 2026
- Java requirement
- The repository says Mockito 5 requires Java 11.github.com · read 9 Oct 2026
- License
- The repository publishes Mockito under the MIT License.github.com · read 9 Oct 2026
- Mocking
- It can mock concrete classes as well as interfaces.github.com · read 9 Oct 2026
- Partial mocks
- Its spy feature invokes real methods while allowing them to be verified and stubbed.site.mockito.org · read 9 Oct 2026
- Purpose
- Mockito is a mocking framework for writing unit tests in Java.site.mockito.org · read 9 Oct 2026
- Security
- The repository links to a security policy.github.com · read 9 Oct 2026
- Spies and injection
- Its API includes spies for partial mocking and @InjectMocks for injecting mock or spy fields.site.mockito.org · read 9 Oct 2026
- Stubbing and verification
- Mockito lets developers stub method calls before execution and verify interactions afterward.site.mockito.org · read 9 Oct 2026
- Support
- The project directs users to its mailing list for questions and documentation feedback, and GitHub for bugs and feature requests.site.mockito.org · read 9 Oct 2026
- Training
- Mockito core engineers offer team training on test automation with mock objects, behavior-driven development, and maintainable tests.site.mockito.org · read 9 Oct 2026
- Use guidance
- The project advises against mocking types you do not own, value objects, or everything indiscriminately.site.mockito.org · read 9 Oct 2026
The review
Mockito is a Java mocking framework for writing unit tests, aimed at developers who need to isolate collaborators and check how code interacts with them. Its focused mock, stub, and verification tools make it a strong fit for Java projects, with some extra setup considerations for Android.
Overview
Mockito can mock interfaces and concrete classes, define behavior before a test runs, then verify selected interactions afterward. That combination suits tests where the behavior of a dependency matters more than running the dependency itself. The project’s advice to avoid mocking types you do not own, value objects, or everything indiscriminately is important: Mockito is most useful when mocks clarify a test, not when they replace meaningful behavior with a web of stand-ins.
Key features
Stubbing sets up how a mock should respond, while verification checks whether chosen interactions occurred. Flexible argument matching and checks for ordered calls or exact and minimum call counts give tests a way to express different interaction requirements without treating every invocation alike.
Spies provide partial mocking: real methods still run, but can also be stubbed and verified. That can help when a test needs to observe or selectively alter an existing object, though it is a narrower tool than replacing a collaborator with a mock. The @InjectMocks annotation supports injecting mock or spy fields.
The recommended distribution route is to add the mockito-core dependency through a build system. The project provides Maven Central and Gradle setup instructions and refers users of other build tools to their own systems. Commercial support and maintenance are available through a Tidelift subscription; Mockito core engineers also offer team training on test automation with mock objects, behavior-driven development, and maintainable tests.
Pricing
Mockito is free: its open-source plan costs free. There is no free trial because the core framework is already available at no charge. The repository identifies the project as MIT licensed. Commercial support and maintenance are offered through a Tidelift subscription, so teams seeking that service should consider it separately from the free framework.
Platforms
Mockito supports Java and is listed for Android, Linux, and self-hosted use. Mockito 5 requires Java 11. Android use relies on Dexmaker, and mockito-android tests require an Android P (API 28) or newer device or emulator. Those version requirements make it a less direct choice for older Android test environments.
Who it's for
Mockito is best suited to Java developers writing unit tests that need controlled collaborator behavior and explicit interaction checks. Android teams can use it with Dexmaker, provided their test device or emulator meets the Android P requirement. Developers who want to test real value-object behavior, or who do not own the types they are considering mocking, should follow the project’s cautions and avoid using mocks as a blanket substitute for real objects.
Pros and cons
- Pros: Mocks work with both interfaces and concrete classes, giving Java tests flexibility when isolating dependencies.
- Pros: Stubbing, flexible argument matching, call-count checks, and ordered verification support precise interaction-focused tests.
- Pros: The core framework is free and MIT licensed, with Maven and Gradle dependency instructions.
- Cons: Mockito 5 requires Java 11, which rules it out for projects that cannot meet that baseline.
- Cons: Android tests using mockito-android need Android P or newer, limiting use on older test devices and emulators.
- Cons: Mockito’s own guidance warns against indiscriminate mocking; teams that mock value objects or types they do not own risk tests that obscure rather than clarify behavior.
Alternatives
QUnit is a free JavaScript testing framework distributed through CDN, npm, and Yarn; choose it when the project is JavaScript rather than Java. Kotest is a free option whose test framework, assertions library, and property testing are standalone projects that can be used independently. NUnit is another free alternative; NUnit 3 and higher is MIT licensed for commercial and noncommercial use.
MockK is a free alternative for readers comparing mocking frameworks. Boost.Test is a free C++ unit testing library under the Boost Software License. ExUnit is a free unit testing framework for Elixir, while Hspec is a free Haskell testing framework with a source package on Hackage. Pest is a free open-source PHP testing framework; Pest 5 requires PHP 8.4 or newer.
For more options, browse Unit Testing Frameworks.
Verdict
Choose Mockito for Java unit tests when you need a free, focused way to stub collaborators and verify interactions. Its practical strengths are broad mock support and fine-grained verification; look elsewhere if your Java baseline is below 11, your Android tests cannot meet API 28, or your test needs are better served by real objects than mocks.