AssertJ Core,在检查 iterables/arrays 内容时是否可能得到与实际 field/property 差异的错误消息?
AssertJ Core, is it possible to get error message with actual field/property difference when checking iterables/arrays content?
在 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).
这里不能像第一个例子那样得到实际的字段差异吗?
目前不可能,因为为 containsExactlyInAnyOrder
构建的错误消息不知道断言失败的根本原因,在我们的例子中它不知道比较差异,它只知道断言失败并生成一般错误消息。
我们已经考虑过支持这个用例,但用当前的错误设计方式实施起来并不简单。
在 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).
这里不能像第一个例子那样得到实际的字段差异吗?
目前不可能,因为为 containsExactlyInAnyOrder
构建的错误消息不知道断言失败的根本原因,在我们的例子中它不知道比较差异,它只知道断言失败并生成一般错误消息。
我们已经考虑过支持这个用例,但用当前的错误设计方式实施起来并不简单。