Your code works just fine for me. I use Jackson for case analysis and probably the only difference. Check out my code:
Result Class:
public class Res { String version; public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } }
Controller:
@RequestMapping(value = "/blah", method = GET, produces = APPLICATION_JSON_VALUE) public HttpEntity<Res> doIt() { Res res = new Res(); res.setVersion("0.1"); return new HttpEntity<>(res); }
Test:
@Test public void blahTest() throws Exception { this.mockMvc.perform( MockMvcRequestBuilders.get("/blah")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.version").value("0.1")) .andDo(MockMvcResultHandlers.print()); }
Answer:
MockHttpServletResponse: Status = 200 Error message = null Headers = {Content-Type=[application/json;charset=UTF-8]} Content type = application/json;charset=UTF-8 Body = {"version":"0.1"} Forwarded URL = null Redirected URL = null Cookies = []
I can only recommend trying to change the Json parser library you are using. Otherwise, try updating the code with all the parts that are necessary to create a simple, minimal, and reproducible example of your problem.
source share