Blog
Mockito Is Not A Testing Strategy
Mockito is useful library. A mock-heavy testing culture is not useful.
Once developers learn to fake every collaborator, tests stop checking behavior and start checking choreography. Then harmless refactors break the suite while database and transaction bugs walk through front door.
The Mocking Addiction
Bad unit test:
verify(repository).save(order);
verify(eventPublisher).publish(any(OrderCreated.class));
Maybe order is invalid. Maybe transaction rolls back. Maybe database constraint fails. Test does not know. It only watches actors touch props.
This is fragile and shallow.
What I Want Instead
For real persistence and messaging boundaries, run real dependency locally in test.
Testcontainers makes this practical.
@Testcontainers
class PaymentRepositoryTest {
@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
@DynamicPropertySource
static void datasource(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}
@Test
void enforcesIdempotencyKeyUniqueness() {
repository.insert("key-1", new BigDecimal("10.00"));
assertThatThrownBy(() -> repository.insert("key-1", new BigDecimal("10.00")))
.isInstanceOf(DuplicateKeyException.class);
}
}
Now test checks real database behavior. This is more valuable than fifty fake verifications.
Unit Tests Still Exist
Pure domain logic should be unit-tested fast without Spring, database, or Docker.
assertThat(policy.canWithdraw(balance, amount)).isTrue();
No mock needed.
When Mocks Are Fine
Mock external paid API. Mock time. Mock slow unreliable partner. Mock boundary you do not own.
But if you mock your own repository, your own mapper, your own service, your own validator, what remains? A puppet show.
Refactor Resistance
Good tests survive internal refactor. Mock-heavy tests often fail because method call changed, not behavior.
This makes developers afraid to improve design. Test suite becomes cage.
A Better Boundary
Keep Mockito for a boundary that is slow, costly, nondeterministic, or outside your control. Do not use it to erase the component whose behavior the test claims to verify.
Mocks are scalpel, not wallpaper. For a serious backend, a real database in persistence tests is ordinary engineering, not luxury.