强>
@WebAppConfiguration @RunWith (SpringJUnit4Cla * * * unner.class) @ContextConfiguration (locations =,{,“类路径:spring/中”,})
@Test public void 测试类(),throws Exception { ResultActions 反应=this.mockMvc.perform (MockMvcRequestBuilders.get(“/服务/测试/testController”) .accept (MediaType.APPLICATION_JSON)//返回值接收json .header(“时间戳”,“1496656373783”) .header (" AppId ", " 1003 ")); .isOk reaction.andExpect (MockMvcResultMatchers.status () ()); MvcResult MvcResult =reaction.andReturn (); .getContentAsString System.out.println (mvcResult.getResponse () ()); }
@Test public void 测试类(),throws Exception { PolicyInfoRequest 请求=new PolicyInfoRequest (); request.setAnnualPremium (100); request.setPolicyNo(“测试- 222”); request.setPolicyRebate(0.28度); request.setPolicyType (1); request.setRebateAmount f (28); String jsonRequest=JSON.toJSONString(请求); ResultActions reaction =this.mockMvc.perform (MockMvcRequestBuilders.post(“/政策/信息/保存”) .contentType (MediaType.APPLICATION_JSON)//请求体时json .header(“时间戳”,“1496656373791”) .header (“AppId”,“1003”) .content (jsonRequest)); .isOk reaction.andExpect (MockMvcResultMatchers.status () ()); MvcResult MvcResult =reaction.andReturn (); .getContentAsString System.out.println (mvcResult.getResponse () ()); }
以上两个例子就能基本覆盖解决使用springtest的MockMvc对控制器进行单元测试的需求。