简体   繁体   中英

Is there a way to assert that response body has required parameters in Json through RestAssured Java

I need to assert in rest-assured Jave that the response body of Json has required parameters.
How can I do that instead of just coverting Json as string and then asserting if string contains a text, as that text can be in vaule of a parameter as well?

Here is that sample 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"

    }
]

So How can I assert that modificationDate , startDate and endDate parameters are found in response body.

You can use Hamcrest.hasKey(K key) to test existence of key in the body.

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

Considering you are getting something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM