SpringBoot - 多種功能測試例子

如何測試SpringBoot的請求?使用spring-boot-starter-test這個包即可完成測試踩麦,SpringBoot項目為什么需要測試的意義大豬就不跟小伙伴們多聊了哈怠褐,重點放在測試代碼上怎茫,我們直接開擼厕诡。

使用說明

導(dǎo)包gradle項目

compile group: 'com.fasterxml.jackson.jaxrs', name:'jackson-jaxrs-xml-provider',version:'2.5.0'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.3.RELEASE'

導(dǎo)包maven項目

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>1.5.3.RELEASE</version>
    <scope>test</scope>
</dependency>

還需要依賴junit包七问,大家自行導(dǎo)入

關(guān)鍵核心測試例子

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
public class BBTestAA {
    @Autowired
    private TestRestTemplate testRestTemplate;

    //Application.class 為SpringBoot的啟動入口類,每個SpringBoot項目大家都會配置
}

GET請求測試

    @Test
    public void get() throws Exception {
        Map<String,String> multiValueMap = new HashMap<>();
        multiValueMap.put("username","lake");//傳值竖伯,但要在url上配置相應(yīng)的參數(shù)
        ActResult result = testRestTemplate.getForObject("/test/get?username={username}",ActResult.class,multiValueMap);
        Assert.assertEquals(result.getCode(),0);
    }

POST請求測試

    @Test
    public void post() throws Exception {
        MultiValueMap multiValueMap = new LinkedMultiValueMap();
        multiValueMap.add("username","lake");
        ActResult result = testRestTemplate.postForObject("/test/post",multiValueMap,ActResult.class);
        Assert.assertEquals(result.getCode(),0);
    }

文件上傳測試

    @Test
    public void upload() throws Exception {
        Resource resource = new FileSystemResource("/home/lake/github/wopi/build.gradle");
        MultiValueMap multiValueMap = new LinkedMultiValueMap();
        multiValueMap.add("username","lake");
        multiValueMap.add("files",resource);
        ActResult result = testRestTemplate.postForObject("/test/upload",multiValueMap,ActResult.class);
        Assert.assertEquals(result.getCode(),0);
    }

文件下載測試

    @Test
    public void download() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.set("token","xxxxxx");
        HttpEntity formEntity = new HttpEntity(headers);
        String[] urlVariables = new String[]{"admin"};
        ResponseEntity<byte[]> response = testRestTemplate.exchange("/test/download?username={1}",HttpMethod.GET,formEntity,byte[].class,urlVariables);
        if (response.getStatusCode() == HttpStatus.OK) {
            Files.write(response.getBody(),new File("/home/lake/github/file/test.gradle"));
        }
    }

請求頭信息傳輸測試

    @Test
    public void getHeader() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.set("token","xxxxxx");
        HttpEntity formEntity = new HttpEntity(headers);
        String[] urlVariables = new String[]{"admin"};
        ResponseEntity<ActResult> result = testRestTemplate.exchange("/test/getHeader?username={username}", HttpMethod.GET,formEntity,ActResult.class,urlVariables);
        Assert.assertEquals(result.getBody().getCode(),0);
    }

PUT

    @Test
    public void putHeader() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.set("token","xxxxxx");
        MultiValueMap multiValueMap = new LinkedMultiValueMap();
        multiValueMap.add("username","lake");
        HttpEntity formEntity = new HttpEntity(multiValueMap,headers);
        ResponseEntity<ActResult> result = testRestTemplate.exchange("/test/putHeader", HttpMethod.PUT,formEntity,ActResult.class);
        Assert.assertEquals(result.getBody().getCode(),0);
    }

DELETE

    @Test
    public void delete() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.set("token","xxxxx");
        MultiValueMap multiValueMap = new LinkedMultiValueMap();
        multiValueMap.add("username","lake");
        HttpEntity formEntity = new HttpEntity(multiValueMap,headers);
        String[] urlVariables = new String[]{"admin"};
        ResponseEntity<ActResult> result = testRestTemplate.exchange("/test/delete?username={username}", HttpMethod.DELETE,formEntity,ActResult.class,urlVariables);
        Assert.assertEquals(result.getBody().getCode(),0);
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末章母,一起剝皮案震驚了整個濱河市母蛛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌乳怎,老刑警劉巖彩郊,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蚪缀,居然都是意外死亡秫逝,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進店門询枚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來违帆,“玉大人,你說我怎么就攤上這事金蜀∷⒑螅” “怎么了?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵渊抄,是天一觀的道長尝胆。 經(jīng)常有香客問我,道長抒线,這世上最難降的妖魔是什么班巩? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上抱慌,老公的妹妹穿的比我還像新娘逊桦。我一直安慰自己,他們只是感情好抑进,可當(dāng)我...
    茶點故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布强经。 她就那樣靜靜地躺著,像睡著了一般寺渗。 火紅的嫁衣襯著肌膚如雪匿情。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天信殊,我揣著相機與錄音炬称,去河邊找鬼。 笑死涡拘,一個胖子當(dāng)著我的面吹牛玲躯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鳄乏,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼跷车,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了橱野?” 一聲冷哼從身側(cè)響起朽缴,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎水援,沒想到半個月后密强,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡裹唆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年誓斥,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片许帐。...
    茶點故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡劳坑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出成畦,到底是詐尸還是另有隱情距芬,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布循帐,位于F島的核電站框仔,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拄养。R本人自食惡果不足惜离斩,卻給世界環(huán)境...
    茶點故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一银舱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧跛梗,春花似錦寻馏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至漾岳,卻和暖如春轰绵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背尼荆。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工左腔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人耀找。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓翔悠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親野芒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容