JUnit 없이 직접 단위 테스트 작성
- 테스트의 기본을 확인하기 위해 단위 테스트를 직접 작성하는 경우를 생각해보자.
- 어색하지만 로직이 이렇다는 것을 확인해둔다.
- given, when, then 표기로 테스트 데이터 및 조건, 동작 시점 등을 구분해서 작성해주면 좋다.
public class StampCalculator {
public static int calculateStampCount(int nowCount, int earned) {
return nowCount + earned;
}
}
public class StampCalculatorTestWithoutJUnit {
public static void main(String[] args) {
calculateTest();
}
private static void calculateTest() {
//given
int nowCount = 5;
int earned = 3;
//when
int actual = StampCalculator.calculateStampCount(nowCount, earned);
int expected = 7;
System.out.println(actual == expected);
}
}
JUnit으로 테스트 작성하기
- Spring initializr로 스프링부트 프로젝트를 생성하면 JUnit을 포함한 테스트 프레임워크 의존성이 추가되어 있으므로, 바로 작성하여 사용할 수 있다.
- src/test/java 하위에 작성하면 된다.
package com.codestates;
import java.util.HashMap;
import java.util.Map;
public class CryptoCurrency {
public static Map<String, String> map = new HashMap<>();
static {
map.put("BTC", "Bitcoin");
map.put("ETH", "Ethereum");
map.put("ADA", "ADA");
map.put("POT", "Polkadot");
}
}