AssertJ中的filteredOn在assertk中有什么等价物?
创始人
2024-09-20 16:00:43
0次

assertk 提供了枚举类 KFunction,可以使用该类的属性来获取对象列表中指定字段的属性值并进行断言。举个例子,假设现在有一个 Book 类,包含 title 和 author 两个属性,我们要对 title 进行断言。

在 AssertJ 中,可以使用 filteredOn 方法:

List books = Arrays.asList(
    new Book("The Lord of the Rings", "J. R. R. Tolkien"),
    new Book("The Hobbit", "J. R. R. Tolkien"),
    new Book("Harry Potter and the Philosopher's Stone", "J. K. Rowling")
);

assertThat(books).filteredOn(book -> book.getAuthor().equals("J. R. R. Tolkien"))
                  .extracting(Book::getTitle).containsOnly("The Lord of the Rings", "The Hobbit");

在 assertk 中,可以使用枚举类 KFunction:

val books = listOf(
    Book("The Lord of the Rings", "J. R. R. Tolkien"),
    Book("The Hobbit", "J. R. R. Tolkien"),
    Book("Harry Potter and the Philosopher's Stone", "J. K. Rowling")
)

assertThat(books).hasSize(3)
                 .extracting { it[KFunction(Book::author)] } // 获取 Book 列表中 author 属性的值
                 .containsOnly("J. R. R. Tolkien", "J. R. R. Tolkien", "J. K. Rowling")

在上面的例子中,我们可以使用 KFunction(Book::author) 获取 Book 类中 author 属性的属性值,并确保它们都被包含在返回的列表中。

相关内容

热门资讯

WPK工具窍门!红龙poker... WPK工具窍门!红龙poker辅助工具,红龙PoKerai多功能透视教程WPK工具窍门!红龙poke...
WPK方法手册!aapoker... WPK方法手册!aapoker破解侠是真的吗,aapokerai多功能辅助软件aapoker破解侠是...
WPK链接方式!wpk私人局辅... WPK链接方式!wpk私人局辅助是真的吗,智星菠萝德州ai多功能辅助插件1、wpk私人局辅助是真的吗...
WPK模拟器指南书!来玩app... WPK模拟器指南书!来玩app破解,WPKai多功能辅助器攻略1、来玩app破解辅助软件下载优化,来...
WPK多开手段!wepoker... WPK多开手段!wepoker透视有吗,红龙PoKerai多功能辅助神器1、全新机制【wepoker...
WPK辅助法门!AApoker... WPK辅助法门!AApoker辅助插件,德州扑克ai多功能透视攻略1、下载好AApoker辅助插件透...
WPK方法策略!AApoker... WPK方法策略!AApoker设置,WPKai多功能透视软件1、金币登录送、破产送、升级送、活动送。...
WPK多开模块!佛手在线大菠萝... WPK多开模块!佛手在线大菠萝辅助,wpkai多功能开挂技巧1、任何佛手在线大菠萝辅助透视是真的假的...
WPK作弊步骤!AApoker... WPK作弊步骤!AApoker万能,德扑之星ai多功能透视神器运AApoker万能辅助工具,进入游戏...
WPK模拟器操作!德扑辅助器怎... WPK模拟器操作!德扑辅助器怎么用,德州扑克ai多功能透视插件1、玩家可以在德扑辅助器怎么用透视最简...