如何測(cè)試
SpringBoot
的請(qǐng)求?使用spring-boot-starter-test
這個(gè)包即可完成測(cè)試,SpringBoot
項(xiàng)目為什么需要測(cè)試的意義大豬就不跟小伙伴們多聊了哈谜洽,重點(diǎn)放在測(cè)試代碼上,我們直接開擼掉房。
使用說(shuō)明
導(dǎo)包gradle項(xiàng)目
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項(xiàng)目
<!-- 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)鍵核心測(cè)試?yán)?/p>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
public class BBTestAA {
@Autowired
private TestRestTemplate testRestTemplate;
//Application.class 為SpringBoot的啟動(dòng)入口類,每個(gè)SpringBoot項(xiàng)目大家都會(huì)配置
}
GET請(qǐng)求測(cè)試
@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請(qǐng)求測(cè)試
@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);
}
文件上傳測(cè)試
@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);
}
文件下載測(cè)試
@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"));
}
}
請(qǐng)求頭信息傳輸測(cè)試
@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);
}