14

I'm trying to make a simple test using spring boot.

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

Using this import for the jsonPath:

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

I get:

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

If i try to cast it to (ResultMatcher) i get:

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

I'm using the version 2.0.4 of Spring boot. Any ideas of what can be the problem?

Thanks

user2295277
  • 209
  • 1
  • 4
  • 12

1 Answers1

30

try it:

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

I don't know when these changes. see more :

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

xizero
  • 324
  • 3
  • 6