TDD(Test-driven Development)
단위테스트
JUnit
프로젝트 구조
빌드 도구 설정
build.gradle 파일 설정
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.9.0'
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.9.0'
}
test {
useJUnitPlatform()
}
public interface ICalculator {
int sum(int x, int y);
int minus(int x, int y);
}
public class Calculator {
private ICalculator iCalculator; // 외부에서 주입받음
public Calculator(ICalculator iCalculator) {
this.iCalculator = iCalculator;
}
public int sum(int x, int y) {
return this.iCalculator.sum(x,y);
}
public int minus(int x, int y) {
return this.iCalculator.minus(x,y);
}
}
public class KrwCalculator implements ICalculator{
private int price = 1;
@Override
public int sum(int x, int y) {
x *= price;
y *= price;
return x+y;
}
@Override
public int minus(int x, int y) {
x *= price;
y *= price;
return x-y;
}
}
public class DollarCalculator implements ICalculator{
private int price = 1;
private MarketApi marketApi;
public DollarCalculator(MarketApi marketApi) {
this.marketApi = marketApi;
}
public void init() {
this.price = marketApi.connect();
}
@Override
public int sum(int x, int y) {
x *= price;
y *= price;
return x+y;
}
@Override
public int minus(int x, int y) {
x *= price;
y *= price;
return x-y;
}
}
public class MarketApi {
public int connect() {
return 1100; // 환율 리턴
}
}
일반 테스트 코드 작성
Mocking
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class DollarCalculatorTest {
@Mock
public MarketApi marketApi;
@BeforeEach
public void init() {
System.out.println("--- init() ---");
Mockito.lenient().when(marketApi.connect()).thenReturn(3000);
}
@Test
public void testHello() {
System.out.println("--- testHello() ---");
System.out.println("hello");
System.out.println();
}
@Test
public void test() {
System.out.println("--- test() ---");
MarketApi marketApi = new MarketApi();
DollarCalculator dollarCalculator = new DollarCalculator(marketApi);
dollarCalculator.init();
Calculator calculator = new Calculator(dollarCalculator);
Assertions.assertEquals(22000, calculator.sum(10,10));
System.out.println(calculator.sum(10, 10));
System.out.println();
}
@Test
public void mockTest() {
System.out.println("--- mockTest() ---");
DollarCalculator dollarCalculator = new DollarCalculator(marketApi);
dollarCalculator.init();
Calculator calculator = new Calculator(dollarCalculator);
Assertions.assertEquals(60000, calculator.sum(10,10));
System.out.println(calculator.sum(10, 10));
System.out.println();
}
}
실행 결과
정리
@Test | 테스트 코드 |
Assertions.assertEquals(기대값, 테스트값); | 테스트 |
@ExtendWith(MockitoExtension.class) | Mocking 준비 |
@Mock | Mocking 설정 |
@BeforeEach | 테스트 실행되기 전에 실행 |