From ca9c604db516f482a4a33d0ad686bcd55ecb673e Mon Sep 17 00:00:00 2001 From: ohdeng02 Date: Sat, 13 Jul 2024 23:42:40 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=8B=A4=EC=8A=B5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 48 ++++++++++++++++++++++++++ src/test/java/study/StringTest.java | 52 +++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 3 deletions(-) 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 0000000..466f03b --- /dev/null +++ b/src/test/java/study/SetTest.java @@ -0,0 +1,48 @@ +package study; + +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.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +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 Case 구현 + + @DisplayName("Set size() 테스트") + @Test + void size() { + assertThat(numbers.size()).isEqualTo(3); + } + + @DisplayName("Set contains() 테스트") + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + void contains(int number) { + assertThat(numbers.contains(number)).isTrue(); + } + + @DisplayName("Set contains() 참거짓 테스트") + @ParameterizedTest + @CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':') + void contains2(int number, boolean expected) { + assertThat(numbers.contains(number)).isEqualTo(expected); + } +} diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 43e47d9..4f06470 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,13 +1,59 @@ package study; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.*; public class StringTest { + @DisplayName("문자열 일치 테스트") @Test void replace() { - String actual = "abc".replace("b", "d"); - assertThat(actual).isEqualTo("adc"); + String actual = "abc".replace("b", "d"); //실행 단계 + assertThat(actual).isEqualTo("adc"); //검증 단계 + } + + @DisplayName("문자열 split() 테스트") + @Test + void split() { + //given + String str1 = "1,2"; + String str2 = "1"; + + //when + String[] actual1 = str1.split(","); + String[] actual2 = str2.split(","); + + //then + assertThat(actual1).containsExactly("1", "2"); + assertThat(actual2).containsExactly("1"); + } + + @DisplayName("문자열 substring() 테스트") + @Test + void substring() { + //given + String str = "(1,2)"; + + //when + String actual = str.substring(1, str.length() - 1); + + //then + assertThat(actual).isEqualTo("1,2"); + } + + @DisplayName("문자열 charAt() 테스트") + @Test + void charAt() { + //given + String str = "abc"; + + //when + Throwable thrown = catchThrowable(() -> str.charAt(3)); + + //then + assertThat(thrown) + .isInstanceOf(StringIndexOutOfBoundsException.class) + .hasMessageContaining("String index out of range: 3"); } }