Hi I have a Spring mvc controller
@RequestMapping(value = \"/jobsdetails/{userId}\", method = RequestMethod.GET)
@ResponseBody
public List jobsDetai
If you are testing the Controller, you won't get the JSon result, which is returned by the view. Whether you can test the view (or test the controller and then the view), or starting a servlet contrainer (with Cargo for example), and test at HTTP level, which is a good way to check what really happen.
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.
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();
For me it worked when I used the code below:
ResultActions result =
this.mockMvc.perform(post(resource).sessionAttr(Constants.SESSION_USER, user).param("parameter", "parameterValue"))
.andExpect(status().isOk());
String content = result.andReturn().getResponse().getContentAsString();
And it worked !! :D
Hope I can help the other with my answer