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 属性的属性值,并确保它们都被包含在返回的列表中。

相关内容

热门资讯

九分钟妙计!wepoker黑侠... 九分钟妙计!wepoker黑侠辅助器(透视)一直是有辅助透视(哔哩哔哩)1、下载好wepoker黑侠...
第6分钟要领!wepokerp... 第6分钟要领!wepokerplus作必弊(透视)一直存在有辅助教程(哔哩哔哩)所有人都在同一条线上...
一分钟秘籍!wepoker到底... 一分钟秘籍!wepoker到底有没有透视(透视)确实是真的有辅助开挂(哔哩哔哩)1、在wepoker...
2分钟教材!wepoker透视... 2分钟教材!wepoker透视有吗(透视)一贯有辅助辅助(哔哩哔哩)亲,关键说明,wepoker透视...
第2分钟资料!wepoker分... 第2分钟资料!wepoker分析(透视)好像真的是有辅助插件(哔哩哔哩)1、进入到wepoker分析...
第2分钟步骤!德普之星私人局透... 第2分钟步骤!德普之星私人局透视(透视)一贯是有辅助下载(哔哩哔哩)1、该软件可以轻松地帮助玩家将德...
第九分钟手册!哈糖大菠萝破解器... 第九分钟手册!哈糖大菠萝破解器(透视)确实真的是有辅助插件(哔哩哔哩)1、哈糖大菠萝破解器有没有辅助...
八分钟教材!hhpoker可以... 八分钟教材!hhpoker可以开挂吗(透视)其实是有辅助工具(哔哩哔哩);1、进入到hhpoker可...
九分钟手筋!wepoker手机... 九分钟手筋!wepoker手机版透视脚本(透视)总是真的有辅助教程(哔哩哔哩)1、打开软件启动之后找...
7分钟项目!wpk刷入池率脚本... 7分钟项目!wpk刷入池率脚本(透视)确实真的是有辅助神器(哔哩哔哩)1.wpk刷入池率脚本 选牌创...