Overview

Chapter 2. Beginning TDD

The chapter introduces test-driven development as a practical way to steer software design by writing tests first, then adding only enough code to make them pass, and finally refactoring with confidence. Using the metaphor of airport signs, it explains that TDD provides clear guidance when building software, replacing guesswork with a disciplined cycle that reduces defects, improves design, and keeps development moving in small, manageable steps.

To illustrate this, the chapter walks through building a simple template engine that replaces placeholders in text with runtime values. It emphasizes translating broad requirements into specific, atomic, isolated tests, since tests describe concrete behavior far better than abstract tasks. The process begins with a single failing test, then a minimal implementation, then additional tests that force out hard-coded shortcuts through techniques like triangulation and programming by intention. Along the way, the narrative shows how to decide between breadth-first and depth-first development, depending on whether the next obstacle is better handled at the public interface or in lower-level parsing details.

The chapter also highlights the importance of refactoring, not only in production code but in tests as well. As the template engine grows, the test suite is cleaned up by removing redundant cases and consolidating shared setup, while the implementation is improved by extracting smaller methods and keeping abstraction levels balanced. Error handling is added through tests for missing values and meaningful exception messages, and the chapter closes by noting two remaining challenges: handling variable values that resemble placeholders and considering performance through a simple timing test. Overall, it presents TDD as a way to make steady progress while preserving clarity, correctness, and design quality.

The TDD cycle is a three-step process of writing a test, making it pass by writing just enough production code, and finally improving the design by disciplined refactorings.
Our first failing test. What a precious moment. What we see here is the Eclipse IDE’s JUnit runner. But all major IDEs have something similar to a red bar, some kind of hierarchical list of tests run, and access to the detailed error—the stack trace for failed tests.
Listing 2.6. Passing as quickly as possible with a hard-coded return statement
Listing 2.7. Forcing out the hard-coded return statement with another test
Listing 2.9. Applying triangulation for the static template text
With a breadth-first strategy, we implement the higher-level functionality first by faking the required lower-level functionality.
With a depth-first strategy, we implement the lower-level functionality first and only compose the higher-level functionality once all the ingredients are present.
Listing 2.11. Search-and-replace for handling multiple variables
Listing 2.13. Test code after removing a redundant test and unifying the fixture
Listing 2.15. Checking for remaining variables after the search-and-replace
Listing 2.16. Extracting the check for missing variables into a method
Listing 2.17. Extracting another method from evaluate()
Listing 2.18. Testing for an expected exception

FAQ

What is the main idea of Test-Driven Development (TDD) in this chapter?TDD is presented as a cycle of test-code-refactor: write a failing test first, add just enough production code to make it pass, and then refactor the code while keeping the tests green.
Why does the chapter compare TDD to airport signs?Airport signs guide travelers step by step toward the right gate, and TDD does something similar for developers by giving clear, small, reliable steps that guide implementation toward the desired solution.
Why is decomposing requirements into tests better than decomposing them into tasks?Tests map directly to expected system behavior and show when the requirement is satisfied, while tasks only describe work to be done and can lose sight of the actual goal.
What makes a good test according to this chapter?A good test is atomic and isolated. It should verify one small piece of behavior and not depend on other tests or shared side effects.
What does “programming by intention” mean?It means writing test code as if the ideal production code already exists, so you design the API from the test’s point of view and shape the implementation to be easy to use.
What was the first test used for the template engine example?The first test checked that a template like "Hello, ${name}" with name = "Reader" evaluates to "Hello, Reader".
Why does the chapter recommend seeing a test fail before making it pass?Seeing the failure confirms that the test is actually being executed and that the expected behavior does not yet exist, which helps avoid false confidence from accidental green tests.
What is triangulation in TDD?Triangulation is the practice of adding another test with different input to force a hard-coded or overly narrow implementation to generalize toward the correct solution.
What is the difference between breadth-first and depth-first TDD?Breadth-first means implementing higher-level behavior first and faking lower-level details. Depth-first means implementing lower-level functionality first, then composing the higher-level feature afterward.
Why does the chapter emphasize refactoring even when tests are passing?Passing tests give safety to improve design. Refactoring removes duplication, simplifies methods, and keeps the code maintainable without breaking existing behavior.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free