英文:
AssertJ usingRecursiveComparison fails when a class field is a collection
问题
class Person {
private List<Phone> phones;
}
class Phone {
private String number;
}
assertThat(result).usingRecursiveComparison()
.ignoringCollectionOrder()
.isEqualTo(expectedPerson);
`expectedPerson`拥有相同数量的电话和电话号码,但测试失败,因为列表引用不同。如果我记得没错,`usingRecursiveComparison`只会比较值。那么为什么会在这里失败呢?
英文:
class Person {
private List<Phone> phones;
}
class Phone {
private String number;
}
assertThat(result).usingRecursiveComparison()
.ignoringCollectionOrder()
.isEqualTo(expectedPerson);
The expectedPerson
has the same number of phones and phone numbers, but the test fails because the list reference is not the same. If I recall correctly, usingRecursiveComparison
would only compare value. So why here it fails?
答案1
得分: 4
很遗憾,无法复现您的问题。
我猜测可能是一些版本冲突或其他环境问题。例如,以下代码片段按预期工作:
class Person {
private final List<Phone> phones;
Person(List<Phone> phones) {
this.phones = phones;
}
}
class Phone {
private final String number;
Phone(String number) {
this.number = number;
}
}
class PersonTest {
@Test
void samePerson() {
Person expected = new Person(List.of(
new Phone("2"),
new Phone("3"),
new Phone("1")));
Person actual = new Person(List.of(
new Phone("1"),
new Phone("2"),
new Phone("3")));
assertThat(actual).usingRecursiveComparison()
.ignoringCollectionOrder()
.isEqualTo(expected);
}
}
备注:
这是pom.xml
中相关的依赖项:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.17.2</version>
<scope>test</scope>
</dependency>
英文:
Unfortunatelly, was not able to reproduce your problem.
I suppose, it might be some version conflicts or other environment issues. For example, following snippet is working as expected:
class Person {
private final List<Phone> phones;
Person(List<Phone> phones) {
this.phones = phones;
}
}
class Phone {
private final String number;
Phone(String number) {
this.number = number;
}
}
class PersonTest {
@Test
void samePerson() {
Person expected = new Person(List.of(
new Phone("2"),
new Phone("3"),
new Phone("1")));
Person actual = new Person(List.of(
new Phone("1"),
new Phone("2"),
new Phone("3")));
assertThat(actual).usingRecursiveComparison()
.ignoringCollectionOrder()
.isEqualTo(expected);
}
}
Notes:
Here are relevant dependencies from pom.xml
:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.17.2</version>
<scope>test</scope>
</dependency>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论