티스토리 뷰

개발 방식 연구/TDD

TDD [3]

kkoon9 2022. 1. 25. 23:13

이제 앞에서 만들었던 요구사항을 만족하는 테스트 코드를 작성해보자.

Inventory 만들기

  • 가로, 세로, 높이
  • 냉장, 냉동, 상온(타입)
  • 총 저장 수량
  • 현재 수량

할일목록

  • Inventory의 크기를 알 수 있어야 한다.
  • Inventory의 타입을 알 수 있어야 한다.
  • 총 저장 수량을 알 수 있어야 한다.
  • 현재 저장된 수량을 알 수 있어야 한다.

첫 번째 InventoryTests

TODO

  • Inventory의 크기를 알 수 있어야 한다.
  • Inventory의 타입을 알 수 있어야 한다.
  • 총 저장 수량을 알 수 있어야 한다.

DONE

package com.kkoon9.inventoryTDD;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class InventoryTests {
    
    @Test
    void testInventory_size_test() {
        Inventory inventory = new Inventory();
        
        inventory.setLength(10);
        inventory.setWidth(10);
        inventory.setHeight(10);

        Assertions.assertEquals(10, inventory.getLength());
        Assertions.assertEquals(10, inventory.getWidth());
        Assertions.assertEquals(10, inventory.getHeight());
    }
}

위 코드는 컴파일 에러가 난다.

Inventory 클래스를 만들어주지 않아서 에러가 난다.

src/main/inventoryTDD에 Inventory 클래스를 추가해주자.

package com.kkoon9.inventoryTDD;

import java.math.BigDecimal;

public class Inventory {
    private BigDecimal length;
    private BigDecimal width;
    private BigDecimal height;

    public Inventory() {
    }

    public Inventory(BigDecimal length, BigDecimal width, BigDecimal height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    public BigDecimal getLength() {
        return length;
    }

    public void setLength(BigDecimal length) {
        this.length = length;
    }

    public BigDecimal getWidth() {
        return width;
    }

    public void setWidth(BigDecimal width) {
        this.width = width;
    }

    public BigDecimal getHeight() {
        return height;
    }

    public void setHeight(BigDecimal height) {
        this.height = height;
    }

}

가로, 세로, 높이는 소수점이 가능할 수 있으므로 BigDecimal 타입으로 선언해주었다.

테스트 코드도 수정해주었다.

package com.kkoon9.inventoryTDD;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

public class InventoryTests {

    @Test
    void testInventory_size_test() {
        Inventory inventory = new Inventory();

        inventory.setLength(new BigDecimal(10));
        inventory.setWidth(new BigDecimal(10));
        inventory.setHeight(new BigDecimal(10));

        Assertions.assertEquals(new BigDecimal(10), inventory.getLength());
        Assertions.assertEquals(new BigDecimal(10), inventory.getWidth());
        Assertions.assertEquals(new BigDecimal(10), inventory.getHeight());
    }
}

TODO

  • Inventory의 타입을 알 수 있어야 한다.
  • 총 저장 수량을 알 수 있어야 한다.
  • 현재 저장된 수량을 알 수 있어야 한다.

DONE

  • Inventory의 크기를 알 수 있어야 한다.

두 번째 InventoryTests

TODO

  • Inventory의 타입을 알 수 있어야 한다.
  • 총 저장 수량을 알 수 있어야 한다.
  • 현재 저장된 수량을 알 수 있어야 한다.

DONE

  • Inventory의 크기를 알 수 있어야 한다.

타입도 추가해보자.

위와 같이 테스트 코드를 먼저 작성해보자.

package com.kkoon9.inventoryTDD;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

public class InventoryTests {
    @Test
    void testInventory_type_test() {
        Inventory inventory = new Inventory();

        inventory.setType(AMBIENT);

        Assertions.assertEquals(AMBIENT, inventory.getType());
    }
}

역시 오류가 나므로 Inventory 클래스를 수정해주자.

package com.kkoon9.inventoryTDD;

import java.math.BigDecimal;

public class Inventory {
    private String type;

