AssertJ的usingRecursiveComparison在类字段为集合时失败。

huangapple go评论80阅读模式
英文:

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&lt;Phone&gt; 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>

AssertJ的usingRecursiveComparison在类字段为集合时失败。

英文:

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&lt;Phone&gt; phones;

    Person(List&lt;Phone&gt; 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(&quot;2&quot;),
                new Phone(&quot;3&quot;),
                new Phone(&quot;1&quot;)));

        Person actual = new Person(List.of(
                new Phone(&quot;1&quot;),
                new Phone(&quot;2&quot;),
                new Phone(&quot;3&quot;)));

        assertThat(actual).usingRecursiveComparison()
                .ignoringCollectionOrder()
                .isEqualTo(expected);
    }
}

AssertJ的usingRecursiveComparison在类字段为集合时失败。
Notes:
Here are relevant dependencies from pom.xml:

    &lt;dependency&gt;
      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
      &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
      &lt;version&gt;5.4.2&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
      &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
      &lt;version&gt;5.4.2&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.assertj&lt;/groupId&gt;
      &lt;artifactId&gt;assertj-core&lt;/artifactId&gt;
      &lt;version&gt;3.17.2&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年10月22日 13:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64476078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定