【问题标题】:Multiple hasProperty constraints for Hamcrest matcherHamcrest 匹配器的多个 hasProperty 约束
【发布时间】:2016-02-05 06:09:12
【问题描述】:

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

assertThat(complexObjectList, Matchers.<ComplexObject>hasItems(
    hasProperty("lang", equalTo(lang)), 
    hasProperty("name", equalTo(name)),
    hasProperty("desc", equalTo(desc)));

我想要一个过滤器

match(lang) &amp;&amp; match(name) &amp;&amp; match(desc)

但是通过上面的代码,我得到了

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

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

【问题讨论】:

    标签: java junit hamcrest


    【解决方案1】:

    您可以使用allOf 匹配器。

    assertThat(complexObjectList,
      Matchers.<ComplexObject>hasItem(allOf(
        hasProperty("lang", equalTo(lang)),
        hasProperty("name", equalTo(name)),
        hasProperty("desc", equalTo(desc))));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2010-09-14
      相关资源
      最近更新 更多