Post

TDD, Display Name, BDD

βœ… TDD

  • red
  • blue
  • green

πŸ”΄

  • first create the test code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//test code
    @DisplayName("Calculate the total price of beverages in order")
    @Test
    void getTotalPrice() {
        //given
        CafeKiosk cafeKiosk = new CafeKiosk();
        Americano americano = new Americano();
        Latte latte = new Latte();
        cafeKiosk.add(americano);
        cafeKiosk.add(latte);

        //when
        int toalPrice = cafeKiosk.calculateTotalPrice();
        //then
        assertThat(toalPrice).isEqualTo(9000);
    }
  • the production code will be set to minimum
  • this test will fail, as in production code, it returns 0
1
2
3
4
//production code
public int calculateTotalPrice() {
        return 0;
    }

πŸ”΅

  • in fast, minimum time,
  • create the minimum production code to make the code pass
1
2
3
4
//production code
public int calculateTotalPrice() {
        return 9000;
    }

🟒

  • create the production code that works
  • while keeping the test code without modification
1
2
3
4
5
6
7
8
//production code
    public int calculateTotalPrice() {
        int total = 0;
        for (Beverage beverage : beverages) {
            total += beverage.getPrice();
        }
        return total;
    }
  • and refactor
1
2
3
4
5
    public int calculateTotalPrice() {
        return beverages.stream()
                .mapToInt(Beverage::getPrice)
                .sum();
    }

βœ… DisplayName

  • use @DisplayName annotation to describe the method
  • Test to... ❌
  • write in sentences ⭕️
  • also write the results added to Orders
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    @Test
    @DisplayName("Add one beverage. Beverage is added to Orders")
    void add() {
        CafeKiosk cafeKiosk = new CafeKiosk();
        cafeKiosk.add(new Americano());
        assertThat(cafeKiosk.getBeverages().size()).isEqualTo(1);
        assertThat(cafeKiosk.getBeverages()).hasSize(1);
        assertThat(cafeKiosk.getBeverages().get(0).getName()).isEqualTo("Americano");
    }

    @Test
    @DisplayName("Add everal beverage to Orders")
    void addSeveral() {
        CafeKiosk cafeKiosk = new CafeKiosk();
        Americano americano = new Americano();
        cafeKiosk.add(americano, 10);

        assertThat(cafeKiosk.getBeverages()).hasSize(10);
        assertThat(cafeKiosk.getBeverages().get(0).getName()).isEqualTo("Americano");
        assertThat(cafeKiosk.getBeverages().get(0)).isEqualTo(americano);
        assertThat(cafeKiosk.getBeverages().get(1)).isEqualTo(americano);
    }
1
2
3
4
5
6
7
8
9
10
11
    @Test
    @DisplayName("Cannot create order before CAFE_OPEN_TIME and after CAFE_CLOSE_TIME")
    void createOrderOutsideCurrentTime() {
        CafeKiosk cafeKiosk = new CafeKiosk();
        Americano americano = new Americano();
        cafeKiosk.add(americano);

        assertThatThrownBy(()->cafeKiosk.createOrder(LocalDateTime.of(2026, 2, 16, 9, 59)))
                .isInstanceOf(IllegalStateException.class)
                .hasMessage("This is not order time. Inquire to manager");
    }

βœ… BDD

Behavior Driven Development

  • not focus on method
  • but focus on scenario, Test Case(TC)

  • Given: context, situation before the action happens, μ–΄λ–€ ν™˜κ²½μ—μ„œ
  • When: the action μ–΄λ–€ 행동을 μ§„ν–‰ν–ˆμ„ λ•Œ
  • Then: check outcome of action μ–΄λ–€ μƒνƒœ λ³€ν™”κ°€ μΌμ–΄λ‚œλ‹€

  • TDD πŸ†š BDD: BDD is based on TDD, focuses more on scenarios
This post is licensed under CC BY 4.0 by the author.