728x90
Hamcrest Assertion
: JUnit의 기본 Assertion 메서드 대체가능
: JUnit 기반의 단위 테스트에서 사용할 수 있는 Assertion 프레임워크
: assertThat()과 매쳐(Matcher)를 사용
: 필요한 매쳐가 있으면 커스텀 가능
사용목적
- 가독성 향상 : 연결하면 그냥 영문장이 됨
- 테스트 실패 메세지의 이해가 쉬움
- 다양한 Matcher를 제공
사용방법
assertThat(실제값, Matcher를 이용한 기대값)
Matcher : 공식문서
방법 예시 1)
// from JUnit
assertEquals(expected, actual);
// to Hamcrest
assertThat(actual, is(equalTo(expected)));
방법 예시 2)
// from JUnit
assertNotNull(currencyName, "should be not null");
// to Hamcrest
assertThat(currencyName, is(notNullValue()));
// assertThat(currencyName, is(nullValue()));
방법 예시 3)
// from JUnit
assertThrows(NullPointerException.class, () -> getCryptoCurrency("XRP"));
// to Hamcrest
Throwable actualException = assertThrows(NullPointerException.class,
() -> getCryptoCurrency("XRP"));
assertThat(actualException.getCause(), is(equalTo(null)));
728x90
'Codestates [Back-end] > 데일리 로그 [TIL]' 카테고리의 다른 글
22.09.08 SpringMVC - 슬라이스 테스트 (데이터 액세스 계층) (0) | 2022.09.08 |
---|---|
22.09.07 SpringMVC - 슬라이스 테스트 (API계층) (0) | 2022.09.07 |
22.09.07 SpringMVC - 단위 테스트 (JUnit) (0) | 2022.09.07 |
22.09.07 SpringMVC - 단위 테스트 (기본) (0) | 2022.09.07 |
22.09.06 SpringMVC - 트랜잭션 (분산 적용 방법 = 한 개 이상 DB용) (0) | 2022.09.06 |