Hamcrest匹配器的多个hasProperty约束

bur*_*ete 3 java junit hamcrest

我尝试了以下代码,用于匹配List中的单个ComplexObject

assertThat(complexObjectList, Matchers.<ComplexObject>hasItems(
    hasProperty("lang", equalTo(lang)), 
    hasProperty("name", equalTo(name)),
    hasProperty("desc", equalTo(desc)));
Run Code Online (Sandbox Code Playgroud)

我想要一个过滤器

match(lang) && match(name) && match(desc)

但是用上面的代码,我明白了

match(lang) || match(name) || match(desc)

如何验证这三种不同的hasProperty匹配器?

Ste*_*ner 10

您可以使用allOf匹配器.

assertThat(complexObjectList,
  Matchers.<ComplexObject>hasItem(allOf(
    hasProperty("lang", equalTo(lang)),
    hasProperty("name", equalTo(name)),
    hasProperty("desc", equalTo(desc))));
Run Code Online (Sandbox Code Playgroud)