gpt4 book ai didi

spring-mvc - Spring 测试和JsonPath问题

转载 作者:行者123 更新时间:2023-12-04 05:29:06 25 4
gpt4 key购买 nike

我在测试和json路径时遇到问题

我只是想执行一个简单的测试,并检查id的值:

mockMvc.perform(get("/applications/")).andExpect(status().isOk())
.andDo(print())
.andExpect(content().contentType(TestUtils.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(4)))
.andExpect(jsonPath("$.id",is(1)));

但是我收到类似以下的错误。好像我的代码应该检查id值。由于返回的JSON中有多个项目,我是否不够具体?任何帮助表示赞赏。谢谢。
     Content type = application/json;charset=UTF-8
Body = [{"id":1,"name":"test2"},{"id":2,"name":"test2"}]
Forwarded URL = null
Redirected URL = null
Cookies = []

java.lang.AssertionError: No value at JSON path "$.id", exception: Expected to find an object with property ['id'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:258)
at ...

最佳答案

发布后5分钟,我弄清楚了答案。需要更深入地研究阵列。这有效:

.andExpect(jsonPath("$.[0].id",is(1)));

关于spring-mvc - Spring 测试和JsonPath问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40282942/

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