dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.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 mius(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 Main {
public static void main(String[] args) {
Calculator calculator = new Calculator(new KrwCalculator());
System.out.println(calculator.sum(10, 10));
System.out.println(calculator.mius(10, 5));
}
}
public class MarketApi {
public int connect() {
// naver
// kakao
return 1100;
}
}
public class DollorCalculator implements ICalculator{
private int price = 1;
private MarketApi marketApi;
public DollorCalculator(MarketApi marketApi) {
this.marketApi = marketApi;
}
public void init() {
this.price = marketApi.connect();
}
@Override
public int sum(int x, int y) {
return 0;
}
@Override
public int minus(int x, int y) {
return 0;
}
}
public class Main {
public static void main(String[] args) {
MarketApi marketApi = new MarketApi();
DollorCalculator dollorCalculator = new DollorCalculator(marketApi);
dollorCalculator.init();
Calculator calculator = new Calculator(dollorCalculator);
System.out.println(calculator.sum(10, 10));
}
}
import org.junit.jupiter.api.Test;
public class DollorCalculatorTest {
@Test
public void testHello() {
System.out.println("hello");
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DollorCalculatorTest {
@Test
public void testHello() {
System.out.println("hello");
}
@Test
public void dollorTest() {
MarketApi marketApi = new MarketApi();
DollorCalculator dollorCalculator = new DollorCalculator(marketApi);
dollorCalculator.init();
Calculator calculator = new Calculator(dollorCalculator);
System.out.println(calculator.sum(10, 10));
Assertions.assertEquals(22000, calculator.sum(10, 10));
}
}
public class DollorCalculator implements ICalculator{
private int price = 1;
private MarketApi marketApi;
public DollorCalculator(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;
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DollorCalculatorTest {
//@Test
public void testHello() {
System.out.println("hello");
}
@Test
public void dollorTest() {
MarketApi marketApi = new MarketApi();
DollorCalculator dollorCalculator = new DollorCalculator(marketApi);
dollorCalculator.init();
Calculator calculator = new Calculator(dollorCalculator);
Assertions.assertEquals(22000, calculator.sum(10, 10));
Assertions.assertEquals(0, calculator.mius(0, 0));
}
}
Mockito 처리
Mockito를 사용하기 위해서는 관련 라이브러리를 추가해 주어야 한다
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testImplementation group: 'org.mockito', name: 'mockito-core', version: '3.9.0'
// https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter
testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '3.9.0'
}
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 DollorCalculatorTest {
@Mock
public MarketApi marketApi;
@BeforeEach
public void init() {
// marketApi.connect()가 호출이 되었을 때 3000을 전달
Mockito.lenient().when(marketApi.connect()).thenReturn(3000);
}
@Test
public void dollorTest() {
MarketApi marketApi = new MarketApi();
DollorCalculator dollorCalculator = new DollorCalculator(marketApi);
dollorCalculator.init();
Calculator calculator = new Calculator(dollorCalculator);
Assertions.assertEquals(22000, calculator.sum(10, 10));
Assertions.assertEquals(0, calculator.mius(0, 0));
}
@Test
public void mockTest() {
DollorCalculator dollorCalculator = new DollorCalculator(marketApi);
dollorCalculator.init();
Calculator calculator = new Calculator(dollorCalculator);
Assertions.assertEquals(60000, calculator.sum(10, 10));
Assertions.assertEquals(0, calculator.mius(10, 10));
}
}