【问题标题】:Sorting array before compare in AssertJ在 AssertJ 中比较之前对数组进行排序
【发布时间】:2020-01-28 23:38:16
【问题描述】:

我使用 assertJ 比较两个对象。我有从我们的应用程序和预期对象输出的实际对象。该对象具有字段,该字段是其他对象的数组,但该数组中的元素以随机顺序存在,因此有时测试通过但也经常失败,因为比较了数组的不正确元素。

我在做什么我从两个对象中获取数组对它们进行排序,然后设置为对象,但这看起来很难看,并且使用了 7 行代码。

Comparator.comparing(Value::getId)

Value[] actualOrderChildren = actualOrder.getChildren();
Arrays.sort(actualOrderChildren, comparing);
actualOrder.setChildren(actualOrderChildren);

Value[] expectedOrderChildren = expectedOrder.getChildren();
Arrays.sort(expectedOrderChildren, comparing);
expectedOrder.setChildren(expectedOrderChildren);

assertThat(actualOrder).isEqualToComparingFieldByFieldRecursively(expectedOrder);

有没有更好看的使用 assertj 流利断言的解决方案?

【问题讨论】:

标签: java assertj


【解决方案1】:

containsExactlyInAnyOrder 可能是您正在寻找的。​​p>

assertThat(actualOrder.getChildren()).containsExactlyInAnyOrder(expectedOrder.getChildren());

【讨论】:

    猜你喜欢
    • 2015-10-07
    • 2019-07-23
    • 1970-01-01
    • 2018-03-30
    • 2018-12-12
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多