Spring MVC controller Test - print the result JSON String Spring MVC controller Test - print the result JSON String json json

Spring MVC controller Test - print the result JSON String


Try this code:

resultActions.andDo(MockMvcResultHandlers.print());


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.