简体   繁体   中英

Integration Testing Spring boot application JSON Assert

I have spring boot application was trying to write Integration testing but when i ht the in memory DB i get one value from that but when do ASSERT TO MATCH THE PARAMETER VALUES i AM NOT ABLE TO COMPARE THE SIZE AND VALUES

these how the response when i hit the api in Postman

{
    "continuationToken": "09/07/19 05:21 PM",
    "permit": [
        {
            "expiry": null,
            "activation": "2019-07-01T06:00:00.000+0000",
            "permitId": "C8S43N5",
            "plateNumber": "ERT1234",
            "plateState": "AB"
        },
        {
            "expiry": null,
            "activation": "2018-12-17T22:04:13.947+0000",
            "permitId": "CGW3TNF",
            "plateNumber": "FDF",
            "plateState": "AB"
        }
  ]
}
        Assert.assertThat(response.getStatusCode(), Matchers.equalTo(HttpStatus.OK));
        String jsonBody = response.getBody();
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$[0].expiry", Matchers.equalTo("2080-06-26T06:00:00.000+0000")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$[0].activation", Matchers.equalTo("2019-06-26T22:33:14.849+0000")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$[0].permitId", Matchers.equalTo("CS383UA")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$[0].plateNumber", Matchers.equalTo("VGZ05")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$[0].plateState", Matchers.equalTo("AB")));

java.lang.AssertionError: Expected: is json with json path "$[0]['expiry']" evaluated to "2080-06-26T06:00:00.000+0000" but: json path "$[0]['expiry']" was not found in <{continuationToken=09/07/19 05:31 PM, permit=[{"expiry":"2080-06-26T06:00:00.000+0000","activation":"2019-06-26T22:33:14.849+0000","permitId":"CS383UA","plateNumber":"VGZ05","plateState":"AB"}]}>

The array is under permit key, so change your code

       Assert.assertThat(response.getStatusCode(), Matchers.equalTo(HttpStatus.OK));
        String jsonBody = response.getBody();
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$.permit[0].expiry", Matchers.equalTo("2080-06-26T06:00:00.000+0000")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$.permit[0].activation", Matchers.equalTo("2019-06-26T22:33:14.849+0000")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$.permit[0].permitId", Matchers.equalTo("CS383UA")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$.permit[0].plateNumber", Matchers.equalTo("VGZ05")));
        Assert.assertThat(jsonBody, JsonPathMatchers.hasJsonPath("$.permit[0].plateState", Matchers.equalTo("AB")));

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