【问题标题】:Is there a way to assert that response body has required parameters in Json through RestAssured Java有没有办法通过 RestAssured Java 断言响应体在 Json 中有必需的参数
【发布时间】:2019-04-03 09:30:01
【问题描述】:

我需要在放心的 Jave 中断言 Json 的响应体具有必需的参数。
我怎样才能做到这一点,而不是仅仅将 Json 转换为字符串,然后断言字符串是否包含文本,因为该文本也可以是参数的值?

这是示例 Json:

[
    {
        "modificationDate": "2018-12-10T09:39:07Z",
        "startDate": "2018-11-08T04:59:25Z",
        "endDate": "2018-12-10T09:39:07Z"

    },

    {
        "modificationDate": "2008-12-10T09:39:07Z",
        "startDate": "2008-11-08T04:59:25Z",
        "endDate": "2008-12-10T09:39:07Z"

    }
]

那么我如何断言modificationDatestartDateendDate 参数在响应正文中找到。

【问题讨论】:

    标签: java json rest-assured


    【解决方案1】:

    您可以使用Hamcrest.hasKey(K key) 来测试正文中是否存在密钥。

    when().get("....")
        .then().body("$", hasKey("modificationDate"))
               .body("$", hasKey("startDate"))
               .body("$", hasKey("endDate"))
    

    考虑到你得到这样的东西

    {
        "modificationDate": "...",
        "startDate": "",
        "endDate": ""
    }
    

    【讨论】:

    • @Waqas,如果此答案对您有帮助并且对您有用,请将其标记为答案。
    • 我已经尝试过了,但是我在 import static org.hamcrest.Matchers; 处得到“无法解析符号匹配器”;即使我在 POM 中添加了以下内容: org.hamcresthamcrest-junit2.0.0.0test依赖>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多