Skip to content

Commit 6bd4ab1

Browse files
committed
feat(level2): 메인 메서드 없이 계산기 테스트 자동화
1 parent f713b7a commit 6bd4ab1

File tree

3 files changed

+126
-4
lines changed

3 files changed

+126
-4
lines changed

src/main/java/Calculator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
public class Calculator {
2-
int add(int a, int b) {
2+
public int add(int a, int b) {
33
return a + b;
44
}
55

6-
int subtract(int a, int b) {
6+
public int subtract(int a, int b) {
77
return a - b;
88
}
99

10-
int multiply(int a, int b) {
10+
public int multiply(int a, int b) {
1111
return a * b;
1212
}
1313

14-
int divide(int a, int b) {
14+
public int divide(int a, int b) {
1515
if (b == 0) {
1616
throw new ArithmeticException("0으로 나눌 수 없습니다.");
1717
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import org.junit.jupiter.api.DisplayName;
2+
import org.junit.jupiter.api.Test;
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
@DisplayName("Calculator Assert Test")
7+
public class CalculatorAssertTest {
8+
9+
private Calculator calculator = new Calculator();
10+
11+
@Test
12+
@DisplayName("add")
13+
void add_test() {
14+
int a = 1;
15+
int b = 2;
16+
int expected = a + b;
17+
18+
int actual = calculator.add(1, 2);
19+
20+
assertEquals(expected, actual);
21+
}
22+
23+
@Test
24+
@DisplayName("subtract")
25+
void subtract_test() {
26+
int a = 1;
27+
int b = 2;
28+
int expected = 1 - 2;
29+
30+
int actual = calculator.subtract(a, b);
31+
32+
assertEquals(expected, actual);
33+
}
34+
35+
@Test
36+
@DisplayName("multiply")
37+
void multiply_test() {
38+
int a = 1;
39+
int b = 2;
40+
int expected = a * b;
41+
42+
int actual = calculator.multiply(a, b);
43+
44+
assertEquals(expected, actual);
45+
}
46+
47+
@Test
48+
@DisplayName("divide")
49+
void divide_test() {
50+
int a = 1;
51+
int b = 2;
52+
int expected = a / b;
53+
54+
int actual = calculator.divide(a, b);
55+
56+
assertEquals(expected, actual);
57+
}
58+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import org.junit.jupiter.api.DisplayName;
2+
import org.junit.jupiter.api.Test;
3+
4+
@DisplayName("Calculator junit5 Test")
5+
public class CalculatorJUnit5Test {
6+
7+
private Calculator calculator = new Calculator();
8+
9+
@Test
10+
@DisplayName("add")
11+
void add_test() {
12+
int a = 1;
13+
int b = 2;
14+
int expected = a + b;
15+
16+
int actual = calculator.add(1, 2);
17+
18+
if (actual != expected) {
19+
throw new RuntimeException("expected: <" + expected + "> but was: <" + actual + ">");
20+
}
21+
}
22+
23+
@Test
24+
@DisplayName("subtract")
25+
void subtract_test() {
26+
int a = 1;
27+
int b = 2;
28+
int expected = 1 - 2;
29+
30+
int actual = calculator.subtract(a, b);
31+
32+
if (actual != expected) {
33+
throw new RuntimeException("expected: <" + expected + "> but was: <" + actual + ">");
34+
}
35+
}
36+
37+
@Test
38+
@DisplayName("multiply")
39+
void multiply_test() {
40+
int a = 1;
41+
int b = 2;
42+
int expected = a * b;
43+
44+
int actual = calculator.multiply(a, b);
45+
46+
if (actual != expected) {
47+
throw new RuntimeException("expected: <" + expected + "> but was: <" + actual + ">");
48+
}
49+
}
50+
51+
@Test
52+
@DisplayName("divide")
53+
void divide_test() {
54+
int a = 1;
55+
int b = 2;
56+
int expected = a / b;
57+
58+
int actual = calculator.divide(a, b);
59+
60+
if (actual != expected) {
61+
throw new RuntimeException("expected: <" + expected + "> but was: <" + actual + ">");
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)