Can AssertJ traverse a JSON API response to verify the path of a node?

huangapple go评论90阅读模式
英文:

Can AssertJ traverse a JSON API response to verify the path of a node?

问题

Hamcrest库有一种非常清晰的方式来遍历JSON API响应,通过使用名为hasJsonPath的方法来验证节点的路径。

assertThat(responseBody, hasJsonPath("$.record.book.author", equalTo("john")));

然而,我在寻找一种类似的方法来使用AssertJ查找路径并进行验证。我原以为以下类似的写法会起作用,但它无法编译通过:

assertThat(responseBody).hasJsonPath("$.record.book.author", equalTo("john"));

AssertJ是否能够遍历节点并验证其存在及其值?

英文:

Hamcrest library has a very clean way of traversing a JSON API response to verify the path of a node by using a method called hasJsonPath

assertThat(responseBody, hasJsonPath("$.record.book.author", equalTo("john")));

However, I am struggling to find a similar way to find a path and verifying it using AssertJ. I was expecting something like this would work, but it won't compile

 assertThat(responseBody).hasJsonPath("$.record.book.author", equalTo("john"));

Can AssertJ traverse a node and verify its existence and its value?

答案1

得分: 3

Prefer using https://github.com/lukas-krecan/JsonUnit over generic assertions library, it has a really nice API to do what you want.

英文:

Prefer using https://github.com/lukas-krecan/JsonUnit over generic assertions library, it has a really nice API to do what you want.

huangapple
  • 本文由 发表于 2020年9月27日 16:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64086206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定