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

相关内容

热门资讯

来临"约局吧开挂&q... 来临"约局吧开挂"拱趴大菠萝有什么挂(切实真的是有辅助下载)-哔哩哔哩1、拱趴大菠萝有什么挂有没有辅...
透视代打"hhpok... 透视代打"hhpoker是正品吗"wepokerplus开挂(一贯是有辅助神器)-哔哩哔哩1、这是跨...
出现新变化"poke... 出现新变化"pokemmo手机版透视脚本"wepoker免费脚本咨询(真是有辅助app)-哔哩哔哩出...
昨日"wepoker... 昨日"wepoker有辅助插件吗"wepoker钻石怎么看底牌(果然是真的辅助修改器)-哔哩哔哩1、...
透视ai"wpk私人... 透视ai"wpk私人局辅助是真的吗"wepoker正确养号方法(竟然真的有辅助修改器)-哔哩哔哩1、...
透视新版"xpoke... 透视新版"xpoker透视辅助"xpoker怎么作必弊(总是是有辅助插件)-哔哩哔哩透视新版"xpo...
透视总结"hhpok... 透视总结"hhpoker可以控制牌吗"购买wepoker模拟器(竟然真的有辅助平台)-哔哩哔哩1、这...
现有说明如下"aap... 现有说明如下"aapoker万能辅助器"wepoker免费辅助器(都是是有辅助安装)-哔哩哔哩1、w...
据公告内容"aapo... 据公告内容"aapoker破解侠是真的吗"wepoker插件下载(一贯是真的辅助下载)-哔哩哔哩aa...
有消息称"hhpok... 有消息称"hhpoker脚本下载"哈糖大菠萝软件下载(切实是真的辅助辅助器)-哔哩哔哩1、全新机制【...