***Junit5
*******************************************************Annotation
@BeforeAll,@AfterAll : 클래스에 있는 모든 메소드를 수행할 때 ,
메소드 수행전 , 후에 수행될 어노테이션이다.
@AfterEach,@BeforeEach : 특정 메소드가 시작되기 후, 전 마다 수행되는 어노테이션
@AfterEach : 다음 테스트에 영향을 주지 않기 위해 종료되어야할 리소스를 처리하는 부분
@BeforeEach : 테스트 할 때의 초기 환경을 setup
@Test : Junit이 수행할 코드 ,메소드에 붙음
@Nested : 연관 테스트 함수를 묶어주고 , 중첩테스트클래스임을 나타내줌 (클래스 레밸)
@RepeatedTest : value 속성에 지정된 수 만큼 반복 ,name 속성에 {displayName} {currentRepetition} {totalRepetitions} 가 함께 쓰일 수 있음
@DisplayName : 테스트 이름 설정 ( 클래스 , 메소드 레밸 이며 주로 @Nested와 같이 쓰임 )
@DisplayName 사용이유
- 메소드명을 한글로 작성해도 언더바를 작성해야 해서 가독성이 좋지 않음
- @Nested 와 함께 쓰려면 클래스를 작성해야 하는데 @DisplayName 을 쓰는게 깔끔
- JUnit 개발자들은 영어가 모국어 수준일텐데도 @DisplayName 어노테이션을 추가했다는 점
@ParameterizedTest : 하나의 테스트 메소드로 여러 개의 파라미터에 대해서 테스트할 수 있습니다.
name 속성을 이용하여 파라미터를 받는 메소드들에게 이름을 지정해 줄 수 있습니다.
name속성 내부에 , {index}는 1부터 시작되며 1씩증가
{arguments}는 모든 파라미터들을 출력
{0}은 첫번째 인수 , {1}은 두번째 인수 , ....
@ValueSource : 리터럴 값의 단일 배열을 지정할 수 있으며 매개 변수화 된 테스트 호출마다 단일 인수를 제공하는 데만 사용할 수 있습니다. @ParameterizedTest 와 함께 쓰임
@CsvSource : ValueSource의 확장판 ,여러 인자를 delimeter(구분자)로 구분해서 넘겨줌.
@ParameterizedTest 와 함께 쓰임
@NullSource @EmptySource : @NullSource @EmptySource 를 사용하면 파라미터 값으로 null과 empty를 넣어줍니다.
@ParameterizedTest 와 함께 쓰임
@NullAndEmptySource : null과 빈 공백을 파라미터값으로 넣어줍니다.
@ParameterizedTest 와 함께 쓰임
example1) ValueSource 인수로써 문자열은 strings 로 , 정수형은 ints 형으로 하고 , method의 인자인 name으로 하나씩 들어감
@ParameterizedTest
@ValueSource(strings = {"q", "qwerasdfzxcv", "qq23"})
void method(String name){
~
}
example2) CsvSource 의 두번째 문자열내부의 tEst 와 test 이 method 인자의 input, expected에 각각 들어감
@ParameterizedTest
@CsvSource(value = {"test:test", "tEst:test", "Java:java"}, delimiter = ':')
void method(String input, String expected) {
String actualValue = input.toLowerCase();
assertEquals(expected, actualValue);
}
*******************************************************method
assertThat :
대부분의 경우 , 메서드 체이닝할 수 있어서 기존 assertXXX 메서드보다 더 많은 유연성을 제공.
예를 보자.
contains : 중복여부, 순서에 관계 없이 값만 일치하면 성공(expected가 actual에 포함되기만 하면 성공)
example) contains 예제
List<Integer> list = Arrays.asList(1, 2, 3);
// Success: 모든 원소를 입력하지 않아도 성공
assertThat(list).contains(1, 2);
// Success: 중복된 값이 있어도 포함만 되어 있으면 성공
assertThat(list).contains(1, 2, 2);
// Success: 순서가 바뀌어도 값만 맞으면 성공
assertThat(list).contains(3, 2);
// Fail: List 에 없는 값을 입력하면 실패
assertThat(list).contains(1, 2, 3, 4);
containsOnly : 순서,중복를 무시하는 대신 원소값과 갯수가 정확히 일치
containsExactly : 중복없어야 하고 , 순서를 포함해서 정확히 일치
assertThatThrownBy : 예외처리를 가독성 있게 테스트할 수 있는 함수제공.
assertThatThrownBy(() -> { 예외발생; }).isInstanceOf(예외종류.class)
.hasMessageContaining(예외문구);
assertThatExceptionOfType : assertThatThrownBy 와 비슷하다.
isEqualTo : actual 와 expected가 동일하면 통과
example)
assertThat(actual).isEqualTo(expected)
legacy assert 메소드
메소드 | 설명 |
assertEquals(x, y) | 객체 x와 y가 일치함을 확인합니다. · x(예상 값)와 y(실제 값)가 같으면 테스트 통과 |
assertArrayEquals(a, b) | 배열 A와 B가 일치함을 확인합니다. |
assertFalse(x) | x가 false 인지 확인합니다. |
assertTrue(x) | x가 true 인지 확인합니다. |
assertTrue(message, condition) | condition이 true이면 message표시 |
assertNull(o) | 객체o가 null인지 확인합니다. |
assertNotNull(o) | 객체o가 null이 아닌지 확인합니다. |
assertSame(ox, oy) | 객체 ox와 oy가 같은 객체임을 확인합니다. · ox와 oy가 같은 객체를 참조하고 있으면 테스트 통과 · assertEquals()메서드는 두 객체의 값이 같은지 확인하고, assertSame()메서드는 두 객체의 레퍼런스가 동일한가를 확인합니다. (== 연산자) |
assertNotSame(ox, oy) | ox와 oy가 같은 객체를 참조하고 있지 않으면 통과 |
assertfail() | 테스트를 바로 실패처리 |
참고 : https://sas-study.tistory.com/314
https://jongmin92.github.io/2020/03/31/Java/use-assertthat/ (assertThat)
https://bcp0109.tistory.com/317 ( containsXXX)
https://bcp0109.tistory.com/297 ( @Nested , @DisplayName )
JUnit 5 알아보기 (tistory.com) ( @Nested )
https://pjh3749.tistory.com/m/241 ,
https://velog.io/@new_wisdom/assertJ-%EA%B3%B5%EC%8B%9D%EB%AC%B8%EC%84%9C%EC%99%80-%ED%95%A8%EA%BB%98%ED%95%98%EB%8A%94-assertJ-%EC%A0%95%EB%A6%AC
( assertThatThrownBy , assertThatExceptionOfType)
https://pjh3749.tistory.com/241 ( isEqualTo )
https://dublin-java.tistory.com/56 ( @ParameterizedTest , @ValueSource , @NullAndEmptySource)
https://velog.io/@max9106/JUnit5-%ED%85%8C%EC%8A%A4%ED%8A%B8-%EB%B0%98%EB%B3%B5-%ED%95%98%EA%B8%B0
https://www.baeldung.com/parameterized-tests-junit-5 ( @CsvSource )
https://beomseok95.tistory.com/205 ( assert 메소드모음 )
JUnit 5 Parameterized Tests 사용하기 (tistory.com) @NullSource @EmptySource
https://effortguy.tistory.com/116 (RepeatedTest)
'21년이전 > 웹_' 카테고리의 다른 글
테스트오류 문제 - No tests found for given includes (0) | 2021.09.07 |
---|---|
SPRING - Logback 사용하기 (0) | 2021.07.29 |
Spring - 컨트롤러가 두번 호출되는 문제 (0) | 2021.07.29 |
Spring security 적용 (0) | 2021.07.28 |
MVN Repository (0) | 2021.07.26 |