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


    这篇文章没什么技术含量,只是单纯的记录一下如何用TestRestTemplate访问受security保护的api,供以后查阅。

    @Slf4j
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment =     SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class AccountControllerTests {
        @Autowired
        private TestRestTemplate restTemplate;
        private HttpEntity httpEntity;
    
        /**
         * 登录
         * @throws Exception
         */
        private void login() throws Exception {
            String expectStr = "{"code":0,"msg":"success"}";
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
            map.add("username", "183xxxxxxxx");
            map.add("password", "123456");
            ResponseEntity responseEntity = restTemplate.postForEntity("/api/account/sign_in", map, String.class);
            //添加cookie以保持状态
            HttpHeaders headers = new HttpHeaders();
            String headerValue = responseEntity.getHeaders().get("Set-Cookie").toString().replace("[", "");
            headerValue = headerValue.replace("]", "");
            headers.set("Cookie", headerValue);
            httpEntity = new HttpEntity(headers);
            assertThat(responseEntity.getBody()).isEqualTo(expectStr);
        }
    
        /**
         * 登出
         * @throws Exception
         */
        private void logout() throws Exception {
            String expectStr = "{"code":0,"msg":"success"}";
            String result = restTemplate.postForObject("/api/account/sign_out", null, String.class, httpEntity);
            httpEntity = null;
            assertThat(result).isEqualTo(expectStr);
        }
    
        /**
         * 获取信息
         * @throws Exception
         */
        private void getUserInfo() throws Exception {
            Detail detail = new Detail();
            detail.setNickname("疯狂的米老鼠");
            detail.setNicknamePinyin("fengkuangdemilaoshu");
            detail.setSex(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            detail.setCreatedAt(sdf.parse("2017-11-03 16:43:27"));
            detail.setUpdatedAt(sdf.parse("2017-11-03 16:43:27"));
            Role role = new Role();
            role.setName("ROLE_USER_NORMAL");
            Set<Role> roles = new HashSet<>();
            roles.add(role);
            User user = new User();
            user.setId(1L);
            user.setPhone("183xxxxxxxx");
            user.setEmail("xxxxxx@gmail.com");
            user.setDetail(detail);
            user.setRoles(roles);
            ResultBean<User> resultBean = new ResultBean<>();
            resultBean.setData(user);
            ObjectMapper om = new ObjectMapper();
            String expectStr = om.writeValueAsString(resultBean);
            ResponseEntity<String> responseEntity = restTemplate.exchange("/api/user/get_user_info", HttpMethod.GET, httpEntity, String.class);
            assertThat(responseEntity.getBody()).isEqualTo(expectStr);
        }
    
        @Test
        public void testAccount() throws Exception {
            login();
            getUserInfo();
            logout();
        }
    

      

  • 相关阅读:
    2015年6月8-9日,杨学明老师《研发多项目管理》专题培训在北京某企业成功举办!
    2015年5月22-23日,杨学明老师《软件测试管理》专题培训在惠州成功举办!
    现代浏览器性能之争
    为libevent添加websocket支持(上)
    使用MUI/html5plus集成微信支付需要注意的几点问题
    在Win32程序中嵌入Edge浏览器组件
    纯中文C++代码,可运行
    修改minifest使桌面软件支持高dpi
    手机操作系统功能对照表
    HTML+JS+JQuery不可以使用status
  • 原文地址:https://www.cnblogs.com/pangguoming/p/10599762.html
Copyright © 2020-2023  润新知