I have a controller mapping
@RequestMapping(value = "/something", method = RequestMethod.GET)
public String get(@RequestParam("id") Person aPerson, Model aModel) {
aModel.addAttribute("person", aPerson);
return "index";
}
How do I go about testing this through MockMvc?
I can do something like this
mockMvc.perform(get("/something?id=1")).andExpect(status().is2xxSuccessful());
But this will not work since the RequestParam is an object, not a String. The conversion is done by Spring but I'm unit testing the method and do not want to start up the application context. How do I unit test something like this with MockMvc?
you may try something like
@Test
public void test() throws Exception {
Model model= new Model();
mockMvc.perform(MockMvcRequestBuilders.get("/something?id=1")
.content(asJsonString(model))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)).andExpect(status().is2xxSuccessful());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.