Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonPathResultMatchers cannot be applied to ResultMatcher

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

like image 917
user2295277 Avatar asked Aug 20 '18 19:08

user2295277


1 Answers

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

like image 121
xizero Avatar answered Sep 28 '22 03:09

xizero