• 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.  
      }
  • 相关阅读:
    SAP 移动类型 整理
    VB6及VS2005 相关的 树TREE控件,网格控件、电子表格控件、网络图及甘持图控件(项目进度)
    金蝶 PK 用友,第三方评论与自我评价(1)
    谁在开发“工作流”WORKFLOW 产品?
    协同及ERP开发平台,我们如何选择?
    关注“北京广联达软件公司”的项目成本管理系统 !
    一个免费提供的开发平台___"KCOM 商业工程"
    企业 ISO“质量、安全和环境” 三大体系认证的管理系统的开发者 !
    MAXWELL 万胜系统软件公司——为工程建设承包商提供优秀的软件套件!
    Contractor Anywhere (任何地方的承包商)也被 SAGE “赛捷”公司收购 !
  • 原文地址:https://www.cnblogs.com/zgq123456/p/12614545.html
Copyright © 2020-2023  润新知