Adding Tests to Untestable Legacy Code
Techniques for getting a test harness around code that was never designed for testability.
Techniques for getting a test harness around code that was never designed for testability.
- File type
- Pages
- 32 pages
- File size
- 1.6 MB
You’ve inherited code with no tests: tightly coupled classes, inline dependencies, global state, 2,000-line methods. Every standard advice assumes testability was built in. The paradox is real—you can’t refactor safely without tests, but you can’t write tests without refactoring. A team inherited an OrderProcessor that created its own database connections, sent emails directly, called external APIs, and read from scattered configuration files. They couldn’t test it. Within two weeks using seam and characterization techniques, they had 80% of critical paths under test without changing production behavior. The “untestable” code became testable.
Focus on getting enough safety net to change the code you need to change, not comprehensive coverage of code that works fine.
This complete guide teaches you:
- Characterization tests: capturing observed behavior without specifications
- Behavior discovery: using test failures to document what code actually does
- The Seam Model: finding injection points in tightly coupled code
- Seam injection: link seams, object seams, and preprocessor seams
- Sprout Methods and Wrap Methods for surgical dependency isolation
- Testing through APIs versus testing internals
- Exposing hidden dependencies: detecting databases, files, and external calls
- Legacy code refactoring safety: incrementally increasing testability
- Deciding when to test versus when to replace
Download Your Legacy Code Testing Guide now to add safety nets around inherited code without major rewrites.
Adding Tests to Untestable Legacy Code
Fill out the form below to receive your pdf instantly.