SpringBoot Test集成測試


如何測試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'
  • 1
  • 2

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM