Seams and Dependency Breaking
You'll learn how to identify "seams" in legacy code—natural points where you can inject testability without rewriting entire modules.
The Core Idea
A seam is a place in your code where you can change behavior without editing the code at that spot. Think of it like electrical outlets in your house: you don't rewire the walls every time you want a different appliance; you just plug something else in. Seams let you substitute dependencies (databases, APIs, file systems) with test doubles, making untestable legacy code testable.
The most common seam types are:
Object Seam
Where you can substitute one object for another through polymorphism or dependency injection. If a class instantiates its dependencies directly (new DatabaseConnection()), there's no seam. But if it accepts them as parameters, you've got a seam.
Preprocessing Seam
Using compiler directives or build configurations to swap implementations. Less elegant, but sometimes your only option in tightly-coupled code.
Link Seam
Replacing entire modules or libraries at link/compile time—useful when you can't touch the source.
Exploiting Seams
Once you've found a seam, you can break dependencies by:
- Passing dependencies as parameters instead of creating them internally
- Extracting interfaces from concrete classes
- Using subclasses to override problematic methods
- Applying the "Sprout" pattern—adding new testable code alongside old code rather than inside it
The beauty? You can make legacy code testable today without waiting for a massive rewrite. Each seam you exploit creates a foothold for further improvement.
Key Takeaway: Seams are natural injection points in legacy code where you can change behavior externally—exploit them to introduce tests without rewriting everything.