Skip to content

학습 테스트 #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/test/java/study/SetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package study;

import org.junit.jupiter.api.BeforeEach;
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 static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashSet;
import java.util.Set;

public class SetTest {
private Set<Integer> numbers;

@BeforeEach
void setUp() {
numbers = new HashSet<>();
numbers.add(1);
numbers.add(1);
numbers.add(2);
numbers.add(3);
}

@Test
void sizeTest() {
int size = numbers.size();
assertThat(size).isEqualTo(3);
}

@Test
void contains() {
assertThat(numbers.contains(1)).isTrue();
assertThat(numbers.contains(2)).isTrue();
assertThat(numbers.contains(3)).isTrue();
}

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void contains2(int input) {
assertTrue(numbers.contains(input));
}


@ParameterizedTest
@CsvSource(value = {"1:true", "2:true", "3:true", "4:false", "5:false"}, delimiter = ':')
void contains3(int input, boolean expected) {
assertThat(numbers.contains(input)).isEqualTo(expected);
}
}

49 changes: 48 additions & 1 deletion src/test/java/study/StringTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
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 {
@Test
void replace() {
String actual = "abc".replace("b", "d");
assertThat(actual).isEqualTo("adc");
}

@Test
void split() {
String[] values = "1,2".split(",");
assertThat(values).containsExactly("1","2");

values = "1".split(",");
assertThat(values).contains("1");
}

@Test
void subString() {
String input = "(1,2)";
String substring = input.substring(1, input.length() - 1);
assertThat(substring).isEqualTo("1,2");
}

// StringIndexOutOfBoundsException: 배열에 할당되지 않은 메모리(인덱스)에 접근했을때 뜨는 에러문
@Test
@DisplayName("특정 위치의 문자를 가져올 때 위치 값을 벗어나면 StringIndexOutOfBoundsException")
void charAt() {
String input = "abc"; // String.length 의 길이는 2 이다.
int index = 3;

assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> {
input.charAt(index);
// (중요) withMessageMatching 에러 문장을 정확히 기재해야 통과
}).withMessageMatching("String index out of range: 3");
}



// Expecting code to raise a throwable. : isInstanceOf() 함수에 입력된 오류가 발생하지 않을 경우 뜨는 에러
@Test
@DisplayName("특정 위치의 문자를 가져올 때 위치 값을 벗어나면 StringIndexOutOfBoundsException")
void charAt2() {
String input = "abc";
int index = 10;
assertThatThrownBy(() -> {
input.charAt(index);
}).isInstanceOf(StringIndexOutOfBoundsException.class) // 예상되는 에러를 지정하는 함수
.hasMessageContaining("%d", index); // 예상되는 에러에 나오는 메세지에 들어있을 내용을 적는다. 맞으면 성공, 아니면 실패
}


}