【发布时间】:2016-08-25 23:09:00
【问题描述】:
我可以使用以下代码检查列表是否有大于 30 的项目。
//Using Hamcrest
List<Integer> ints= Arrays.asList(22,33,44,55);
assertThat(ints,hasItem(greaterThan(30)));
但是如果一个列表至少有 2 个大于 30 的项目,我怎么能断言呢?
有了AssertJ,我知道有一个解决方案。但我不知道如何通过Hamcrest 实现这一点。
//Using AssertJ
List<Integer> ints= Arrays.asList(22,33,44,55);
Condition<Integer> greaterThanCondition = new Condition<Integer>("greater") {
@Override
public boolean matches (Integer i){
return i>30;
}
} ;
assertThat(ints).haveatLeast(2,greaterThanCondition);
【问题讨论】: