如何在用自定义对象进行静止的对象验证期间忽略属性
我需要根据 org.json.simple
软件包的预期 jsonobject
验证整个JSON响应,同时忽略
JSONObject jsonObject = new JSONObject();
jsonObject.put("key1", "value1");
jsonObject.put("key2", "value2");
given().post("some/url").then().assertThat().body("result", equalTo(jsonObject, ignore="ignoreKey")) //something like this
API返回响应的响应中的一些其他属性, :
{
"result": {
"key1": "value1",
"key2": "value2",
"ignoreKey": "ignoreValue"
}
}
- 我不在乎密钥/值的顺序/值
- 我都关心密钥和值
- ,我知道我可以使用
jsonpath
获得单个值,但是实际的JSON非常大且复杂,因此我想避免做出这样的多种断言:
response.then().assertThat().body("result.key1", equalTo(jsonObject.get("key1")));
response.then().assertThat().body("result.key2", equalTo(jsonObject.get("key2")));
注意:我正在使用hamcrest匹配器 - 等于()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个想法(来自以前使用放心的人):
1个客户希望SDK yo消耗您的服务。所有行业领导者(Think AWS,Salesforce等)都有工作的SDK,因为它可以节省客户的工作。
2-Having a SDK可以节省您的工作,因为您知道团队正在使用一个共同的库,而不必调试每个团队的其他实施。
3-you可以从开放的API规格中生成功能齐全的客户端/模型(仅放入基本URL),其中有几行:https://medium.com/codex/generating-clients-models-for-restful-services-from-openapi-specification- EDF211E5D761
4-you必须集成。无论如何测试您的API(检查API网关 - > lambda权限),所以为什么不使用生成的客户端(通常与Cucumber Tests这样的东西)。
因此,最终,我将用生成的客户端替换休息,该客户将返回强烈键入的模型。然后,您可以只使用常规的Junit(例如整个对象上的平等,或在单个字段上断言)来验证您的响应是否有意义?
A couple of thoughts here (from someone who used to use rest assured):
1-your clients would like an sdk yo consume your service. All industry leaders (think aws, Salesforce, etc) come with a working sdk, as it saves their clients work.
2-having an sdk saves you work, as you know teams are using a common library rather than having to debug a different implementation per team.
3-you can generate fully functional clients/models (just put in the base url) from your open api spec with a few lines in gradle: https://medium.com/codex/generating-clients-models-for-restful-services-from-openapi-specification-edf211e5d761
4-you have to integration. test your api anyways (check things like api gateway -> lambda permissions) so why not use your generated client (often with something like cucumber tests).
So in the end, I’d replace rest assured with a generated client, which will return strongly typed models. Then you can just use regular junit (such as equals on the whole object, or asserts on a single field) to verify your responses Does that make sense?