Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 백준 2630번
- 스파르타코딩클럽
- 백준
- spartacodingclub
- 전문가를 위한 스프링5
- 가상 채팅서버
- 스프링 MVC 2편
- 타임리프
- 알고리즘
- 김영한님
- 1시간 만에 끝내는 직장인 코딩 용어
- SQL
- 팀300
- 스프링 시큐리티 구조
- 전문가를 위한 스프링
- 스프링
- 누구나큰일낼수있어
- 백준 1992번
- thymeleaf
- JPA
- 자바의 정석 기초편
- 스파르타코딩클럽 #spartacodingclub #누구나큰일낼수있어
- 채팅서버 설계
- 백준 1992번 풀이
- 스프링심화반
- 스프링 입문을 위한 자바 객체 지향의 원리와 이해
- C++
- 백준 #N과 M(4) #백트래킹
- 스프링 핵심원리 기본편
- 객체지향의 사실과 오해
Archives
- Today
- Total
민우의 코딩노트
AssertJ 필수 부분 정리 본문
https://pjh3749.tistory.com/241
출처: 위의 글을 보고 AssertJ의 필수 부분을 정리해보았습니다.
Filtering assertions - iterables나 arrays에 적용되는 filtering
List<Human> list = new ArrayList<>();
Human park = new Human("Park", 25);
Human jack = new Human("Jack", 22);
list.add(park);
list.add(jack);
assertThat(list).assertThat(list).filteredOn(human -> human.getName().contains("a"))
.containsOnly(park, jack);
assertThat 구문에서 filteredOn을 사용한다.
이름을 가져와서 a 가 포함되어 있는 객체들만 필터링을 하고 그 객체를 검증한다.
assertThat(list).filteredOn("age", 25).containsOnly(park, lee);
assertThat 구문에서 클래스의 프로퍼티(나이)에 접근하여 값을 검증한다.
assertThat(list).filteredOn("age", notIn(22)).containsOnly(park, lee);
notIn 함수를 사용하여, age가 22가 아닌 객체를 검증할 수 있음.
프로퍼티를 추출하기
assertThat(list).extracting("name").contains("Kim", "Park", "Lee", "Amy", "Jack");
extracting()을 사용하면 프로퍼티를 쉽게 추출 가능하다.
assertThat(list).extracting("name", String.class).contains("Kim", "Park", "Lee", "Amy", "Jack");
하나의 인자를 검증할 때는 클래스를 명시하여 타입 검증을 강하게 할 수 있다.
assertThat(list).extracting("name", "age")
.contains(tuple("Kim", 22)
튜플로도 추출 가능하다. (여러 필드를 한 번에 검증할 때 유용)
(잘못된 사용법 피하기)
as()를 assertion 이전에 부르지 말기
assertThat(actual).as("description").isEqualTo(expected);
comparator를 assertion 이전에 불러라
assertThat(actual).usingComparator(new CustomComparator()).isEqualTo("a");
BDD 스타일
bdd스타일은 given, when, then으로 이루어진 스타일
Exception 처리 test
assertThatThrownBy()
assertThatThrownBy(() -> { throw new Exception("boom!"); }).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
assertThatThrownBy()는 좀 더 나은 가독성으로 작성할 수 있게 해준다.
자주 쓰이는 예외 처리 syntax
- assertThatNullPointerException
- assertThatIllegalArgumentException
- assertThatIllegalStateException
- assertThatIOException
예외를 던지지 않는 경우 처리
assertThatCode(() -> {
// code that should throw an exception
...
}).doesNotThrowAnyException();
'Knowledge > Spring' 카테고리의 다른 글
빈 생명주기 콜백 - 스프링 핵심원리 기본편 (0) | 2022.03.08 |
---|---|
[JPA] 연관관계 매핑 - @OneToMany @ManyToOne (0) | 2022.02.22 |
Spring boot JPA / ModelMapper, Entity -> DTO 변환 (0) | 2022.02.02 |
Swagger UI 3.0 적용하여 편리하게 API 명세서 작성하기 (0) | 2022.01.29 |
Spring JPA - 날짜 사이 데이터 가져오기 (LocalDateTime) (0) | 2022.01.29 |