spring boot使用TestRestTemplate集成测试

这篇文章没什么技术含量,只是单纯的记录一下如何用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();
    }

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 164,589评论 24 698
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 129,323评论 18 137
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 43,317评论 6 343
  • 内存泄漏的产生 Android虚拟机中把内存分为两部分,一部分为堆空间,里面储存的是对象的实例,需要开发者主动创建...
    千年竹妖阅读 258评论 1 2
  • 这只是一场很普通的爱恋,每个人的青春里或许都有,不用自己经历,看见别人的爱恋,就看到了自己的未来,也许相似,也许大...
    草莓奶油权阅读 194评论 0 1