Join our Newsletter — 33% off our NHI Course

Mockito

A Java mocking framework used to replace dependencies with controlled test doubles. It lets developers define expected method calls and return values, which isolates the unit under test and makes controller or service tests easier to validate deterministically.

What Mockito Is Used For

Mockito is a Java test double framework for isolating units of code from real dependencies. It helps developers replace collaborators with controlled behaviour so tests can verify interactions, return values, and error handling deterministically.

How Mockito Supports Unit Test Isolation

Mockito is most useful when a class depends on other services, repositories, clients, or utility objects that would make tests slow, flaky, or hard to predict. By substituting those dependencies with mocks or spies, the test focuses on the unit’s own logic instead of the behaviour of the wider system.

This isolation is especially valuable in service and controller tests, where external calls, database access, or complex setup can obscure the actual outcome being verified. A well-structured Mockito test makes the boundary between the unit under test and its collaborators explicit.

Common Mockito Patterns and Test Doubles

The framework is built around a few core patterns: stubbing, verification, and argument matching. Stubbing defines what a dependency should return or throw, verification checks that a method was called as expected, and matchers allow tests to express intent without overfitting to exact object instances.

Mockito also supports both mocks and spies. Mocks are fully controlled substitutes, while spies wrap a real object and override selected behaviour. That distinction matters because spies can preserve some production logic while still allowing specific interactions to be controlled in a test.

Mockito in Reliable Java Test Design

Mockito is not just a convenience layer, it is a test design tool that can shape how code is structured. If a class is difficult to mock cleanly, that often signals tight coupling, hidden side effects, or unclear boundaries in the design.

Used well, Mockito encourages smaller units, clearer dependencies, and tests that fail for the right reason. Used poorly, it can produce tests that mirror implementation details too closely, making refactors expensive and brittle.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS, OWASP SAMM and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP ASVS V15 — Secure Coding and Architecture Mockito supports testable code boundaries and isolated unit verification.
Recommendation — Use V15 to design code with clear seams that are easy to unit test with mocks.
OWASP SAMM Design — Security Architecture Mockito fits design practices that improve testability and component separation.
Recommendation — Assess whether unit seams and dependency boundaries support dependable automated testing.
NIST SP 800-53 Rev 5 SA-11 — Developer Testing and Evaluation Mockito is a testing tool used to support developer verification of software behaviour.
Recommendation — Apply SA-11 to require developer testing that exercises software logic under controlled conditions.

Practitioner Guidance

Why practitioners should care: Mockito helps teams keep unit tests fast and deterministic, but the quality of the test depends on whether the mock represents a meaningful dependency boundary. A test that only verifies internal chatter between objects can look precise while adding little confidence.

Common misunderstanding: Mocks are not a substitute for testing real integration behaviour. Mockito is strongest when it validates a unit’s logic at the boundary, while separate tests should cover wiring, persistence, HTTP, or other system interactions that a mock intentionally hides.

Practitioner takeaway: Use Mockito to isolate behaviour, not to recreate the whole system inside the test suite.