Skip to content

Commit 98b0f3f

Browse files
committed
refactor. 문자열 계산기 테스트 코드 JUnit -> AssertJ로 리팩토링
1 parent 65e7e2c commit 98b0f3f

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import domain.StringCalculator;
2-
import org.junit.jupiter.api.Assertions;
32
import org.junit.jupiter.api.Test;
43
import org.junit.jupiter.params.ParameterizedTest;
54
import org.junit.jupiter.params.provider.CsvSource;
@@ -8,30 +7,32 @@
87

98
import java.util.List;
109

11-
import static org.junit.jupiter.api.Assertions.assertEquals;
12-
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
import static org.assertj.core.api.Assertions.*;
1311

1412
public class StringCalculatorTest {
1513
private final StringCalculator stringCalculator = new StringCalculator();
1614

1715
@ParameterizedTest
18-
@ValueSource(strings = {"1,2,3:4", "1 , 2: 3,4 "})
16+
@ValueSource(strings = {
17+
"1,2,3:4",
18+
"1 , 2: 3,4 "
19+
})
1920
public void 기본_구분자_문자열_계산기_테스트(String str) {
2021
int actual = stringCalculator.calculate(str);
2122

22-
assertEquals(10, actual);
23+
assertThat(actual).isEqualTo(10);
2324
}
2425

2526
@ParameterizedTest
2627
@CsvSource({
27-
" '//+\n', '+' ",
28-
" '// \n', ' ' ",
29-
" '//||\n', '||' "
28+
"'//+\n', '+'",
29+
"'// \n', ' '",
30+
"'//||\n', '||'"
3031
})
3132
public void 커스텀_구분자_파싱_테스트(String str, String expected) {
3233
String customDelimeter = stringCalculator.findCustomDelimeter(str);
3334

34-
assertEquals(expected, customDelimeter);
35+
assertThat(customDelimeter).isEqualTo(expected);
3536
}
3637

3738
@Test
@@ -40,21 +41,25 @@ public class StringCalculatorTest {
4041

4142
List<Integer> actual = stringCalculator.parseNumber(tokens);
4243

43-
assertEquals(List.of(1, 2, 3, 4), actual);
44+
assertThat(actual).containsExactly(1, 2, 3, 4);
4445
}
4546

4647
@Test
4748
public void 문자열_리스트_정수_리스트로_변환_음수가_포함되면_예외_발생() {
4849
String[] tokens = {"1", "2", "-3", "4"};
4950

50-
assertThrows(RuntimeException.class, () -> stringCalculator.parseNumber(tokens));
51+
assertThatThrownBy(() -> stringCalculator.parseNumber(tokens))
52+
.isInstanceOf(RuntimeException.class)
53+
.hasMessageContaining("음수는 처리할 수 없습니다.");
5154
}
5255

5356
@Test
5457
public void 문자열_리스트_정수_리스트로_변환_문자가_포함되면_예외_발생() {
5558
String[] tokens = {"a1", "b", " ", "4"};
5659

57-
assertThrows(RuntimeException.class, () -> stringCalculator.parseNumber(tokens));
60+
assertThatThrownBy(() -> stringCalculator.parseNumber(tokens))
61+
.isInstanceOf(RuntimeException.class)
62+
.hasMessageContaining("문자열은 처리할 수 없습니다.");
5863
}
5964

6065
@ParameterizedTest
@@ -66,7 +71,7 @@ public class StringCalculatorTest {
6671
public void 커스텀_구분자_문자열_계산기_테스트(String str) {
6772
int actual = stringCalculator.calculate(str);
6873

69-
assertEquals(10, actual);
74+
assertThat(actual).isEqualTo(10);
7075
}
7176

7277
@ParameterizedTest
@@ -76,7 +81,9 @@ public class StringCalculatorTest {
7681
"//\n1;2;3;4"
7782
})
7883
public void 커스텀_구분자_형식에_맞지_않은_경우_예외_발생(String str) {
79-
assertThrows(RuntimeException.class, () -> stringCalculator.calculate(str));
84+
assertThatThrownBy(() -> stringCalculator.calculate(str))
85+
.isInstanceOf(RuntimeException.class)
86+
.hasMessageContaining("커스텀 구분자를 찾을 수 없습니다.");
8087
}
8188

8289
@ParameterizedTest
@@ -86,14 +93,16 @@ public class StringCalculatorTest {
8693
" "
8794
})
8895
public void 문자열이_비어있는_경우_예외_발생(String str) {
89-
assertThrows(RuntimeException.class, () -> stringCalculator.calculate(str));
96+
assertThatThrownBy(() -> stringCalculator.calculate(str))
97+
.isInstanceOf(RuntimeException.class)
98+
.hasMessageContaining("문자열이 비어있습니다.");
9099
}
91100

92101
@Test
93102
public void 구분자_없이_숫자만_입력한_경우() {
94103
String str = "1";
95104
int actual = stringCalculator.calculate(str);
96105

97-
assertEquals(1, actual);
106+
assertThat(actual).isEqualTo(1);
98107
}
99108
}

0 commit comments

Comments
 (0)