@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration//注解该注解之后才能获取@Autowired的context @ContextConfiguration({"classpath:application.xml","classpath:springmvc.xml"})//添加配置 public class SpringTestContorllerTest { @Autowired WebApplicationContext context; MockMvc mockMvc; @Before public void initMockMvc(){ mockMvc= MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testLogin() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/modelAttribute").param("pwd", "123") .param("pwd", "123456")).andReturn(); MockHttpServletRequest request = mvcResult.getRequest(); MockHttpServletResponse response = mvcResult.getResponse(); } }
待续:spring -test