Mockito
Write clean, maintainable unit tests with minimal boilerplate with Java
Mockito is the most popular Java mocking framework, trusted by over 15,100 developers on GitHub to write clean, maintainable unit tests with minimal boilerplate. It provides a simple, fluent API that lets you create mocks, spies, and stubs in just a few lines of code, resulting in highly readable tests and clear verification errors.
With first-class support for behavior‑driven development (BDDMockito), inline mock makers, and strict stubs, integrates effortlessly with JUnit, TestNG, Android (via DexMaker), and other testing ecosystems, ensuring broad compatibility across JVM-based projects.
Key Features:
- Clean & Fluent API: Create mocks with
mock()
, spies withspy()
, and define behavior usingwhen()
/given()
methods for straightforward test setups. - Expressive Verification - Use
verify()
with flexible argument matchers (e.g.,any()
,eq()
) or captors (@Captor
) to assert interactions precisely. - Behavior‑Driven Development (BDD) Support: Leverage BDD-style syntax via
BDDMockito.then()
andgiven()
for readable, narrative test definitions. - Inline Mocking & ByteBuddy Integration: Switchable mock‑maker plugins let you mock final classes and methods without classpath hacking, powered by ByteBuddy under the hood.
- Annotation‑Driven Injection: Auto‑inject mocks and spies into your classes using
@Mock
,@Spy
, and@InjectMocks
, streamlining test initialization. - Strict Stubs & Error Reporting: Enable strict stubbing to catch unused or misconfigured stubs, reducing false‑positive test results and improving test quality.
- Android Compatibility: Use Mockito on Android projects via the DexMaker engine, ensuring consistent behavior across JVM and Dalvik runtimes.
- Comprehensive Documentation & Community: Access up‑to‑date Javadoc on javadoc.io, search the GitHub Wiki for FAQs and best practices, or join the mailing list and StackOverflow tags for support.
By adopting Mockito, Java teams can write robust, self‑documenting unit tests that drive confidence in code changes, accelerate development cycles, and foster a culture of TDD and clean design.