Video in progress
How does TDD affect design, and in which cases does strict TDD do harm instead of good?
Because the test is written first, the class is designed from the caller's point of view: dependencies are explicit, the API is small, side effects sit at the boundary. But in exploratory code where requirements are unclear, framework "glue" and fast-changing prototypes, strict TDD makes you spend time on tests that get thrown away.
📖 Answer
The main benefit of TDD isn't the number of tests, it's the feedback. If a class is hard to test (9 dependencies in the constructor, a gateway created with new, LocalDate.now()), you notice before you write the code. A test written after the code adapts to the existing design, and that signal is lost.
There are two styles, and they produce different designs:
- Outside-in (London): first a test at the controller or use case level, with the collaborators mocked. Good for "discovering" interfaces, but if many mocks stay, the tests stick to the implementation.
- Inside-out (Detroit): first the domain objects, with real collaborators. The tests are sturdier, but you see the external API later.
In practice I write domain logic inside-out and the adapters at the boundary with integration tests.
Where TDD does harm:
- A spike. You're trying out a library or an approach, and the code gets deleted tomorrow. A test here is just slowness. The rule: throw the spike away, then rewrite it with TDD.
- Framework configuration. For a
SecurityFilterChainor Kafka settings, a "failing unit test first" checks almost nothing; an integration test is useful here, and writing it afterward is fine. - Unclear requirements. If the business doesn't know what it wants, you rewrite dozens of tests on every change. Prototype first with short feedback, then write tests for the part that has stabilized.
The place where TDD is never excessive is bug fixing: first a failing test that reproduces the bug, then the fix. That test protects against the regression forever and proves the fix really works.
Rather than forcing TDD on a team, I demand the outcome: in the PR, behavior is covered by tests, and you can show the test failing first. Who wrote them in which order is secondary.
Sign up to mark studied questions and save your progress.