31

Characterization tests; Fowler vs Feathers

Pinning a gnarly legacy freight calculation with characterization tests, then refactoring safely.

Demo 31Source on GitHub ↗

When you do not understand inherited code, characterization tests lock in its current behavior so a later change that alters it is caught. This module pins a legacy freight-quote calculator (with three real boundary quirks: a truncating weight tier, a strict residential threshold, a truncating insurance surcharge) with an 8-row characterization table whose expected values were computed by actually running the legacy code. A Fowler-style refactor to a sealed carrier model then keeps every pinned case green.

The chapter contrasts Fowler’s Refactoring (clean code, net already present) with Feathers’ legacy-first discipline (build the net, then improve).

Build, run, observe

cd examples/31-characterization
mvn -q test
./demo.sh

The module also runs from the aggregator root with mvn -q -f examples/pom.xml test.