---
# System prepended metadata

title: TDD 練習

---

# Example : Simple Calculator

**Description:** Develop a basic calculator that can add and subtract numbers.

**Steps:**

1. Write a failing test case to verify that the calculator can correctly add two numbers.
2. Implement the minimal code required to make the test pass.
3. Refactor the code if necessary.
5. Write a failing test case to verify that the calculator can correctly subtract two numbers.
6. Implement the code to make the test pass.
7. Refactor the code if necessary.

This example introduces the basic concepts of TDD by starting with simple arithmetic operations and gradually building up the calculator's functionality.

```java
// CalculatorTest.java
import org.junit.Assert;
import org.junit.Test;

public class CalculatorTest {

    @Test
    public void testAdditionSuccessfully() {
        Calculator calculator = new Calculator();
        int result = calculator.add(2, 3);
        Assert.assertEquals(5, result);
    }

    @Test
    public void testSubtractionSuccessfully() {
        Calculator calculator = new Calculator();
        int result = calculator.subtract(5, 3);
        Assert.assertEquals(2, result);
    }
}
```
```java
// Calculator.java
public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }
}
```



# Example: Wizard Duel Tournament

**Description:** Develop a wizard duel tournament simulator where multiple wizards compete against each other in magical battles.

**Steps:**

1. Write a failing test case to verify that a wizard has a name, health, and a set of spells.
2. Implement the minimal code required to make the test pass.
3. Write a failing test case to verify that a wizard can learn new spells.
4. Implement the code to make the test pass.
5. Write a failing test case to verify that a wizard can cast a spell on another wizard.
6. Implement the code to make the test pass.
7. Write a failing test case to verify that a wizard's health decreases when they are hit by a spell.
8. Implement the code to make the test pass.
9. Write a failing test case to simulate a wizard duel tournament where multiple wizards compete against each other.
10. Implement the code to make the test pass.
11. Write additional test cases to handle edge cases (e.g., leveling up, facing different opponents) and repeat the process.
12. Continuously refactor the code, improve test coverage, and ensure that all tests pass.

```java
// WizardDuelTournamentTest  .java
import org.junit.Assert;
import org.junit.Test;

public class WizardDuelTournamentTest {

    @Test
    public void testWizardNameHealthSpells() {
        // 測試一個新的巫師 instance 被成功建立。
        // 驗證標準：確保巫師的名字和生命值被正確地設定，並且他開始時沒有任何法術。
        // Testing the initialization of a new wizard object.
        // Criteria：Ensuring the wizard's name and health are set correctly and he starts with no spells.

        Wizard wizard = new Wizard("Gandalf", 100);
        Assert.assertEquals("Gandalf", wizard.getName());
        Assert.assertEquals(100, wizard.getHealth());
        Assert.assertTrue(wizard.getSpells().isEmpty());
    }

    @Test
    public void testWizardLearnSpell() {
        // 測試巫師學習新法術。
        // 驗證標準：確保法術被正確地添加到巫師的法術列表中。
        // Testing the functionality of a wizard learning a new spell.
        // Criteria：Ensuring the spell is correctly added to the wizard's spell list.

        Wizard wizard = new Wizard("Gandalf", 100);
        Spell spell = new Spell("Fireball", 30);

        wizard.learnSpell(spell);

        Assert.assertEquals(1, wizard.getSpells().size());
        Assert.assertTrue(wizard.getSpells().contains(spell));
    }

    @Test
    public void testWizardCastSpell() {
        // 測試一名巫師對另一名巫師施放法術的功能。
        // 驗證標準：確保目標巫師的健康值正確地減少了。
        // Testing the functionality of a wizard casting a spell on another wizard.
        // Criteria：Ensuring the target wizard's health is decreased correctly.

        Wizard attacker = new Wizard("Gandalf", 100);
        Wizard target = new Wizard("Saruman", 100);
        Spell spell = new Spell("Fireball", 30);

        attacker.learnSpell(spell);
        attacker.castSpell(target, spell);

        Assert.assertEquals(70, target.getHealth());
    }

    @Test
    public void testWizardHealthDecreaseAfterBeingHit() {
        // 測試當巫師被法術擊中時，他的健康值應該如何減少。
        // 確保巫師的健康值正確地減少了。
        // Testing how a wizard's health should decrease when hit by a spell.
        // Ensuring the wizard's health is decreased correctly.

        Wizard wizard = new Wizard("Gandalf", 100);
        Spell spell = new Spell("Fireball", 30);

        wizard.receiveSpell(spell);

        Assert.assertEquals(70, wizard.getHealth());
    }

    @Test
    public void testWizardDuelTournament() {
        // 測試一場巫師之間的對決。
        // 驗證標準：確保每個巫師施放的法術都正確地影響了對方的健康值。
        // Testing a duel between wizards.
        // Criteria：Ensuring each wizard's casted spell correctly affects the other's health.

        Wizard gandalf = new Wizard("Gandalf", 100);
        Wizard saruman = new Wizard("Saruman", 100);
        Spell fireball = new Spell("Fireball", 30);
        Spell lightningBolt = new Spell("Lightning Bolt", 40);

        gandalf.learnSpell(fireball);
        saruman.learnSpell(lightningBolt);

        gandalf.castSpell(saruman, fireball);
        saruman.castSpell(gandalf, lightningBolt);

        Assert.assertEquals(70, gandalf.getHealth());
        Assert.assertEquals(60, saruman.getHealth());
    }
}

```
```java
// Wizard .java
import java.util.ArrayList;
import java.util.List;

public class Wizard {
    private String name;
    private int health;
    private List<Spell> spells;

    public Wizard(String name, int health) {
        this.name = name;
        this.health = health;
        this.spells = new ArrayList<>();
    }

    public String getName() {
        return name;
    }

    public int getHealth() {
        return health;
    }

    public List<Spell> getSpells() {
        return spells;
    }

    public void learnSpell(Spell spell) {
        spells.add(spell);
    }

    public void castSpell(Wizard target, Spell spell) {
        if (spells.contains(spell)) 
            target.receiveSpell(spell);
    }

    public void receiveSpell(Spell spell) {
        health -= spell.getDamage();
}
```
```java
// Spell .java
public class Spell {
    private String name;
    private int damage;

    public Spell(String name, int damage) {
        this.name = name;
        this.damage = damage;
    }

    public String getName() {
        return name;
    }

    public int getDamage() {
        return damage;
    }
}
```
