Die drei Gesetze von TDD
Verinnerlichen Sie Robert Martins drei Gesetze von TDD, die genau festlegen, wann Sie Produktions- und Testcode schreiben dürfen.
Die drei Gesetze von TDD ist eine kostenlose Testing Mastery: JUnit, Mockito & Integration Tests-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Testing Mastery: JUnit, Mockito & Integration Tests-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Testing Mastery: JUnit, Mockito & Integration Tests-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Die drei Gesetze von TDD“ kostenlos?
Ja — der vollständige Text von „Die drei Gesetze von TDD“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Testing Mastery: JUnit, Mockito & Integration Tests-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Testing Mastery: JUnit, Mockito & Integration Tests-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Die drei Gesetze von TDD“?
Verinnerlichen Sie Robert Martins drei Gesetze von TDD, die genau festlegen, wann Sie Produktions- und Testcode schreiben dürfen. Du übst Testing Mastery: JUnit, Mockito & Integration Tests mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Testing Mastery: JUnit, Mockito & Integration Tests zu starten?
Keine Vorkenntnisse erforderlich. Testing Mastery: JUnit, Mockito & Integration Tests auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Die drei Gesetze von TDD“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Testing Mastery: JUnit, Mockito & Integration Tests-Lektion Code schreiben und ausführen?
Ja. Jede Testing Mastery: JUnit, Mockito & Integration Tests-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einführung in den TDD-Zyklus
- Tests zuerst schreiben
- Refactoring für Testbarkeit
- Die drei Gesetze von TDD