Mocks vs. Stubs: When to Use Which
🌳 Evergreen ·
Stubs and mocks serve fundamentally different purposes and should not be used interchangeably. Stubs provide canned responses: they replace a dependency’s output so the test controls what the SUT sees. Mocks verify interactions: they assert the SUT communicated correctly with its collaborators. The common mistake is using mocks everywhere because frameworks make it easy, creating brittle tests coupled to implementation details that break on any refactoring.
Prefer stubs for queries (methods that return data) and reserve mocks for commands (methods that produce side effects you need to verify). This aligns with test-behavior-not-implementation.