MockMvc操作总结版权声明

原创
小哥 3年前 (2022-11-11) 阅读数 44 #大杂烩

1.

protected MockMvc mockMvc;

@Autowired protected WebApplicationContext wac;

@Before //此方法在每个方法执行之前执行一次。 public void setMockMvc() { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象 }

@Test public void delete() throws Exception{ String responseString = mockMvc.perform(MockMvcRequestBuilders.get("/w/res/delete").contentType( MediaType.APPLICATION_FORM_URLENCODED).param("id","10111111") ).andExpect(status().isOk()).andDo(print()).andReturn().getResponse().getContentAsString(); System.out.println("哈哈哈"+responseString);//在Controller 中加 @ResponseBody 您可以输出想要返回的内容。

}

2.

@Test public void preUpdate() throws Exception{ MvcResult responseString = mockMvc.perform(MockMvcRequestBuilders.get("/w/res/preUpdate/20721").contentType( MediaType.APPLICATION_FORM_URLENCODED).param("id","11111") ).andExpect(status().isOk()).andDo(print()).andReturn();//.getResponse().getContentAsString(); System.out.println("测试结果--------"+responseString.getModelAndView());//在Controller 中加 @ResponseBody 您可以输出想要返回的内容。 }

responseString.getModelAndView()你得到的是modelview

responseString.getModelAndView().getModel()你得到的是ModelMap

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门