1、需要javax.servlet-api V3.0以上支持
javax.servlet javax.servlet-api 4.0.0 provided org.springframework spring-test 4.1.6.RELEASE
2、实例
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath*:/spring.xml","classpath*:/springmvc-web.xml"})@WebAppConfigurationpublic class SpringMVCTest { // 模拟request,response private MockHttpServletRequest request; private MockHttpServletResponse response; @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Autowired private ManualTaskController manualTaskController; @Before public void setUp(){ request = new MockHttpServletRequest(); request.setCharacterEncoding("UTF-8"); response = new MockHttpServletResponse(); mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } /** * 使用对象接口方式 */ @Test public void testHeatMap(){ ManualTaskReq req = new ManualTaskReq(); BaseResponse baseResponse = manualTaskController.queryTest(req); System.out.println(baseResponse.getData()); } /** * 使用URL方式 * @throws Exception */ @Test public void testByUrl() throws Exception { ManualTaskReq req = new ManualTaskReq(); req.setTableName("test_table"); req.setStartDate("2018-01-01"); req.setEndDate("2018-11-11"); mockMvc.perform(MockMvcRequestBuilders.post("/manualTask/queryTest.do") .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .content(JSONObject.toJSONString(req))).andDo(MockMvcResultHandlers.print()); }}