Chapter 4. Concepts and patterns for TDD
This chapter broadens TDD beyond the “easy” examples and presents a practical toolbox for handling more realistic, complicated development situations. It emphasizes that selecting the next test is guided by heuristics rather than rigid rules: developers may choose details first or the big picture first, uncertain territory or familiar ground, high-value behavior or low-hanging fruit, and happy-path behavior or error handling depending on risk and payoff. To make tests pass, it highlights three progress strategies: faking a small piece of behavior, triangulating from multiple tests toward a general solution, and using an obvious implementation when the correct answer is already clear.
It then establishes core testing concepts that make TDD work well in practice. Fixtures are described as the full context in which tests run, including setup state and surrounding runtime state, and good fixtures reduce duplication while keeping tests focused. The chapter explains how test doubles help isolate difficult dependencies, and distinguishes state-based testing, which checks outcomes through object state, from interaction-based testing, which verifies that collaborators were called as expected. It also classifies test doubles as stubs, fakes, and mocks, showing how each supports different levels of realism and verification.
Finally, the chapter shifts from testing technique to design and legacy-code survival. It recommends design choices that improve testability, especially preferring composition over inheritance, avoiding static methods and Singleton-heavy designs, isolating dependencies behind seams, and injecting dependencies instead of hiding them inside code. It surveys useful unit-testing patterns for assertions, fixture construction, and general test structure, including parameterized tests, self-shunts, intimate inner classes, privileged access, extra constructors, and test-specific subclasses. The chapter closes by framing legacy code as code without tests and outlining a cautious process for working in it: analyze the change, identify change points and inflection points, add characterization tests, and then test-drive the desired change safely.
Triangulation is about narrowing down the solution space until it matches our exact needs.
Listing 4.1. Typical usage pattern for a test double
Listing 4.2. (Re)implementing OrderProcessorTest with dynamic mock
Inheriting from a superclass lets the subclass inherit all the functionality of the superclass. It does, however, also introduce inconvenient constraints related to instantiating the subclass in a test harness.
Composition allows for a more flexible design solution for reusing functionality from another class. For example, we can instantiate the composite object with alternative implementations of its components if they are difficult to obtain in a test harness.
Static method calls are difficult to fake because the target class for the call is hardcoded. Similarly, it is difficult to substitute a fake implementation for a Singleton class obtained through the usual (static...) getInstance() method call.
Listing 4.5. Code smell relieved: static method call wrapped into an instance method
Listing 4.6. Exploiting a seam
Listing 4.7. A getter dependency
Dependency Injection reverses the fulfillment of dependencies.
Listing 4.8. Dissolving tight coupling with dependency injection
Listing 4.12. Example of Interaction Assertions
Listing 4.13. Example of a Parameterized Creation Method
Registering fixture objects to a registry allows us to trigger a wholesale teardown easily for all registered objects during fixture teardown.
Listing 4.14. Example of the Parameterized Test pattern using JUnit 4
Listing 4.15. Example of the Self-Shunt pattern in action
Listing 4.16. Example of an Intimate Inner Class
Listing 4.17. Example of Extra Constructor exposing dependencies
The process of working with legacy code
Close-proximity inflection points typically cover the change point tightly, making it easy to test for the current behavior in detail.
More distant inflection points by definition cover more ground and, thus, may protect us from side effects of the change that our analysis had missed.
Test Driven ebook for free