Skip to content
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

학습 테스트 실습 리뷰 부탁드립니다 #1843

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

import org.assertj.core.api.Assertions;
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.*;

@DisplayName("Set Collection에 대한 학습 테스트")
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
@DisplayName("Set size 확인")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DisplayName 으로 어떠한 테스트 인지 파악할 수 있어 좋네요 💯

메소드 이름이나 @DisplayName이 어느 정도까지 구체적이어야 하는지에 대해서 정답은 없다고 생각합니다.
다만, 이름만으로도 언제 이 메서드를 호출해야 하는지 또 어떤 테스트를 하는 것인지 의미를 파악할 수 있도록 작성하시면 다른 사람이 볼때도 쉽게 의도를 파악할 수 있을거 같네요. 아래 자료도 참고해 보시면 좋을거 같아요. 👍

https://woowacourse.github.io/javable/2020-04-26/Method-Naming

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 링크 404 뜨고 있습니다... ㅜㅜㅜㅜㅜ

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public void setTest01() throws Exception {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

물론 DisplayName으로 어떤 테스트인지 파악할 수 있지만 함수명도 어떤 의미의 테스트인지 담을 수록 좋습니다.
다음 단계부터는 함수명도 의미있는 이름으로 바꿔보도록 해요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

함수명... 변수명...
이름짓기가 너무 힘드네요😂
https://www.curioustore.com/#!/
사이트의 도움을 받아야겠습니다.

assertThat(numbers.size()).isEqualTo(3);
}


@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
@DisplayName("Set 값 존재여부 확인")
public void setTest02(int inputNumber) throws Exception {
assertThat(numbers.contains(inputNumber)).isTrue();
}

@ParameterizedTest
@CsvSource(value = {"0:false","1:true","2:true","3:true","4:false"}, delimiter = ':')
@DisplayName("Set 값 존재 확인")
public void setTest03(int inputNumber, boolean isFree) throws Exception {
assertThat(numbers.contains(inputNumber)).isEqualTo(isFree);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

커밋 로그를 의미있는 단위로 나눠주시면 좋습니다.
또한 메시지에 로그 타입도 같이 작성해주시면 좋을거 같아요 👍

feat (feature)
fix (bug fix)
docs (documentation)
style (formatting, missing semi colons, …)
refactor
test (when adding missing tests)
chore (maintain)

아래 링크 참고하셔서 미션 수행시 기능 단위로 Commit Log 남길때 참고해보시면 좋을거 같아요 😄
https://gist.github.com/stephenparish/9941e89d80e2bc58a153

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

커밋 컨벤션 처음 알았네요!
커밋로그에 맞게 남기곘습니다👍

51 changes: 51 additions & 0 deletions src/test/java/study/StringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package study;


import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

// String 클래스에 대한 학습 테스트
@DisplayName("String 클래스에 대한 학습 테스트")
public class StringTest {

@Test
@DisplayName("문자열 1,2 나누기")
public void stringTest01() throws Exception {
String[] result = "1,2".split(",");
// assertThat(result).contains("1", "2");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의미있는 주석이 아니라면 웬만하면 삭제하여주세요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의미없는 주석은 삭제하겠습니다

assertThat(result).containsExactly("1", "2");
}

@Test
@DisplayName("문자열 1, 나누기")
public void stringTest02() throws Exception {
String[] result = "1,".split(",");
assertThat(result).containsExactly("1");
}

@Test
@DisplayName("문자열 (1,2) substring 으로 ()제거하기")
public void stringTest03() throws Exception {
String data = "(1,2)";
String result = data.substring(1, data.length()-1);
assertThat(result).isEqualTo("1,2");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘 작성하셨네요 💯

추가로 아래처럼 given, when, then 순으로 작성하시면
좀 더 가독성 및 Test 코드를 보고 의도를 파악하기 쉽습니다.😄

// given
String data = "(1,2)";

// when
String result = data.substring(1, data.length()-1);

// then
assertThat(result).isEqualTo("1,2");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given, when, then 템플릿으로 진행하겠습니다

}

@Test
@DisplayName("문자열 abc 뽑아내기")
public void stringTest04() throws Exception {
String data = "abc";

assertEquals(data.charAt(0), 'a');
assertEquals(data.charAt(1), 'b');
assertEquals(data.charAt(2), 'c');

assertThatThrownBy(() -> {
data.charAt(3);
}).isInstanceOf(IndexOutOfBoundsException.class)
.hasMessageContaining("String index out of range: 3");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외 테스트도 잘 작성하여주셨습니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외테스트 경우엔 실패할 에러메시지까지 일치시켜야 되는건가요?
아니면 예외처리안에서 에러가 발생하면 통과인지 궁금합니다.

}
}