Comparison
Hard-coded dependencyvsSeam
Hard-coded dependency
the class constructs its own collaborator inside a method, so testing it means standing up the real thing.
A class that names and constructs a concrete collaborator itself, welding the two together at compile time. It is the thing dependency injection is contrasted against, and the practical symptom is always the same: a unit test that needs a database, a clock or the network. Not every `new` is a problem — constructing a value object or a data structure is fine; constructing something with behaviour, I/O or a lifetime is the case to watch.
Full entry →Seam
you needed to test the payment path and there was nowhere to slip a fake in without editing the code you were testing.
A place where you can change behaviour without editing the code at that place — a parameter, an interface, a configuration point. Seams are what make legacy code testable, because they are where a real dependency can be replaced with a fake. Finding or creating a seam is usually the first move when changing code nobody dares touch, and the absence of seams is exactly what makes such code frightening.
Full entry →