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