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

相关内容

热门资讯

透视解密!pokemmo脚本手... 透视解密!pokemmo脚本手机版,epoker底牌透视-本来是有辅助方法(哔哩哔哩)1、pokem...
透视了解!德州hhpoker是... 透视了解!德州hhpoker是真的吗,hhpkoer辅助挂是真的吗-原来真的有辅助方法(哔哩哔哩)1...
透视曝光!hhpoker透视方... 透视曝光!hhpoker透视方法,hhpoker有没有作弊辅助-本来真的有辅助神器(哔哩哔哩)1、金...
透视详情!wpk是真的还是假的... 透视详情!wpk是真的还是假的,wpk真的有透视嘛-本来存在有辅助工具(哔哩哔哩)1、实时透视辅助更...
透视揭幕!购买的wpk辅助在哪... 透视揭幕!购买的wpk辅助在哪里下载,wpk软件是真的吗-真是一直总是有辅助插件(哔哩哔哩)是不是有...
透视分享!wepoker有没有... 透视分享!wepoker有没有挂,wepoker数据分析-一贯是真的有辅助脚本(哔哩哔哩)1、免费脚...
透视了解!wepoker私人局... 透视了解!wepoker私人局有透视吗,WePoKer辅助器-确实一直都是有辅助教程(哔哩哔哩)1、...
透视辅助!wpk真的有透视嘛,... 透视辅助!wpk真的有透视嘛,wpk真吗-本来是真的有辅助app(哔哩哔哩)1、透视辅助!wpk真的...
透视详情!wepoker私人局... 透视详情!wepoker私人局辅助器怎么用,wepokerh5破解-确实一直都是有辅助工具(哔哩哔哩...
透视详情!智星德州插件2024... 透视详情!智星德州插件2024最新版,pokemmo脚本-一直真的有辅助脚本(哔哩哔哩)1、上手简单...