AssertJ Core,在检查可迭代/数组内容时是否可能收到实际字段/属性差异的错误消息?
在 Assertj Core 中,您可以逐个字段递归地比较对象。
在此测试中 address.countryCode 在两个对象中有所不同:
@Test
public void shouldBeEqual() {
Person person1 = createPerson();
Person person2 = createPerson2();
assertThat(person1)
.usingRecursiveComparison()
.isEqualTo(person2);
}
我收到错误消息:
java.lang.AssertionError:
Expecting actual:
Person@4b44655e
to be equal to:
Person@290d210d
when recursively comparing field by field, but found the following difference:
field/property 'address.countryCode' differ:
- actual value : "US"
- expected value: "CA"
The recursive comparison was performed with this configuration:
- no overridden equals methods were used in the comparison (except for java types)
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- java.nio.file.Path -> lexicographic comparator (Path natural order)
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
当您在上面的错误消息中获得实际字段差异时,它非常有帮助,但如果我有一个列表并且想要验证列表的内容:
@Test
public void shouldBeEqual2() {
List<Person> personList = List.of(createPerson(), createPerson2());
assertThat(personList)
.hasSize(2)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrder(createPerson(), createPerson());
}
我收到错误消息:
java.lang.AssertionError:
Expecting actual:
[Person@4fe767f3, Person@18ce0030]
to contain exactly in any order:
[Person@2805c96b, Person@4445629]
elements not found:
[Person@4445629]
and elements not expected:
[Person@18ce0030]
when comparing values using recursive field/property by field/property comparator on all fields/properties using the following configuration:
- no overridden equals methods were used in the comparison (except for java types)
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- java.nio.file.Path -> lexicographic comparator (Path natural order)
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
是否无法像第一个示例中那样在此处获取实际的字段差异?
In Assertj Core you can compare ojects field by field recursively.
In this test address.countryCode differ in the two objects:
@Test
public void shouldBeEqual() {
Person person1 = createPerson();
Person person2 = createPerson2();
assertThat(person1)
.usingRecursiveComparison()
.isEqualTo(person2);
}
I get error gessage:
java.lang.AssertionError:
Expecting actual:
Person@4b44655e
to be equal to:
Person@290d210d
when recursively comparing field by field, but found the following difference:
field/property 'address.countryCode' differ:
- actual value : "US"
- expected value: "CA"
The recursive comparison was performed with this configuration:
- no overridden equals methods were used in the comparison (except for java types)
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- java.nio.file.Path -> lexicographic comparator (Path natural order)
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
When you get the actual field difference in the error message like above it is very helpful, but if I have a list and want to verify the content of the list:
@Test
public void shouldBeEqual2() {
List<Person> personList = List.of(createPerson(), createPerson2());
assertThat(personList)
.hasSize(2)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrder(createPerson(), createPerson());
}
I get error message:
java.lang.AssertionError:
Expecting actual:
[Person@4fe767f3, Person@18ce0030]
to contain exactly in any order:
[Person@2805c96b, Person@4445629]
elements not found:
[Person@4445629]
and elements not expected:
[Person@18ce0030]
when comparing values using recursive field/property by field/property comparator on all fields/properties using the following configuration:
- no overridden equals methods were used in the comparison (except for java types)
- these types were compared with the following comparators:
- java.lang.Double -> DoubleComparator[precision=1.0E-15]
- java.lang.Float -> FloatComparator[precision=1.0E-6]
- java.nio.file.Path -> lexicographic comparator (Path natural order)
- actual and expected objects and their fields were compared field by field recursively even if they were not of the same type, this allows for example to compare a Person to a PersonDto (call strictTypeChecking(true) to change that behavior).
Is it not possible to get the actual field difference here like in the first example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前这是不可能的,因为为
containsExactlyInAnyOrder
构建的错误消息不知道断言失败的根本原因,在我们的例子中,它不知道比较差异,它只知道断言失败并构建通用错误消息。我们已经考虑过支持此用例,但以当前的错误设计方式实现起来并不简单。
It is not possible at the moment because the error message built for
containsExactlyInAnyOrder
is not aware of the root cause of the assertion failure, in our case it does not know the comparison differences, it only knows that the assertion failed and build a generic error message.We already have thought of supporting this use case but it is not straightforward to implement with the current way errors are designed.