Mockito is a Java mocking framework that creates test doubles for the collaborators of the class you are testing. Its significance is that it lets you test one unit in isolation, control what its dependencies return, and verify how they were called, without databases, networks or hand-written fake classes. It is the de facto standard mocking library in the Java ecosystem and the one that Spring Boot's test starter bundles.
This article covers the core API with JUnit 5, current Mockito 5 behaviour and the situations where mocking is the wrong tool.
A unit test should fail only when the unit under test is wrong. If the class calls a repository, an HTTP client or a clock, a failure in those would break the test for unrelated reasons. Mockito replaces them with objects you configure:
Add mockito-core and mockito-junit-jupiter (Spring Boot's spring-boot-starter-test already includes both). Use the extension instead of calling MockitoAnnotations.openMocks yourself.
MockitoExtension uses strict stubs by default: an unused stubbing fails the test with UnnecessaryStubbingException, which keeps tests honest. Prefer constructor injection in production code so @InjectMocks has a clear target.
When you use a matcher for one argument, every argument in that call must be a matcher; wrap raw values with eq(...). Use ArgumentCaptor when you need to assert on a complex argument:
For methods that return void, stub with doThrow(...).when(mock).method() or doNothing().
A spy wraps a real instance. Use doReturn(...).when(spy).method() so the real method is not invoked while stubbing.
Spies are a sign that a class does too much. Consider splitting the class before reaching for one.
Mockito.mockStatic, inside a try-with-resources block, and constructors with mockConstruction. Use these rarely; they are a design smell more often than a solution.MockitoExtension; the old JUnit 4 runner is only for legacy code.In a plain unit test, use @Mock. In a Spring ApplicationContext test, use Spring's bean override annotations so the mock is injected into the container. @MockBean was deprecated in Spring Boot 3.4 and removed in 4.0; use @MockitoBean and @MockitoSpyBean from Spring Framework 6.2 and later.
| Situation | Better choice |
|---|---|
| Verifying SQL, mappings and transactions | Real database via Testcontainers |
| Value objects and pure functions | Plain assertions, no mocks |
| HTTP serialization and retries | Local stub server such as WireMock |
| Many interactions between collaborators | Hand-written fake with real behaviour |
Over-mocking couples tests to implementation details, so refactoring breaks tests that should still pass.
A mock is fully fake: every method returns defaults unless stubbed. A spy wraps a real object, so un-stubbed methods run the real code.
Your test configured a stub that was never used. Remove it, or if the stub is shared setup, mark it lenient().
Yes, since the inline mock maker became the default in Mockito 5. Older versions needed mockito-inline.
Use Mockito for simple interaction checks and fakes when the collaborator has meaningful behaviour that several tests share.
Mockito isolates a unit from its dependencies through stubbing, verification, captors and spies. Use MockitoExtension with JUnit 5, rely on strict stubs, and keep mocking for boundaries you do not own. In Spring context tests, use @MockitoBean rather than the removed @MockBean.