    public Inventory(BigDecimal length, BigDecimal width, BigDecimal height, String type) {
        this.length = length;
        this.width = width;
        this.height = height;
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

이로써 두 번째 요구사항도 해결하였다.

TODO

  • 총 저장 수량을 알 수 있어야 한다.
  • 현재 저장된 수량을 알 수 있어야 한다.

DONE

  • Inventory의 크기를 알 수 있어야 한다.
  • Inventory의 타입을 알 수 있어야 한다.

근데 요구사항에 타입은 3개밖에 없으므로 Enum으로 관리하는 게 더 용이할 것 같다.

이렇게 테스트 코드를 작성했을 때 다른 게 떠올랐다면 위 TODO에 추가해주고 작업을 진행하자.

TODO

  • 총 저장 수량을 알 수 있어야 한다.
  • 현재 저장된 수량을 알 수 있어야 한다.
  • Inventory Type을 enum 타입으로 관리하자.

DONE

  • Inventory의 크기를 알 수 있어야 한다.
  • Inventory의 타입을 알 수 있어야 한다.

enum으로 관리하기

먼저, 테스트 코드 먼저 변경해보자.

package com.kkoon9.inventoryTDD;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

public class InventoryTests {
    @Test
    void testInventory_type_test() {
        Inventory inventory = new Inventory();

        inventory.setType(EnumType.Temperature.AMBIENT);

        Assertions.assertEquals(EnumType.Temperature.AMBIENT, inventory.getType());
    }
}

EnumType 클래스를 만들자.

package com.kkoon9.inventoryTDD;

public class EnumType {
    public enum Temperature {
        AMBIENT	    ("상온"),
        FROZEN	    ("냉동"),
        FRIDGE      ("냉장");

        private final String value;
        Temperature(String value) { this.value = value; }
    }
}

이제 Inventory에 type을 Temperature enum을 가지게 코드를 변경해보자.

package com.kkoon9.inventoryTDD;

import java.math.BigDecimal;

public class Inventory {
    private EnumType.Temperature type;

    public Inventory() {
    }

    public Inventory(BigDecimal length, BigDecimal width, BigDecimal height, EnumType.Temperature  type) {
        this.length = length;
        this.width = width;
        this.height = height;
        this.type = type;
    }

    public EnumType.Temperature getType() {
        return type;
    }

    public void setType(EnumType.Temperature type) {
        this.type = type;
    }
}

이제 테스트를 실행해보면 통과하는 것을 알 수 있다.

세 번째 InventoryTests

TODO

  • 총 저장 수량을 알 수 있어야 한다.
  • 현재 저장된 수량을 알 수 있어야 한다.

DONE

  • Inventory의 크기를 알 수 있어야 한다.
  • Inventory의 타입을 알 수 있어야 한다.
  • Inventory Type을 enum 타입으로 관리하자.

테스트 코드를 살펴보자.

package com.kkoon9.inventoryTDD;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

public class InventoryTests {
    @Test
    void testInventory_capacity_test() {
        Inventory inventory = new Inventory();

        inventory.setCapacity(10);

        Assertions.assertEquals(10, inventory.getCapacity());
    }

    @Test
    void testInventory_current_count_test() {
        Inventory inventory = new Inventory();

        inventory.setCurrent(10);

        Assertions.assertEquals(10, inventory.getCurrent());
    }
}

위 컴파일 에러를 해결하기 위해 Inventory 클래스를 수정해보자.

package com.kkoon9.inventoryTDD;

import java.math.BigDecimal;

public class Inventory {
    private int capacity;
    private int current;

		public Inventory(BigDecimal length, BigDecimal width, BigDecimal height, EnumType.Temperature type, int capacity, int current) {
        this.length = length;
        this.width = width;
        this.height = height;
        this.type = type;
        this.capacity = capacity;
        this.current = current;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }
}

현재 저장된 수량이 총 저장 수량을 넘으면 안 된다.

위 요구사항도 추가 후 테스트 코드를 작성해보자.

TODO

  • 현재 저장된 수량이 총 저장 수량을 넘으면 안 된다.

DONE

  • Inventory의 크기를 알 수 있어야 한다.
  • Inventory의 타입을 알 수 있어야 한다.
  • Inventory Type을 enum 타입으로 관리하자.
  • 총 저장 수량을 알 수 있어야 한다.
  • 현재 저장된 수량을 알 수 있어야 한다.
package com.kkoon9.inventoryTDD;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

public class InventoryTests {
		@Test
    void testInventory_unable_current_count_test() {
        Inventory inventory = new Inventory();
        inventory.setCapacity(5);
        inventory.setCurrent(10);

        Assertions.assertEquals(10, inventory.isAbleCurrentCount());
    }
}

Inventory 클래스를 수정해보자.

package com.kkoon9.inventoryTDD;

import java.math.BigDecimal;

public class Inventory {
		boolean isAbleCurrentCount() {
        return this.getCapacity() >= this.getCurrent();
    }
}

다음과 같은 순서로 TDD를 진행하게 된다.

  1. 요구사항 도출
  2. 테스트 코드 작성
  3. 테스트 코드에 사용될 코드(ex Inventory) 작성
  4. 테스트 통과 시 리팩토링 후 1~3번 반복

하지만 계속해서 이런식으로 개발하는 건 사실상 불가능하다.

그래서 어느 적정수준까지는 타협을 하면서 개발하는 것이 좋다.

'개발 방식 연구 > TDD' 카테고리의 다른 글

TDD 관련 글 정리  (0) 2022.02.13
AssertJ  (0) 2022.01.29
AssertJ vs. JUnit  (0) 2022.01.26
TDD [2]  (0) 2022.01.25
TDD [1]  (0) 2022.01.25
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함