30

Dependency-breaking techniques for Java

Feathers' techniques adapted to Java to make hard-wired inherited code testable.

Demo 30Source on GitHub ↗

Michael Feathers’ dependency-breaking techniques, in Java. This module takes a hard-wired legacy shipment processor and applies four techniques to make it testable: Extract Interface plus Parameterize Constructor (a narrow collaborator a test can replace), Extract and Override Factory Method, Subclass and Override Method (to isolate LocalDate.now()), and Adapt Parameter (depend on an interface you own, not a concrete file type).

The JUnit 5 tests prove the now-testable behavior with Mockito mocks, verify, ArgumentCaptor, and real-file @TempDir cases.

Build, run, observe

cd examples/30-dependency-breaking
mvn -q test
./demo.sh

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