Testing Mastery: JUnit, Mockito & Integration Tests · Lezione

Le tre leggi del TDD

Interiorizzi le tre leggi del TDD di Robert Martin, che stabiliscono esattamente quando è possibile scrivere codice di produzione e codice di test.

Lezione 4 di 413 passaggi

Le tre leggi del TDD è una lezione Testing Mastery: JUnit, Mockito & Integration Tests gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Testing Mastery: JUnit, Mockito & Integration Tests, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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
Gratis per iniziare

Impara Testing Mastery: JUnit, Mockito & Integration Tests con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Le tre leggi del TDD» è gratuita?

Sì — il testo completo di «Le tre leggi del TDD» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Testing Mastery: JUnit, Mockito & Integration Tests, passa a CoddyKit PRO. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Cosa imparerò in «Le tre leggi del TDD»?

Interiorizzi le tre leggi del TDD di Robert Martin, che stabiliscono esattamente quando è possibile scrivere codice di produzione e codice di test. Eserciti Testing Mastery: JUnit, Mockito & Integration Tests con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Testing Mastery: JUnit, Mockito & Integration Tests?

Non è richiesta alcuna esperienza precedente. Testing Mastery: JUnit, Mockito & Integration Tests su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Le tre leggi del TDD»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Testing Mastery: JUnit, Mockito & Integration Tests?

Sì. Ogni lezione Testing Mastery: JUnit, Mockito & Integration Tests include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Introduzione al ciclo TDD
  2. Scrivere prima i test
  3. Refactoring per la testabilità
  4. Le tre leggi del TDD
← Torna a Testing Mastery: JUnit, Mockito & Integration Tests