0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · Pelajaran

Tiga Hukum TDD

Pahami tiga hukum TDD Robert Martin yang secara tepat menentukan kapan Anda boleh menulis kode produksi dan kode pengujian.

Tiga Hukum TDD adalah pelajaran Testing Mastery: JUnit, Mockito & Integration Tests gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Testing Mastery: JUnit, Mockito & Integration Tests, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Testing Mastery: JUnit, Mockito & Integration Tests mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Tiga Hukum TDD” gratis?

Ya — teks lengkap “Tiga Hukum TDD” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Testing Mastery: JUnit, Mockito & Integration Tests, upgrade ke CoddyKit PRO. Kursus Testing Mastery: JUnit, Mockito & Integration Tests mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Tiga Hukum TDD”?

Pahami tiga hukum TDD Robert Martin yang secara tepat menentukan kapan Anda boleh menulis kode produksi dan kode pengujian. Kamu berlatih Testing Mastery: JUnit, Mockito & Integration Tests dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Testing Mastery: JUnit, Mockito & Integration Tests?

Tidak diperlukan pengalaman sebelumnya. Testing Mastery: JUnit, Mockito & Integration Tests di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Tiga Hukum TDD” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Testing Mastery: JUnit, Mockito & Integration Tests ini?

Ya. Setiap pelajaran Testing Mastery: JUnit, Mockito & Integration Tests menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Pengantar Siklus TDD
  2. Menulis Pengujian Terlebih Dahulu
  3. Pemfaktoran Ulang untuk Kemudahan Pengujian
  4. Tiga Hukum TDD
← Kembali ke Testing Mastery: JUnit, Mockito & Integration Tests