如何在用自定义对象进行静止的对象验证期间忽略属性

发布于 2025-02-05 11:00:35 字数 879 浏览 3 评论 0 原文

我需要根据 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"
    }
}
  1. 我不在乎密钥/值的顺序/值
  2. 我都关心密钥和值
  3. ,我知道我可以使用 jsonpath 获得单个值,但是实际的JSON非常大且复杂,因此我想避免做出这样的多种断言:
    response.then().assertThat().body("result.key1", equalTo(jsonObject.get("key1")));
    response.then().assertThat().body("result.key2", equalTo(jsonObject.get("key2")));

注意:我正在使用hamcrest匹配器 - 等于()

I need to validate the entire JSON response against an expected JsonObject from org.json.simple package while ignoring some additional properties from the response

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

The response returned from the api is like this:

{
    "result": {
        "key1": "value1",
        "key2": "value2",
        "ignoreKey": "ignoreValue"
    }
}
  1. I do not care about the order of key/values
  2. I care about both keys and values
  3. I know I can get individual values using jsonPath but the actual JSON is quite large and complex so I would like to avoid making multiple assertions like this:
    response.then().assertThat().body("result.key1", equalTo(jsonObject.get("key1")));
    response.then().assertThat().body("result.key2", equalTo(jsonObject.get("key2")));

Note: I am using hamcrest matchers - equalTo()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛上残留的泪 2025-02-12 11:00:35

这里有几个想法(来自以前使用放心的人):

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文