使用 jsonPath 的 Junit 测试没有找到匹配的内容
Junit test with jsonPath doesn't find the matching content
我有这个简单的单元测试:-
@Test
void getCityName() throws Exception {
when(weatherService.getWeatherByCity(capitalize(TestData.getWeather().getCity())))
.thenReturn(TestData.getWeatherList());
mockMvc.perform(get(WeatherController.WEATHER+WeatherController.GET_WEATHER_BY_CITY+"/espoo"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
/* it doesn't work if uncommented.
.andExpect(jsonPath("$.city")
.value("espoo"))
*/
.andDo(MockMvcResultHandlers.print());
}
.andDo(MockMvcResultHandlers.print());
打印出来:-
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json"]
Content type = application/json
Body = [{"id":null,"city":"espoo","country":null,"description":null,"currentTemp":0.0,"feelsLike":3.3,"minTemp":2.2,"maxTemp":5.5,"tempLimit":3.0,"frequency":5,"frequencyUnit":"SECOND","uri":"https://api.openweathermap.org/data/2.5/weather?q=espoo&units=metric&APPID=5536a9b0c84d081997982254c24fc53a"}]
Forwarded URL = null
Redirected URL = null
Cookies = []
我可以看到有 "city":"espoo"
。我如何匹配这个。我累了:-
.andExpect(jsonPath("$.city")
.value("espoo"))
它给出错误:-
DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - 已完成 200 OK
DEBUG com.jayway.jsonpath.internal.path.CompiledPath - 评估路径:$.size()
DEBUG com.jayway.jsonpath.internal.path.CompiledPath - 评估路径:$['city']
java.lang.AssertionError:JSON 路径“$.city”中没有值
响应类型为数组:
[{"id":null,"city":"espoo","country":null,"description":null,"currentTemp":0.0,"feelsLike":3.3,"minTemp":2.2,"maxTemp":5.5,"tempLimit":3.0,"frequency":5,"frequencyUnit":"SECOND","uri":"https://api.openweathermap.org/data/2.5/weather?q=espoo&units=metric&APPID=5536a9b0c84d081997982254c24fc53a"}]
因此您需要先选择索引,然后选择属性 $[0].city
.andExpect(jsonPath("$[0].city", is(espoo)))
=== 已编辑 ===
请尝试:
.andExpect(jsonPath("$[0].city").value("espoo"));
我有这个简单的单元测试:-
@Test
void getCityName() throws Exception {
when(weatherService.getWeatherByCity(capitalize(TestData.getWeather().getCity())))
.thenReturn(TestData.getWeatherList());
mockMvc.perform(get(WeatherController.WEATHER+WeatherController.GET_WEATHER_BY_CITY+"/espoo"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
/* it doesn't work if uncommented.
.andExpect(jsonPath("$.city")
.value("espoo"))
*/
.andDo(MockMvcResultHandlers.print());
}
.andDo(MockMvcResultHandlers.print());
打印出来:-
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json"]
Content type = application/json
Body = [{"id":null,"city":"espoo","country":null,"description":null,"currentTemp":0.0,"feelsLike":3.3,"minTemp":2.2,"maxTemp":5.5,"tempLimit":3.0,"frequency":5,"frequencyUnit":"SECOND","uri":"https://api.openweathermap.org/data/2.5/weather?q=espoo&units=metric&APPID=5536a9b0c84d081997982254c24fc53a"}]
Forwarded URL = null
Redirected URL = null
Cookies = []
我可以看到有 "city":"espoo"
。我如何匹配这个。我累了:-
.andExpect(jsonPath("$.city")
.value("espoo"))
它给出错误:-
DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - 已完成 200 OK DEBUG com.jayway.jsonpath.internal.path.CompiledPath - 评估路径:$.size() DEBUG com.jayway.jsonpath.internal.path.CompiledPath - 评估路径:$['city']
java.lang.AssertionError:JSON 路径“$.city”中没有值
响应类型为数组:
[{"id":null,"city":"espoo","country":null,"description":null,"currentTemp":0.0,"feelsLike":3.3,"minTemp":2.2,"maxTemp":5.5,"tempLimit":3.0,"frequency":5,"frequencyUnit":"SECOND","uri":"https://api.openweathermap.org/data/2.5/weather?q=espoo&units=metric&APPID=5536a9b0c84d081997982254c24fc53a"}]
因此您需要先选择索引,然后选择属性 $[0].city
.andExpect(jsonPath("$[0].city", is(espoo)))
=== 已编辑 ===
请尝试:
.andExpect(jsonPath("$[0].city").value("espoo"));