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

相关内容

热门资讯

让我来分享经验!xpoker辅... 让我来分享经验!xpoker辅助器(辅助挂)原来是真的有挂(的确有挂辅助攻略)1、每一步都需要思考,...
一分钟揭秘!xpoker辅助神... 一分钟揭秘!xpoker辅助神器(辅助挂)好像真的有挂(有挂辅助辅助器)1、脚本辅助下载、免费透视脚...
必看攻略!pokemmo脚本辅... 必看攻略!pokemmo脚本辅助(辅助挂)切实是真的有挂(有挂技巧辅助软件)1、完成有辅助插件,帮助...
总算了解!hhpoker买挂(... 总算了解!hhpoker买挂(辅助挂)竟然存在有挂(有挂方法辅助工具)1、实时透视辅助更新:用户可以...
今日焦点!poker辅助器免费... 今日焦点!poker辅助器免费安装(辅助挂)一直真的是有挂(有挂教程辅助攻略)1、免费脚本咨询教程、...
重大通报!模拟器打开hhpok... 重大通报!模拟器打开hhpoker(辅助挂)真是存在有挂(详细教程辅助脚本)1、点击下载安装,脚本分...
分享个大家!pokerworl... 分享个大家!pokerworld破解版下载(辅助挂)确实存在有挂(有挂方略辅助软件)1、起透看视 辅...
热点推荐!德州局hhpoker... 热点推荐!德州局hhpoker(辅助挂)原来是有挂(有挂方式辅助器)1、点击下载安装,脚本分类一目了...
玩家必看科普!hhpoker德... 玩家必看科普!hhpoker德州挂真的有吗(辅助挂)确实真的是有挂(有挂教学辅助方法)暗藏猫腻,小编...
玩家必看秘籍!wepoker黑... 玩家必看秘籍!wepoker黑侠破解(辅助挂)切实真的有挂(有挂解惑辅助方法)1、有没有辅助教程、有...