AssertJ: 指定嵌套列表的排序顺序
创始人
2024-09-20 16:00:23
0

在AssertJ中,可以使用usingRecursiveFieldByFieldElementComparator方法来指定嵌套列表的排序顺序。以下是一个示例代码:

import org.assertj.core.util.Lists;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class NestedListSortingExample {

    public static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }
    }

    public static void main(String[] args) {
        // 创建两个Person对象
        Person person1 = new Person("Alice", 25);
        Person person2 = new Person("Bob", 30);

        // 创建两个嵌套列表,其中每个列表包含两个Person对象
        List> nestedList1 = Arrays.asList(
                Arrays.asList(person1, person2),
                Arrays.asList(person1, person2)
        );
        List> nestedList2 = Arrays.asList(
                Arrays.asList(person2, person1),
                Arrays.asList(person2, person1)
        );

        // 使用AssertJ进行断言
        assertThat(nestedList1)
                .usingRecursiveFieldByFieldElementComparator() // 使用递归字段比较器
                .isEqualTo(nestedList2); // 断言两个嵌套列表相等

        // 如果排序顺序不同,断言将会失败
        assertThatThrownBy(() ->
                assertThat(nestedList1)
                        .usingRecursiveFieldByFieldElementComparator()
                        .isEqualTo(Lists.newArrayList(nestedList2.get(1), nestedList2.get(0))))
                .isInstanceOf(AssertionError.class);
    }
}

在上面的示例中,我们创建了两个Person对象并将它们放入两个嵌套列表中。然后,我们使用usingRecursiveFieldByFieldElementComparator方法来指定比较器,该比较器将递归地按字段进行比较。最后,我们使用isEqualTo方法来断言两个嵌套列表是否相等。

如果排序顺序不同,断言将会失败,并抛出AssertionError异常。

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...