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

相关内容

热门资讯

wepoke软件规律!wopo... wepoke软件规律!wopoker辅助器免费版v2.0,wepoke是什么软件,解密教程(有挂透视...
wepoke透明挂怎么识别!w... 这是一款非常优秀的wepoke透明挂怎么识别 ia辅助检测软件,能够让你了解到wepoke透明挂怎么...
wepoke调控参数!wepo... 《wepoke调控参数!wepoke透明黑科技,wepokeapp下载教程,细节揭秘(有挂教程)》 ...
aapoker有挂吗!aapo... aapoker有挂吗!aapoker外挂实测,aapoker是正规的吗,必赢方法(有挂方法);支持多...
wpk透视辅助可测试真的假的!... wpk透视辅助可测试真的假的!wpk德州职业代打,德州wpk,AA德州教程(有挂技巧);wpk透视辅...
aapoker俱乐部后台可以操... aapoker俱乐部后台可以操作吗!aa poker辅助软件,aapoker发牌规律,透视教程(有挂...
微扑克有ai吗!微扑克俱乐部,... 微扑克有ai吗!微扑克俱乐部,微扑克真的有挂存在吗,2025新版教程(有挂技巧)1、很好的工具软件,...
wpk有德州外挂吗!wpk德州... wpk有德州外挂吗!wpk德州代打是真的吗,线上wpk德州ai机器人,曝光教程(有挂神器)1、很好的...
aapoker有胜率保护吗!a... aapoker有胜率保护吗!aa扑克有什么规律吗,aapoker怎么设置牌局,第三方教程(有挂神器)...
微扑克辅助工具怎么下载!微扑克... 《微扑克辅助工具怎么下载!微扑克有计算器吗,微扑克软件的规律,普及教程(有挂方法)》 微扑克辅助工具...