我正在寻找一种解决方案来检查集合中的每个项目是否具有expectedNullField
空字段。
以下不起作用:
assertThat(aCollection).extracting("expectedNullField").isNull();
请注意,以下内容按预期工作:
assertThat(aCollection).extracting("expectedNotNullField").isNotNull();
有人帮我吗?
谢谢。
If you know the size (let's say it is 3) you can use
assertThat(aCollection).extracting("expectedNullField")
.containsOnly(null, null, null);
or if you are only interested in checking that there is a null value
assertThat(aCollection).extracting("expectedNullField")
.containsNull();
Note that you can't use:
assertThat(aCollection).extracting("expectedNullField")
.containsOnly(null);
because it is ambiguous (containsOnly specifying a varargs params).
I might consider adding containsOnlyNullElements()
in AssertJ to overcome the compiler error above.
您可以使用条件
Condition<YourClass> nullField = new Condition<>("expecting field to be null") {
@Override
public boolean matches(YourClass value) {
return value.getField() == null;
}
};
assertThat(aCollection).have(nullField);
这可能比其他解决方案更容易阅读
assertThat(aCollection).filteredOn("expectedNullField", not(null)).isEmpty();