Mocks vs. Stubs: When to Use Which

🌳 Evergreen Note Planted 27 April 2026
software-testing unit-tests test-doubles mocks stubs

Stubs and mocks serve fundamentally different purposes and should not be used interchangeably. Stubs handle inbound interactions: they control what the SUT receives. Mocks handle outbound interactions: they verify what the SUT sends. The common mistake is using mocks everywhere because frameworks make it easy, coupling tests to implementation details that break on any refactoring.

The rule: prefer stubs for queries and reserve mocks for commands. This aligns with testing behavior over implementation.

📝 Related Blog Posts