spring boot使用TestRestTemplate集成測(cè)試

這篇文章沒(méi)什么技術(shù)含量,只是單純的記錄一下如何用TestRestTemplate訪問(wèn)受security保護(hù)的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以保持狀態(tài)
        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();
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評(píng)論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,253評(píng)論 6 342
  • 內(nèi)存泄漏的產(chǎn)生 Android虛擬機(jī)中把內(nèi)存分為兩部分,一部分為堆空間,里面儲(chǔ)存的是對(duì)象的實(shí)例,需要開發(fā)者主動(dòng)創(chuàng)建...
    童長(zhǎng)老閱讀 386評(píng)論 1 2
  • 這只是一場(chǎng)很普通的愛(ài)戀,每個(gè)人的青春里或許都有,不用自己經(jīng)歷,看見(jiàn)別人的愛(ài)戀,就看到了自己的未來(lái),也許相似,也許大...
    草莓奶油權(quán)閱讀 362評(píng)論 0 1

友情鏈接更多精彩內(nèi)容