|
| 1 | +import org.junit.jupiter.api.DisplayName; |
| 2 | +import org.junit.jupiter.api.Test; |
| 3 | + |
| 4 | +import static org.assertj.core.api.AssertionsForClassTypes.*; |
| 5 | + |
| 6 | +public class StringTest { |
| 7 | + private final String ASSERTJ_CONTAINS_TEST = "1,2"; |
| 8 | + private final String ASSERTJ_CONTAINS_EXACTLY_TEST = "1,"; |
| 9 | + private final String COMMA = ","; |
| 10 | + private final String SUBSTRING_TEST = "(1,2)"; |
| 11 | + private final String CHARAT_TEST = "abc"; |
| 12 | + |
| 13 | + @Test |
| 14 | + void assertJContains() { |
| 15 | + assertThat(ASSERTJ_CONTAINS_TEST.split(COMMA)).contains("1"); |
| 16 | + assertThat(ASSERTJ_CONTAINS_TEST.split(COMMA)).contains("2"); |
| 17 | + assertThat(ASSERTJ_CONTAINS_TEST.split(COMMA)).doesNotContain("3"); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void assertJContainsExactly() { |
| 22 | + assertThat(ASSERTJ_CONTAINS_EXACTLY_TEST.split(COMMA)).containsExactly("1"); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void substring() { |
| 27 | + int lparenIndex = SUBSTRING_TEST.indexOf('('); |
| 28 | + int rparenIndex = SUBSTRING_TEST.indexOf(')'); |
| 29 | + assertThat(SUBSTRING_TEST.substring(lparenIndex+1, rparenIndex)).isEqualTo(ASSERTJ_CONTAINS_TEST); |
| 30 | + } |
| 31 | + |
| 32 | + @DisplayName("특정 위치의 문자를 반환한다. 위치 값을 벗어나면 StringIndexOutOfBoundsException 발생한다.") |
| 33 | + @Test |
| 34 | + void chatAt() { |
| 35 | + int outOfBoundIndex = 4; |
| 36 | + |
| 37 | + assertThat(CHARAT_TEST.charAt(1)).isEqualTo('b'); |
| 38 | + assertThatExceptionOfType(StringIndexOutOfBoundsException.class) |
| 39 | + .isThrownBy(() -> { |
| 40 | + CHARAT_TEST.charAt(outOfBoundIndex); |
| 41 | + }).withMessage("String index out of range: %d", outOfBoundIndex); |
| 42 | + } |
| 43 | +} |
0 commit comments