사용자 도구

사이트 도구


java:junit:hamcrest

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:junit:hamcrest [2012/01/17 01:16]
kwon37xi
java:junit:hamcrest [2015/11/23 20:05] (현재)
kwon37xi [의존성]
줄 1: 줄 1:
 ====== hamcrest ====== ====== hamcrest ======
-  * http://code.google.com/p/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 ===== ===== hasProperty =====
줄 7: 줄 13:
   * 단일 객체 비교<code java>   * 단일 객체 비교<code java>
 User user = new User("이하늬", 20, "몰라"); // name, age, description User user = new User("이하늬", 20, "몰라"); // name, age, description
 +
 +// 테스트 결과 Matchers.<Type>hasProperty(..) 형태로 사용해야만 제대로 작동했다.
 assertThat(user, Matchers.<User>hasProperty("name", is("이하늬"))); assertThat(user, Matchers.<User>hasProperty("name", is("이하늬")));
  
줄 12: 줄 20:
 assertThat(user, Matchers.<User>hasProperty("name", nullValue())); assertThat(user, Matchers.<User>hasProperty("name", nullValue()));
 </code> </code>
-  * [[http://theholyjava.wordpress.com/2011/10/15/hasproperty-the-hidden-gem-of-hamcrest-and-assertthat/|hasProperty, the Hidden Gem of Hamcrest]]<code java>+  * 컬렉션<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() assertThat("Expected images", (Iterable<Object>) hotel.getImages()
             , containsInAnyOrder(hasProperty("filename", is("radisson1.jpg"))             , containsInAnyOrder(hasProperty("filename", is("radisson1.jpg"))
             , hasProperty("filename", is("radisson2.jpg"))));             , hasProperty("filename", is("radisson2.jpg"))));
 </code> </code>
-  * [[http://weblogs.java.net/blog/johnsmart/archive/2008/04/on_the_subtle_u.html|On the subtle uses of Hamcrest tests]]<code java>+  * [[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 관련 문제 피해가기 // hamcrest의 generic 관련 문제 피해가기
 List stakeholders  = stakeholderManager.findByName("Health"); // 원래는 List<Stakeholder> 타입이지만 hamcrest 비교시 문제가 발행해서 List로 변경. List stakeholders  = stakeholderManager.findByName("Health"); // 원래는 List<Stakeholder> 타입이지만 hamcrest 비교시 문제가 발행해서 List로 변경.
java/junit/hamcrest.1326730619.txt.gz · 마지막으로 수정됨: 2012/01/17 01:16 저자 kwon37xi