From 9f99cb5b6af371b4646730451d1cd2f3c8e00c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Tue, 5 Mar 2024 20:19:45 +0900 Subject: [PATCH 1/8] split study test --- build.gradle | 2 +- src/test/java/.gitkeep | 0 src/test/java/study/StringTest.java | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) delete mode 100644 src/test/java/.gitkeep create mode 100644 src/test/java/study/StringTest.java diff --git a/build.gradle b/build.gradle index 6282254c4ce..a48e7963fb7 100644 --- a/build.gradle +++ b/build.gradle @@ -15,7 +15,7 @@ dependencies { java { toolchain { - languageVersion = JavaLanguageVersion.of(11) + languageVersion = JavaLanguageVersion.of(15) } } diff --git a/src/test/java/.gitkeep b/src/test/java/.gitkeep deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java new file mode 100644 index 00000000000..c8fdbcb215e --- /dev/null +++ b/src/test/java/study/StringTest.java @@ -0,0 +1,14 @@ +package study; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StringTest { + + @Test + void split() { + String[] result = "1,2".split(","); + assertThat(result).contains("1"); + } +} From f945e7860415554fe62702dca6c93bf57bfbf723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Tue, 5 Mar 2024 20:47:06 +0900 Subject: [PATCH 2/8] =?UTF-8?q?[1-2]=20substring=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/StringTest.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index c8fdbcb215e..63fc271b9e8 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,5 +1,6 @@ package study; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -7,8 +8,22 @@ public class StringTest { @Test - void split() { + void split1() { String[] result = "1,2".split(","); assertThat(result).contains("1"); + assertThat(result).containsExactly("1", "2"); } + + @Test + void split2() { + String[] result = "1".split(","); + assertThat(result).containsExactly("1"); + } + + @Test + void subString() { + String substring = "(1,2)".substring(1, 4); + assertThat(substring).isEqualTo("1,2"); + } + } From 47cee810d7d465073135048b6614ef4fe0fe6717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Tue, 5 Mar 2024 20:58:34 +0900 Subject: [PATCH 3/8] =?UTF-8?q?[1-3]=20charAt=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/StringTest.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 63fc271b9e8..f0511fafd3a 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertThrows; public class StringTest { @@ -25,5 +27,20 @@ void subString() { String substring = "(1,2)".substring(1, 4); assertThat(substring).isEqualTo("1,2"); } - + + @Test + @DisplayName("String의 charAt() 메소드를 활용해 특정 위치의 문자를 가져오는 Test") + void charAt1() { + char charAt = "abc".charAt(0); + assertThat(charAt).isEqualTo("a"); + } + + @Test + @DisplayName("String의 charAt() 메소드를 활용해 특정 위치의 문자를 가져올 때 위치 값 벗어났을 때 Exception Test") + void charAt2() { + int charLength = "abc".length(); + assertThrows(IndexOutOfBoundsException.class, () -> { + "abc".charAt(charLength+1); + }); + } } From 7eed379e0bf8bb4f7bda2bf7501496a010dade00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Tue, 5 Mar 2024 21:08:07 +0900 Subject: [PATCH 4/8] =?UTF-8?q?[2-1]=20size=20=EB=A9=94=EC=86=8C=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/java/study/SetTest.java diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java new file mode 100644 index 00000000000..abcdfe73ceb --- /dev/null +++ b/src/test/java/study/SetTest.java @@ -0,0 +1,31 @@ +package study; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SetTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @Test + @DisplayName("Set의 size()를 활용해 Set의 크기를 확인하는 Test") + void size() { + int setSize = numbers.size(); + assertThat(setSize).isEqualTo(3); + } + +} From c3b10f50a70a62d6ab4fd44850d7c76327b7e61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Tue, 5 Mar 2024 21:16:57 +0900 Subject: [PATCH 5/8] =?UTF-8?q?[2-2]=20contains=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index abcdfe73ceb..00e86945a2a 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -3,6 +3,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.util.HashSet; import java.util.Set; @@ -28,4 +30,10 @@ void size() { assertThat(setSize).isEqualTo(3); } + @DisplayName("Set의 contains()를 이용해 값이 존재하는지 확인하는 Test") + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + void contains(int input) { + assertThat(numbers.contains(input)).isTrue(); + } } From e5f0ba385dcf40af2acbd344226d107876d4cee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Tue, 5 Mar 2024 21:19:23 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[2-1]=20assertJ=EC=9D=98=20hasSize=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index 00e86945a2a..8701a558c33 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -27,7 +27,7 @@ void setUp() { @DisplayName("Set의 size()를 활용해 Set의 크기를 확인하는 Test") void size() { int setSize = numbers.size(); - assertThat(setSize).isEqualTo(3); + assertThat(numbers).hasSize(setSize); } @DisplayName("Set의 contains()를 이용해 값이 존재하는지 확인하는 Test") From f45ec5b812c20a8022603978c568d5bc6fda2f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Tue, 5 Mar 2024 21:25:18 +0900 Subject: [PATCH 7/8] [2-3] csvSource annotation --- src/test/java/study/SetTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index 8701a558c33..7709b33ceb7 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import java.util.HashSet; @@ -36,4 +37,12 @@ void size() { void contains(int input) { assertThat(numbers.contains(input)).isTrue(); } + + @DisplayName("JUnit CsvSource를 이용한 Test") + @ParameterizedTest + @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') + void csvSource(int input, boolean expected) { + assertThat(numbers.contains(input)).isEqualTo(expected); + } + } From 577063b7a974724594ed876dd18a28df1309f2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=8F=99=EC=98=A5?= Date: Wed, 6 Mar 2024 18:48:13 +0900 Subject: [PATCH 8/8] =?UTF-8?q?refactor:=201=EB=8B=A8=EA=B3=84=20=ED=95=99?= =?UTF-8?q?=EC=8A=B5=ED=85=8C=EC=8A=A4=ED=8A=B8=20=ED=94=BC=EB=93=9C?= =?UTF-8?q?=EB=B0=B1=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 12 +++++++++--- src/test/java/study/StringTest.java | 29 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index 7709b33ceb7..dd175d306c1 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -38,11 +38,17 @@ void contains(int input) { assertThat(numbers.contains(input)).isTrue(); } - @DisplayName("JUnit CsvSource를 이용한 Test") + @DisplayName("JUnit CsvSource를 이용한 Contain(true) Test") @ParameterizedTest - @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') - void csvSource(int input, boolean expected) { + @CsvSource(value = {"1:true", "2:true", "3:true"}, delimiter = ':') + void csvSource1(int input, boolean expected) { assertThat(numbers.contains(input)).isEqualTo(expected); } + @DisplayName("JUnit CsvSource를 이용한 Contain(false) Test") + @ParameterizedTest + @CsvSource(value = {"4:false", "5:false"}, delimiter = ':') + void csvSource2(int input, boolean expected) { + assertThat(numbers.contains(input)).isEqualTo(expected); + } } diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index f0511fafd3a..69287f85eb0 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -2,6 +2,13 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -28,19 +35,29 @@ void subString() { assertThat(substring).isEqualTo("1,2"); } - @Test + @ParameterizedTest @DisplayName("String의 charAt() 메소드를 활용해 특정 위치의 문자를 가져오는 Test") - void charAt1() { - char charAt = "abc".charAt(0); - assertThat(charAt).isEqualTo("a"); + @MethodSource("stringAndIndex") + void charAt1(String input, int index) { + char charAt = "abc".charAt(index); + assertThat(charAt).isEqualTo(input); + } + + public static Stream stringAndIndex() { + return Stream.of( + Arguments.of("a", 0), + Arguments.of("b", 1), + Arguments.of("c", 2) + ); } @Test @DisplayName("String의 charAt() 메소드를 활용해 특정 위치의 문자를 가져올 때 위치 값 벗어났을 때 Exception Test") void charAt2() { int charLength = "abc".length(); - assertThrows(IndexOutOfBoundsException.class, () -> { + assertThatThrownBy(() -> { "abc".charAt(charLength+1); - }); + }).isInstanceOf(IndexOutOfBoundsException.class) + .hasMessageContaining("out of range"); } }