-
Notifications
You must be signed in to change notification settings - Fork 1
Junit 대신 assertj쓰기
kimhanui edited this page Nov 17, 2020
·
1 revision
assertj도 Junit에서 자동으로 lib등록을 해줌! 그런데 왜 익숙한 junit이 아닌 assertj를 썼을까?
@Test
public void 롬복_기능_테스트(){
//given
String name = "test";
int amount = 1000;
//when
HelloResponseDto dto = new HelloResponseDto(name, amount);
//then
assertThat(dto.getName()).isEqualTo(name); <-- 여기의 assertj의 assertThat가 쓰인 것.
assertThat(dto.getAmount()).isEqualTo(amount);
}백기선님의 비교강의: https://www.youtube.com/watch?v=zLx_fI24UXM&t=408s
assertThat(bookStore.getLocation(), Matchers.is("seattle")); <-- Matchers가 들어감- CoreMatchers와 달리 추가적으로 lib가 필요하지 않음
- JUnit의 assertThat을 쓰게되면 is()와 같이 CoreMatchers lib가 필요하다.
- 자동완성이 좀 더 확실하게 지원된다.
- IDE에서는 CoreMatchers와 같은 Matcher lib의 자동완성 지원이 약하다.
- 무슨 말이냐면 junit은 assertThat쓰려면 메서드를 다 외워야되는데 (위 예시에서 두번째 param에 is만 쓰면 Matchers.is라고 추천하거나 수정해주지않음), assertj는 알아서 자동완성이 뜨니까 안 외워도 됨.
- Mactcher 라이브러리 : 필터나 검색등을 위해 값을 비교할 때 좀 더 편리하게 사용하도록 도와주는 라이브러리
- hamcrest는 JUnit에 사용되는 Matcher 라이브러리이다. 테스트 표현식을 작성할 때 좀 더 문맥적으로 자연스럽고 우아한 문장을 만들 수 있도록 도와준다.
예를들어 assertEquals(expedted, actual)이라는 코드보다 assertThat(expected, is(actual))라는 코드가 훨씬 보기 쉬울 것이다.
- assertThat을 위해 assertj를 쓰는거임. 왜? junit의 assertThat은 구리니까!(static import가 안될때가 있음)
- 테스트 실행(@Test이런거) junit쓰는데 assertThat은 assertj가 좋다.