Guide · Open source
How to use Mockito: set up mocks for Java unit tests
Updated · written with AI from the project's own documentation and checked against our catalogue data
Mockito is an open-source Java library that creates mocks, stand-in objects that replace the parts of a program your tests depend on. This guide explains what Mockito does, what you need before installing it, and how to get it from Maven Central. It is written for Java developers who are new to mocking and want to test one class without a real database or network service.
Quick start
- 1Confirm your Java version. Make sure you run Java 11 or newer, which the README requires for Mockito 5.
- 2Add the dependency. Add org.mockito:mockito-core to your Gradle or Maven build, using the current version listed on Maven Central.
- 3Create a mock. Use Mockito in your test to create a stand-in object for a dependency your class uses.
- 4Run your tests. Run the tests with your build tool so the mocked dependency replaces the real one.
- 5Read the docs when stuck. Use the javadoc at javadoc.io or ask on Stack Overflow with the mockito tag.
What Mockito does
Mockito lets you create mocks, which are stand-in objects for the dependencies a piece of code relies on. A test can then check one class on its own, without a real database or network service behind it.
The README describes Mockito as the most popular mocking framework for Java.
Check the requirements before you install
Mockito 5 requires Java 11 or newer, according to the README. Mockito 4 and earlier do not have this requirement, but the README notes that only one major version is supported at a time and changes are not backported to older versions.
You also need a build tool such as Gradle or Maven to fetch the library from Maven Central.
- Java 11 or newer for Mockito 5
- A build tool such as Gradle or Maven
- Maven Central artifact: org.mockito:mockito-core
Add Mockito to your project from Maven Central
Mockito is published to Maven Central under the coordinates org.mockito:mockito-core. Add that dependency through your build tool. The README does not show a full build file, so check the Maven Central page for the current version to use.
The README also shows how to clone the repository, which is useful if you want to read or build the source yourself.
- Group ID: org.mockito
- Artifact ID: mockito-core
git clone https://github.com/mockito/mockito.gitBuild Mockito locally (optional)
To build the project from source, the README gives a Gradle command. You can open the project in any IDE that supports Gradle, such as IntelliJ IDEA or Fleet. For Eclipse, the README says you may need to run a Gradle task first.
- Open in any Gradle-compatible IDE, such as IntelliJ IDEA or Fleet
- For Eclipse, you may need to run the eclipse Gradle task before importing
./gradlew buildGet help and read the docs
The full documentation is on The Mockito Site and in the javadoc for org.mockito.Mockito on javadoc.io. Javadoc is available 24 hours after each release, according to the README.
For questions, the README points to Stack Overflow under the mockito tag, a moderated mailing list, and the GitHub issue tracker.
- Stack Overflow, tag: mockito
- Mailing list (moderated)
- GitHub issue tracker
What people use it for
- Testing a service class that normally calls a database, by replacing the database dependency with a mock
- Testing a class that calls a network service without making real network calls during the test
- Learning test-driven development in Java with a widely documented mocking library
Frequently asked questions
Is Mockito free to use?
Yes. The README lists the license as MIT, and the LICENSE file is in the repository. Paid commercial support is available only through a Tidelift subscription, which is separate from the library.
What Java version does Mockito need?
Mockito 5 requires Java 11 or newer, according to the README. Older Java versions need an older Mockito release, and the README says only one major version is supported at a time.
How do I install Mockito?
Mockito is hosted on Maven Central as org.mockito:mockito-core. Add that dependency to your Gradle or Maven build file and your build tool will download it.
Is Mockito only for Java?
The README describes Mockito as a Java library and does not cover other languages.
What are the alternatives to Mockito?
Comparable Java mocking projects include EasyMock, JMock, and PowerMock. This guide does not compare their features, so check each project's documentation before choosing.
Where can I get help if Mockito is not working?
Search or ask on Stack Overflow using the mockito tag, post to the moderated mailing list, or open a ticket in the GitHub issue tracker.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Mockito suits Java developers who need to unit test classes with external dependencies and want a widely used, well-documented, MIT-licensed library. It is a poor fit for non-Java projects or for teams stuck on Java 8 or older, and teams that need paid maintainer support should look at the Tidelift option or another tool.
Install options & downloads