JsonPathResultMatchers不能应用于ResultMatcher

时间:2018-08-20 19:46:17

标签: java spring-boot spring-boot-test

我正在尝试使用Spring Boot做一个简单的测试。

mockMvc.perform(post("/user")
       .contentType(MediaType.APPLICATION_JSON)
       .content(objectMapper.writeValueAsString(userJohn)))
       .andExpect(jsonPath("$[0].username", is("bob")));

将此导入用于jsonPath:

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

我得到:

andExpect (org.springframework.test.web.servlet.ResultMatcher) in ResultActions cannot be applied to (org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath)

如果我尝试将其转换为(ResultMatcher),则会得到:

java.lang.ClassCastException: org.springframework.test.web.servlet.result.JsonPathResultMatchers cannot be cast to org.springframework.test.web.servlet.ResultMatcher

我正在使用Spring Boot的2.0.4版本。 有什么问题的想法吗?

谢谢

1 个答案:

答案 0 :(得分:7)

尝试:

mockMvc.perform(post("/user")
   .contentType(MediaType.APPLICATION_JSON)
   .content(objectMapper.writeValueAsString(userJohn)))
   .andExpect(jsonPath("$[0].username").value("bob"));

我不知道这些变化何时发生。查看更多:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/servlet/result/JsonPathResultMatchers.html

相关问题