我正在寻找一种解决方案,以检查集合中的每个项目都具有expectedNullFieldnull 字段。
以下内容不起作用:
assertThat(aCollection).extracting("expectedNullField").isNull();
请注意以下工作正常:
assertThat(aCollection).extracting("expectedNotNullField").isNotNull();
有人帮我吗?
谢谢。
如果您知道尺寸(假设是3),则可以使用
assertThat(aCollection).extracting("expectedNullField")
.containsOnly(null, null, null);
Run Code Online (Sandbox Code Playgroud)
或者如果您只想检查是否有空值
assertThat(aCollection).extracting("expectedNullField")
.containsNull();
Run Code Online (Sandbox Code Playgroud)
请注意,您不能使用:
assertThat(aCollection).extracting("expectedNullField")
.containsOnly(null);
Run Code Online (Sandbox Code Playgroud)
因为它是模棱两可的(仅包含varargs参数)。
我可能会考虑添加containsOnlyNullElements()AssertJ以克服上面的编译器错误。
您可以使用条件
Condition<YourClass> nullField = new Condition<>("expecting field to be null") {
@Override
public boolean matches(YourClass value) {
return value.getField() == null;
}
};
assertThat(aCollection).have(nullField);
Run Code Online (Sandbox Code Playgroud)
这可能比其他解决方案更容易阅读
assertThat(aCollection).filteredOn("expectedNullField", not(null)).isEmpty();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1835 次 |
| 最近记录: |