我正在使用 MockMVC 测试一个 todo Controller :
mockMvc.perform(MockMvcRequestBuilders.get("/toDos/")
.with(user("user").password("password").roles("ADMIN"))
.content("{ \"saved_date\": \"2010-01-01\"}")
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect((ResultMatcher) jsonPath("$.id").doesNotExist())
.andExpect(status().isOk())
.andExpect( content().contentType("application/json"));
}
我不断收到此错误:
java.lang.ClassCastException: org.springframework.test.web.client.match.JsonPathRequestMatchers$5 cannot be cast to org.springframework.test.web.servlet.ResultMatcher
我想删除对 (ResultMatcher) 的转换,但不知道如何创建一个 ResultMatcher 来测试 Id 的存在。有任何想法吗 ?
最佳答案
我想你想使用:
org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
... 代替:
org.springframework.test.web.client.match.MockMvcResultMatchers.jsonPath
关于spring - JsonPathRequestMatchers 到 ResultMatcher 使用 MockMVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719251/