SpringBoot - 多種功能測試例子

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

使用說明

導包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'

導包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包,大家自行導入

關鍵核心測試例子

@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上配置相應的參數(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);
    }

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

  • 上一篇文章開頭提到,寫作的意義和價值是不同的。意義由心靈噴發(fā)而出,價值針對某種功能。 今天談談寫作四部曲最后一篇文...
    任游子閱讀 313評論 0 3
  • 該寫些什么呢 我今天除了做運動還是在攢紅包,雖然時間過得很快,快得我都不知道今天都干了些什么,以至于日記里也不知道...
    好好好姑娘閱讀 189評論 0 0
  • 2.12日 周二 多云轉(zhuǎn)陰 慶媽媽第529篇 處于待維修狀態(tài)的電視,今天又重新打開,只為好朋友提醒,每晚八點觀看中...
    史響慶閱讀 114評論 0 0

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