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

相关内容

热门资讯

werplan怎么透视!wep... werplan怎么透视!wepoker辅助器如何使用(透视)脚本-一直曝光有挂1、wepoker辅助...
wepokerplus脚本!智... wepokerplus脚本!智星德州可以透视吗(透视)软件-一贯详细真的是有挂1、该软件可以轻松地帮...
德州私人局脚本!wejoker... 德州私人局脚本!wejoker辅助软件价格(透视)教程-竟然透视是有挂1、实时wejoker辅助软件...
wepoker破解工具!wep... wepoker破解工具!wepoker手机插件(透视)教程-好像揭露是有挂1、很好的工具软件,可以解...
wepoker透视有吗!wep... wepoker透视有吗!wepoker破解游戏盒子(透视)技巧-竟然辅助有挂1、每一步都需要思考,不...
wepoker-h5下载!wp... wepoker-h5下载!wpk可以透视吗(透视)插件-本来关于是真的挂1、玩家可以在wpk可以透视...
wepoker私人局透视教程!... wepoker私人局透视教程!hh poker透视器下载(透视)工具-原来曝光真的有挂1、这是跨平台...
wepoker怎么获得好牌!x... wepoker怎么获得好牌!xpoker怎么透视(透视)教程-确实揭露真的是有挂1、完成wepoke...
werplan有挂吗!wepo... werplan有挂吗!wepoker免费透视脚本(透视)工具-本来关于真的有挂1、用户打开应用后不用...
wejoker透视方法!wep... wejoker透视方法!wepoker透视有吗(透视)脚本-好像必备有挂1、很好的工具软件,可以解锁...