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

相关内容

热门资讯

aapoker挂!aapoke... aapoker挂!aapoker安卓版辅助,(aapOker)竟然有挂,详细教程(有挂教学);1分钟...
透视辅助!约局吧是否有挂,po... 透视辅助!约局吧是否有挂,pokermaster修改器(好像是有挂)1)约局吧是否有挂辅助挂:进一步...
透视有挂!poker红龙辅助,... 透视有挂!poker红龙辅助,aa poker辅助包(竟然是有挂)1、点击下载安装,poker红龙辅...
aapoker外 挂!aa p... aapoker外 挂!aa poker辅助软件,(AAPOker)其实真的有挂,详细教程(有挂辅助挂...
aapoker辅助!德州扑克a... aapoker辅助!德州扑克aa扑克平台,(AaPOKER)其实真的有挂,详细教程(有挂规律);《W...
透视模拟器!智星德州辅助译码插... 透视模拟器!智星德州辅助译码插件靠谱吗,cloudpoker怎么开挂(真是真的是有挂)智星德州辅助译...
aapoker有猫腻!aapo... 此外,数据分析德州(aapoker有猫腻)辅助神器app还具备辅助透视行为开挂功能,通过对客户aap...
透视辅助!德州透视插件,pok... 透视辅助!德州透视插件,pokemmo辅助器手机版下载(原来存在有挂)亲,关键说明,pokemmo辅...
透视软件!约局吧德州有挂吗,德... 透视软件!约局吧德州有挂吗,德州局透视脚本免费版下载手机版(竟然真的是有挂);一、德州局透视脚本免费...
aapoker发牌机制!aap... aapoker发牌机制!aapoker有挂吗,(AAPoker)其实是真的有挂,详细教程(有挂黑科技...