내용으로 건너뛰기
권남
사용자 도구
로그인
사이트 도구
검색
도구
문서 보기
이전 판
역링크
최근 바뀜
미디어 관리자
사이트맵
로그인
>
최근 바뀜
미디어 관리자
사이트맵
추적:
java:junit:hamcrest
이 문서는 읽기 전용입니다. 원본을 볼 수는 있지만 바꿀 수는 없습니다. 문제가 있다고 생각하면 관리자에게 문의하세요.
====== hamcrest ====== * http://hamcrest.org/ : 원래 Java용이었으나 다양한 언어로 만들어짐. * [[http://www.javacodegeeks.com/2015/11/hamcrest-matchers-tutorial.html|Hamcrest matchers tutorial]] * [[http://www.javacodegeeks.com/2015/11/custom-hamcrest-matchers.html|Custom Hamcrest Matchers]] ===== 의존성 ===== * 의존성을 걸때는 최소한 ''hamcrest-core''와 ''hamcrest-library''를 함께 걸어줘야 충분한 Matcher들을 확보할 수 있다. * 그렇지 않으면 한번에 ''hamcrest-all''에 걸어준다. ===== hasProperty ===== * [[http://www.jarvana.com/jarvana/view/org/hamcrest/hamcrest-library/1.2.1/hamcrest-library-1.2.1-javadoc.jar!/org/hamcrest/Matchers.html#hasProperty%28java.lang.String,%20org.hamcrest.Matcher%29|hasProperty]] : CoreMatchers가 아니 [[http://mvnrepository.com/artifact/org.hamcrest/hamcrest-library|Matchers]]에 있다. * 객체의 전체를 비교하지 않고 일부 필드의 값만 비교하고자 할 때 사용한다. 특히, 컬렉션과 연동해서 사용할 때 좋다. * 단일 객체 비교<code java> User user = new User("이하늬", 20, "몰라"); // name, age, description // 테스트 결과 Matchers.<Type>hasProperty(..) 형태로 사용해야만 제대로 작동했다. assertThat(user, Matchers.<User>hasProperty("name", is("이하늬"))); user = new User(null, 20, "몰라"); // name, age, description assertThat(user, Matchers.<User>hasProperty("name", nullValue())); </code> * 컬렉션<code java> List<User> users = new ArrayList<User>(); users.add(new User("이하늬", 20, "예뻐~")); users.add(new User("김태희", 30, "더 예뻐~")); assertThat(users, Matchers.<User>hasItem(Matchers.<User>hasProperty("name", is("김태희")))); </code> * [[http://theholyjava.wordpress.com/2011/10/15/hasproperty-the-hidden-gem-of-hamcrest-and-assertthat/|hasProperty, the Hidden Gem of Hamcrest]] 다른 예. containsInAnyOrder. (Iterable<Object>)는 위에서 처럼 타입을 명시한다면 불필요해 보임.<code java> assertThat("Expected images", (Iterable<Object>) hotel.getImages() , containsInAnyOrder(hasProperty("filename", is("radisson1.jpg")) , hasProperty("filename", is("radisson2.jpg")))); </code> * [[http://weblogs.java.net/blog/johnsmart/archive/2008/04/on_the_subtle_u.html|On the subtle uses of Hamcrest tests]] Generic에 관한 문제를 설명한 글인데, 여기도 마찬가지로 타입을 명시한다면 불필요한 작업으로 보임.<code java> // hamcrest의 generic 관련 문제 피해가기 List stakeholders = stakeholderManager.findByName("Health"); // 원래는 List<Stakeholder> 타입이지만 hamcrest 비교시 문제가 발행해서 List로 변경. assertThat((List<Object>) stakeholders, hasItem(hasProperty("name",is("Health Associates")))); </code>
java/junit/hamcrest.txt
· 마지막으로 수정됨: 2015/11/23 20:05 저자
kwon37xi
문서 도구
문서 보기
이전 판
역링크
맨 위로