import org.junit.runner.RunWith; 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.MvcResult; import org.springframework.test.web.servlet.ResultActions;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath*:/spring-servlet.xml","classpath*:/spring-config.xml"}) @WebAppConfiguration public class BaseJunitTest { protected MockMvc mockMvc; protected ResultActions resultActions; protected MvcResult mvcResult ; }
@Test public void sendJMJKK0104Test() { Map map = new HashedMap(); map.put("app_userID", "254852"); map.put("virtual_card_id", "502C3CA91F65706E6BCDC9941860E980D38C53E34475F4D0CC47122EB3239252"); map.put("qr_code_type", "1"); // 二维码类型 map.put("qr_code_load_type", "1"); // 二维码获取方式 map.put("tranCode", "01");// 健康卡应用业务类别代码
sendPostMvc("访问的路径", map); } private String sendPostMvc(String path,Map params) { try {
ResultActions result = mockMvc.perform(post(path) .contentType(MediaType.APPLICATION_JSON)//验证响应contentType .content(JSONObject.toJSONString(params)));//传递json格式参数 result.andDo(print()); result.andExpect(MockMvcResultMatchers.status().isOk()); String responseString = result.andReturn().getResponse().getContentAsString(); System.out.println("=====客户端获得反馈数据:" + responseString); return responseString; } catch (Exception e) { e.printStackTrace(); } return ""; }
|