← New search

Other meanings of Mockito

Software

Mockito

Mockito is an open-source Java mocking framework used for unit testing. It simplifies the creation of mock objects, allowing developers to isolate the code under test by simulating dependencies. Mockito is widely adopted in the Java ecosystem, particularly with JUnit and TestNG.

MIT
License
License
5.14.2
Latest stable release
Version
1

Overview and core features

Mockito provides a fluent API for creating mocks, stubbing method calls, and verifying interactions. It supports annotations such as @Mock and @InjectMocks to reduce boilerplate code. The framework integrates seamlessly with JUnit via the MockitoExtension (JUnit 5) or MockitoJUnitRunner (JUnit 4). Since version 2, Mockito can mock final classes and methods using an inline mock maker, and from version 3.4 it supports mocking static methods.1 Its behavior-driven development (BDD) style API, e.g., then() and should(), aligns with BDD practices. Core features include stubbing with when().thenReturn(), verification of method calls, argument matchers, and the ArgumentCaptor for capturing arguments.

2

History and development

Mockito was created by Szczepan Faber in 2008 as a fork of EasyMock, aiming to provide a cleaner and more intuitive API.2 The name is a Spanish diminutive meaning "little mock." The project quickly gained traction in the Java community, becoming one of the most popular mocking frameworks. In 2010, Mockito was adopted by the Spring Framework for its test modules. Key milestones include the introduction of the inline mock maker in version 2.0 (2017) to mock final classes, and native support for mocking static methods in version 3.4.0 (2019). The framework is maintained by a team of volunteers under the MIT License, with its source code hosted on GitHub.3 As of 2025, Mockito remains actively developed, with a focus on performance and compatibility with modern Java versions.

3

Usage and integration

Mockito is commonly used with JUnit 5, where the @ExtendWith(MockitoExtension.class) annotation initializes mocks. For JUnit 4, the MockitoJUnitRunner serves the same purpose. The @Spy annotation allows partial mocking of real objects. Integration with Spring Boot is facilitated by the @MockBean annotation (from Spring Boot 1.4) and the MockitoTestExecutionListener. The framework also supports argument matchers like any() and eq(), and the InOrder verifier checks the order of method calls. Mockito can be used alongside PowerMock for mocking static and private methods, though Mockito's own static mocking reduces the need for PowerMock. The official documentation provides comprehensive examples for common testing scenarios.4

4

Lesser-known aspects and trivia

Mockito originally had a strict mode that was later refined into the MockSettings.strictness() API. The RETURNS_DEEP_STUBS answer allows chained calls without explicit stubbing. A notable edge case is that mocking lambdas can be buggy in some versions due to Java's lambda implementation. The project's name is a Spanish word; Szczepan Faber chose it because it sounded fun and was easy to pronounce. The initial design was influenced by the "mockito" pattern, which emphasizes simple, readable tests. The @Spy annotation was introduced in version 1.8. A lesser-known contribution is the MockitoJUnit.rule() for JUnit 4 rule-based initialization. The project's wiki contains detailed discussions of design decisions and historical context.5

Glossary

Mock object
A simulated object that mimics the behavior of a real object in a controlled way, used in unit testing.
Stubbing
Defining a mock's response to a specific method call, typically using when().thenReturn().
Verification
Confirming that a method on a mock was called (or not called) with specific arguments, using verify().
Spy
A partial mock that wraps a real object, allowing some methods to be stubbed while others run real logic.
Argument matcher
A flexible way to match method arguments, e.g., any(), eq(), or custom matchers.