spring boot使用TestRestTemplate集成测试 RESTful 接口

只是单纯的记录一下如何用TestRestTemplate访问受security保护的api,供以后查阅。

  1.  
    @Slf4j
  2.  
    @RunWith(SpringRunner.class)
  3.  
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  4.  
    public class AccountControllerTests {
  5.  
    @Autowired
  6.  
    private TestRestTemplate restTemplate;
  7.  
    private HttpEntity httpEntity;
  8.  
     
  9.  
    /**
  10.  
    * 登录
  11.  
    * @throws Exception
  12.  
    */
  13.  
    private void login() throws Exception {
  14.  
    String expectStr = "{"code":0,"msg":"success"}";
  15.  
    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
  16.  
    map.add("username", "183xxxxxxxx");
  17.  
    map.add("password", "123456");
  18.  
    ResponseEntity responseEntity = restTemplate.postForEntity("/api/account/sign_in", map, String.class);
  19.  
    //添加cookie以保持状态
  20.  
    HttpHeaders headers = new HttpHeaders();
  21.  
    String headerValue = responseEntity.getHeaders().get("Set-Cookie").toString().replace("[", "");
  22.  
    headerValue = headerValue.replace("]", "");
  23.  
    headers.set("Cookie", headerValue);
  24.  
    httpEntity = new HttpEntity(headers);
  25.  
    assertThat(responseEntity.getBody()).isEqualTo(expectStr);
  26.  
    }
  27.  
     
  28.  
    /**
  29.  
    * 登出
  30.  
    * @throws Exception
  31.  
    */
  32.  
    private void logout() throws Exception {
  33.  
    String expectStr = "{"code":0,"msg":"success"}";
  34.  
    String result = restTemplate.postForObject("/api/account/sign_out", null, String.class, httpEntity);
  35.  
    httpEntity = null;
  36.  
    assertThat(result).isEqualTo(expectStr);
  37.  
    }
  38.  
     
  39.  
    /**
  40.  
    * 获取信息
  41.  
    * @throws Exception
  42.  
    */
  43.  
    private void getUserInfo() throws Exception {
  44.  
    Detail detail = new Detail();
  45.  
    detail.setNickname("疯狂的米老鼠");
  46.  
    detail.setNicknamePinyin("fengkuangdemilaoshu");
  47.  
    detail.setSex(1);
  48.  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  49.  
    detail.setCreatedAt(sdf.parse("2017-11-03 16:43:27"));
  50.  
    detail.setUpdatedAt(sdf.parse("2017-11-03 16:43:27"));
  51.  
    Role role = new Role();
  52.  
    role.setName("ROLE_USER_NORMAL");
  53.  
    Set<Role> roles = new HashSet<>();
  54.  
    roles.add(role);
  55.  
    User user = new User();
  56.  
    user.setId(1L);
  57.  
    user.setPhone("183xxxxxxxx");
  58.  
    user.setEmail("xxxxxx@gmail.com");
  59.  
    user.setDetail(detail);
  60.  
    user.setRoles(roles);
  61.  
    ResultBean<User> resultBean = new ResultBean<>();
  62.  
    resultBean.setData(user);
  63.  
    ObjectMapper om = new ObjectMapper();
  64.  
    String expectStr = om.writeValueAsString(resultBean);
  65.  
    ResponseEntity<String> responseEntity = restTemplate.exchange("/api/user/get_user_info", HttpMethod.GET, httpEntity, String.class);
  66.  
    assertThat(responseEntity.getBody()).isEqualTo(expectStr);
  67.  
    }
  68.  
     
  69.  
    @Test
  70.  
    public void testAccount() throws Exception {
  71.  
    login();
  72.  
    getUserInfo();
  73.  
    logout();
  74.  
    }
原文地址:https://www.cnblogs.com/zgq123456/p/12614545.html