2013-11-20 29 views
24
import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.Matchers.hasItem; 
import static org.hamcrest.Matchers.equalTo; 

assertThat(actual, hasItem(hasProperty("id", equalTo(1L)))); 

其中實際是PO爲ID爲Long的POJO。hamcrest hasItem hasProperty,如果存在屬性值的對象存在,則斷言

我得到的,

在類型MatcherAssert方法assertThat(T, Matcher<? super T>)不適用於參數(List<Pojo>, Matcher<Iterable<? super Object>>)

從各種文件和其他計算器的頁面,它應該是有效的,但我得到上述錯誤。

回答

49

嘗試明確的類型參數填充 - 假設actualList<YourPojo>,嘗試調用:

assertThat(actual, hasItem(Matchers.<YourPojo>hasProperty("id", equalTo(1L)))); 
+0

了,謝謝。作品。使用containsInAnyOrder時,我們遇到了類似的問題,但無法使其正常工作。 – wenic

6

較短的版本時,您不必指定類類型:

List<IssueDefinitionDto> definitions = ...; // Tested variable 
... 
assertThat(definitions, hasItem(hasProperty("id", is(10L))));