【发布时间】:2012-11-01 23:28:25
【问题描述】:
我正在使用 Spring 的“spring-test-mvc”库来测试 Web 控制器。我有一个非常简单的控制器,它返回一个 JSON 数组。然后在我的测试中我有:
@Test
public void shouldGetAllUsersAsJson() throws Exception {
mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON))
.andExpect(content().mimeType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("fName").exists());
}
以上测试返回:
java.lang.AssertionError: No value for JSON path: fName
为了快速检查我实际得到的结果,我运行了以下测试:
@Test
public void shouldPrintResults() throws Exception {
mockMvc.perform(get("/v1/users").accept(MediaType.APPLICATION_JSON))
.andDo(print());
}
它在MockHttpServletResponse的正文中返回正确的JSON数组
我不确定为什么 jsonPath 在 JSON 数组中看不到 fName。
【问题讨论】:
标签: spring mockito spring-test jsonpath spring-test-mvc