Spring MVC controller Test - print the result JSON String
The trick is to use andReturn()
MvcResult result = springMvc.perform(MockMvcRequestBuilders .get("/jobsdetails/2").accept(MediaType.APPLICATION_JSON)).andReturn();String content = result.getResponse().getContentAsString();
You can enable printing response of each test method when setting up the MockMvc
instance.
springMvc = MockMvcBuilders.webAppContextSetup(wContext) .alwaysDo(MockMvcResultHandlers.print()) .build();
Notice the .alwaysDo(MockMvcResultHandlers.print())
part of the above code. This way you can avoid applying print handler for each test method.