JUnit声明,匹配器和嵌套对象 [英] JUnit Assert, Matchers and nested objects

查看:79
本文介绍了JUnit声明,匹配器和嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下收藏:

Set<DecisionGroup> parentDecisionGroups

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

first of all in my test I need to check that this collection contains two objects with a given ids:

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

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

so far so good...

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

Right now I need to check that parentDecisionGroups.get(0).getOwnerDecision() (where parentDecisionGroup.id == decisionGroup1.getId()) is equals to decision1 and parentDecisionGroups.get(1).getOwnerDecision() (where parentDecisionGroup.id == decisionGroup2.getId()) is equals to decision2

如何使用org.hamcrest.*org.junit.Assert.*完成此操作?

how to do this with org.hamcrest.* and org.junit.Assert.* ?

推荐答案

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

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

So you'll get something like:

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

这篇关于JUnit声明,匹配器和嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