The Three Laws of TDD
Internalize Robert Martin's three laws of TDD that govern exactly when you may write production and test code.
The Three Laws of TDD is a free Testing Mastery: JUnit, Mockito & Integration Tests lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Testing Mastery: JUnit, Mockito & Integration Tests learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Discipline Behind the Cycle
The red-green-refactor cycle becomes precise through three short rules known as the Three Laws of TDD, formulated by Robert C. Martin. They constrain exactly when you write code.
The First Law
Law 1: You may not write production code until you have written a failing unit test.
No code is added speculatively. Every line exists to satisfy a test.
The Second Law
Law 2: You may not write more of a unit test than is sufficient to fail. A compilation failure counts as failing.
The Third Law
Law 3: You may not write more production code than is sufficient to pass the currently failing test.
A Tiny Failing Test
Following Law 2, write only enough test to fail. Here the class does not even exist yet, so it fails to compile.
@Test
void addsTwoNumbers() {
assertEquals(5, new Calc().add(2, 3));
}Just Enough Production Code
By Law 3 you write the minimum to pass, even if it looks naive at first.
class Calc {
int add(int a, int b) { return a + b; }
}Tight Loop
The three laws lock you into a loop measured in seconds, not minutes: write a bit of failing test, write a bit of code, repeat.
Why So Strict?
The strictness guarantees every line of production code is covered by a test and keeps you from over-engineering features no test demands.
Avoiding Speculative Design
Because you only write code that a test requires, you naturally avoid YAGNI violations (You Aren't Gonna Need It) and dead code.
Practicing the Laws
The laws feel restrictive at first. With practice the loop becomes muscle memory, producing comprehensive test coverage almost as a side effect.
Relationship to Red-Green-Refactor
The laws drive the red and green phases; refactoring still happens once tests pass, with the safety net the laws built.
Quick Check
According to the first law, when may you write production code?
Recap
You learned the disciplined core of TDD:
- Law 1: no production code without a failing test
- Law 2: write only enough test to fail
- Law 3: write only enough code to pass
- The laws create a tight loop and full coverage
Frequently asked questions
Is the “The Three Laws of TDD” lesson free?
Yes — the full text of “The Three Laws of TDD” is free to read here on the web, and the Testing Mastery: JUnit, Mockito & Integration Tests course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Testing Mastery: JUnit, Mockito & Integration Tests course, upgrade to CoddyKit PRO.
What will I learn in “The Three Laws of TDD”?
Internalize Robert Martin's three laws of TDD that govern exactly when you may write production and test code. You practise Testing Mastery: JUnit, Mockito & Integration Tests with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Testing Mastery: JUnit, Mockito & Integration Tests?
No prior experience is required. Testing Mastery: JUnit, Mockito & Integration Tests on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Three Laws of TDD” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Testing Mastery: JUnit, Mockito & Integration Tests lesson?
Yes. Every Testing Mastery: JUnit, Mockito & Integration Tests lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to TDD Cycle
- Writing Tests First
- Refactoring for Testability
- The Three Laws of TDD