liveBook · Manning

Overspecification

As explained in Unit Testing Principles, Practices, and Patterns

A testing anti-pattern in which tests are coupled to implementation details rather than externally observable behavior.

Overspecification is a testing anti-pattern: tests become coupled to the system under test’s implementation details instead of verifying externally observable behavior. Prefer tests that describe what a unit does over tests that prescribe which classes, collaborators, or call sequences it must use. section 2.5

Use these distinctions to recognize and reduce overspecification:

  • Treat tests that verify units of code, mock interactions, or internal collaborator communication as warning signs when they replace behavioral verification. section 2.3 section 2.5
  • Remember that test doubles can hide a design problem rather than solve the inability to test a piece of code. section 2.5
  • Accept some failure cascading when it reveals that a widely depended-on piece of code has been broken; minimizing individual failure locations is not the primary goal. section 2.3

Behavior versus implementation

A behavior-focused test asks, “When I call my dog, does he come right to me?” An overspecified test checks the internal sequence instead: which leg moves first, when the head turns, and when the tail starts wagging. The first test protects observable behavior; the second couples the test to implementation details. section 2.3

When I call my dog, he comes right to me.

Use the concise dog-call statement as the behavioral specification. section 2.3

When I call my dog, he moves his front left leg first, then the front right
leg, his head turns, the tail start wagging...

Contrast the behavioral statement with a detailed internal sequence to spot overspecification. section 2.3

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines the coupling problem, contrasts behavior with code units, and identifies test doubles and mocks as related risks.
Examples
Contrasts a dog coming when called with a test that checks the dog’s exact internal movement sequence.
What you can do after
Evaluate tests by whether they protect observable behavior or prescribe replaceable implementation details.
How it connects
Connects overspecification to unit testing, test doubles, mocks, London testing, classical testing, and test-driven development.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.