gpt4 book ai didi

java - 集成测试 Spring boot 应用程序 JSON 断言

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:14 26 4
gpt4 key购买 nike

我有 Spring Boot 应用程序正在尝试编写集成测试,但是当我访问内存数据库时,我从中得到一个值,但是何时断言以匹配参数值我无法比较大小和值

当我在 Postman 中点击 api 时,这些响应是如何的

{
"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"}]}>

最佳答案

该数组位于 permit 键下,因此请更改您的代码

       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")));

关于java - 集成测试 Spring boot 应用程序 JSON 断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56958031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com