Over-Specification with Behavior Verification
What you'll learn: You'll discover how verifying too many implementation details in tests makes them fragile and harder to maintain.
The Problem with Too Much Verification
When using behavior verification (testing interactions like method calls), it's tempting to verify every interaction your code performs. This creates over-specified tests that break whenever you change how something works internally, even when the external behavior remains correct.
Think of it like having a surveillance camera in your kitchen watching every motion you make while cooking. If you normally grab a spoon from the left drawer but one day use the right drawer, the camera flags it as wrong—even though you still made the same delicious meal. The test cares about implementation details it shouldn't.
What Over-Specification Looks Like
Over-specified tests verify:
- The exact order of method calls
- Every single method invocation, not just critical ones
- Internal collaborations that could change
- How many times something was called when you only care that it happened
These tests become brittle—they break for the wrong reasons. You refactor code to improve it, all external behavior stays identical, but dozens of tests fail because they were checking how the code worked internally.
The Right Level of Verification
Focus behavior verification on:
- Critical interactions that define your contract (like "did we save to the database?")
- Boundary calls to external systems
- Interactions that represent important business decisions
Don't verify internal implementation details that could reasonably change during refactoring.
Key Takeaway: Verify the interactions that matter to your contract, not every method call your implementation happens to make—over-specification couples tests to implementation details, making refactoring painful and discouraging code improvements.