0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · レッスン

TDDの3つの法則

本番コードとテストコードをいつ書いてよいかを正確に定める、Robert MartinのTDDの3つの法則を身につけます。

「TDDの3つの法則」はCoddyKit上の無料Testing Mastery: JUnit, Mockito & Integration Testsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはTesting Mastery: JUnit, Mockito & Integration Tests学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Testing Mastery: JUnit, Mockito & Integration Testsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「TDDの3つの法則」レッスンは無料ですか?

はい。「TDDの3つの法則」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Testing Mastery: JUnit, Mockito & Integration Testsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Testing Mastery: JUnit, Mockito & Integration Testsコースには全4レッスンが含まれています。

「TDDの3つの法則」で何を学びますか?

本番コードとテストコードをいつ書いてよいかを正確に定める、Robert MartinのTDDの3つの法則を身につけます。 ブラウザで直接実行するハンズオンコードでTesting Mastery: JUnit, Mockito & Integration Testsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Testing Mastery: JUnit, Mockito & Integration Testsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのTesting Mastery: JUnit, Mockito & Integration Testsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「TDDの3つの法則」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このTesting Mastery: JUnit, Mockito & Integration Testsレッスンでコードを書いて実行できますか?

はい。すべてのTesting Mastery: JUnit, Mockito & Integration Testsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. TDDサイクル入門
  2. 先にテストを書く
  3. テスト容易性のためのリファクタリング
  4. TDDの3つの法則
← Testing Mastery: JUnit, Mockito & Integration Testsに戻る