springboot @SpringBootTest 단위 테스트 호출 controller 인터페이스

2459 단어 springboot
     get、post、put、delete      
@Test
public void testGetMethod() throws Exception {
   MvcResult result = mockMvc.perform(get("/face/match"))
         .andExpect(status().isOk())//    testRest  get  
         .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))//           text/plain;charset=UTF-8
         .andReturn();//          
   System.out.println(result.getResponse().getContentAsString());
}

/**      */
@Test
public void testAddFace() throws Exception {
   FaceDTO faceDTO = new FaceDTO();
   faceDTO.setGroupId("litsoft07");
   faceDTO.setUserId("xf");
   String image = ImageToBase64Utils.GetImageStrFromPath("C:\\Users\\Administrator\\Desktop\\test.jpg");
   log.info(image);
   faceDTO.setImage(image);
   faceDTO.setImageType(BaiduImageTypeMenu.BASE64.getCode());
   log.info("      :"+JSON.toJSONString(faceDTO));

   MvcResult result = mockMvc.perform(
         post("/face/addFace").contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).content(JSON.toJSONString(faceDTO)))
         .andReturn();
   System.out.println(result.getResponse().getContentAsString());
}

/**    */
@Test
public void testUpateFace() throws Exception {
   FaceDTO faceDTO = new FaceDTO();
   faceDTO.setGroupId("litsoft07");
   faceDTO.setUserId("xf");
   String image = ImageToBase64Utils.GetImageStrFromPath("C:\\Users\\Administrator\\Desktop\\test.jpg");
   log.info(image);
   faceDTO.setImage(image);
   faceDTO.setImageType(BaiduImageTypeMenu.BASE64.getCode());
   log.info("      :"+JSON.toJSONString(faceDTO));
   MvcResult result = mockMvc.perform(
         put("/face/updateFace").contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).content(JSON.toJSONString(faceDTO)))
         .andReturn();
   System.out.println(result.getResponse().getContentAsString());
}

/**    */
@Test
public void testDeleteFace() throws Exception {
   FaceDTO faceDTO = new FaceDTO();
   faceDTO.setGroupId("litsoft07");
   faceDTO.setUserId("xf");
   faceDTO.setFaceToken("11c7a123a10bcbf3bcc46e5ef0a01767");
   log.info("      :"+JSON.toJSONString(faceDTO));
   MvcResult result = mockMvc.perform(
         delete("/face/deleteFace").contentType(MediaType.APPLICATION_JSON_UTF8_VALUE).content(JSON.toJSONString(faceDTO)))
         .andReturn();
   System.out.println(result.getResponse().getContentAsString());
}

좋은 웹페이지 즐겨찾기