Mockito is a mocking framework for unit tests written in Java. It provides a small API to create mocks, stub behavior, verify interactions, and support partial spies and injection.
Overview
Mockito helps Java developers isolate units under test by replacing collaborators with mock objects. You create mocks for interfaces or concrete classes, define return values or behavior with stubbing, and verify that expected methods were called with specific arguments.
Core APIs include mock creation with optional settings, when or given stubbing, verify for interaction checks, and spy for partial mocking where real methods still run. Flexible argument matchers, captors, and BDD-style syntax through BDDMockito extend how assertions are expressed. Annotations such as Mock, Spy, and InjectMocks automate wiring in tests.
The framework emphasizes readable tests and clear verification errors when expectations fail. It integrates with common Java build tools through the mockito-core artifact published to Maven Central, and supports Android testing with related dexmaker work.
Documentation is largely maintained in Javadoc, with a reference card and project site for examples. The maintainers caution against mocking types you do not own, mocking value objects, or mocking every dependency.
Key features
- Mock interfaces and concrete classes
- Stub and verify interactions
- Partial mocking with spies
- InjectMocks field injection
- Flexible argument matchers
- BDDMockito syntax
- Android support via dexmaker
- Annotation-based test setup
Use cases
- Java unit test isolation
- Behavior verification in TDD
- Testing without real databases or services
- Android component unit tests
- Refactoring-friendly mock APIs