【问题标题】:JUnit Assert, Matchers and nested objectsJUnit 断言、匹配器和嵌套对象
【发布时间】:2017-11-06 08:56:40
【问题描述】:

我有以下收藏:

Set<DecisionGroup> parentDecisionGroups

首先在我的测试中,我需要检查这个集合是否包含两个具有给定 ID 的对象:

assertThat(parentDecisionGroups, hasItem(hasProperty("id", equalTo(decisionGroup1.getId()))));
assertThat(parentDecisionGroups, hasItem(hasProperty("id", equalTo(decisionGroup2.getId()))));

到目前为止一切都很好......

现在我需要检查 parentDecisionGroups.get(0).getOwnerDecision()(其中 parentDecisionGroup.id == decisionGroup1.getId())是否等于 decision1parentDecisionGroups.get(1).getOwnerDecision()(其中 parentDecisionGroup.id == decisionGroup2.getId())是等于decision2

如何使用org.hamcrest.*org.junit.Assert.* 做到这一点?

【问题讨论】:

    标签: java junit hamcrest


    【解决方案1】:

    您可以使用CombinableMatcherboth(matcher1).and(matcher2) 的匹配器。

    所以你会得到类似的东西:

    assertThat(parentDecisionGroups, hasItem(
                   both(hasProperty("id", equalTo(decisionGroup1.getId()))).
                   and(hasProperty("ownerDecision", equalTo("decision1"))));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-05
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 2019-11-02
      相关资源
      最近更新 更多