package com.mandi.system.test; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) @Transactional public class Systemcontest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; private RequestMappingHandlerMapping rmhm; @Before public void setup() { // webAppContextSetup 注意上面的static import // webAppContextSetup 构造的WEB容器可以添加fileter 但是不能添加listenCLASS // WebApplicationContext context = // ContextLoader.getCurrentWebApplicationContext(); // 如果控制器包含如上方法 则会报空指针 this.mockMvc = webAppContextSetup(this.wac).build(); } @Test public void test(){ try { MockHttpServletRequestBuilder rq=MockMvcRequestBuilders.post("/admin/modulecon/rolemodule.htm"); rq.param("id", "37").param("pagesize", "15").param("pages", "1").param("tid", "12"); mockMvc.perform(rq).andExpect(status().isOk()).andDo(print()); } catch (Exception e) { e.printStackTrace(); } } }