SpringBoot 테스트 통합 테스트

11304 단어 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'
testCompile group: 'junit', name: 'junit', version: '4.10'

maven 프로젝트
<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.10version>
    <scope>testscope>
dependency>
<dependency>
    <groupId>com.fasterxml.jackson.jaxrsgroupId>
    <artifactId>jackson-jaxrs-xml-providerartifactId>
    <version>2.5.0version>
    <scope>testscope>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
    <version>1.5.3.RELEASEversion>
    <scope>testscope>
dependency>

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

    //Application.class  SpringBoot      ,  SpringBoot        
}

테스트 요청
    @Test
    public void get() throws Exception {
        Map multiValueMap = new HashMap<>();
        multiValueMap.put("username","lake");//  ,   url        
        ActResult result = testRestTemplate.getForObject("/test/get?username={username}",ActResult.class,multiValueMap);
        Assert.assertEquals(result.getCode(),0);
    }

테스트 요청
    @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 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 result = testRestTemplate.exchange("/test/getHeader?username={username}", HttpMethod.GET,formEntity,ActResult.class,urlVariables);
        Assert.assertEquals(result.getBody().getCode(),0);
    }

정보 수정
    @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 result = testRestTemplate.exchange("/test/putHeader", HttpMethod.PUT,formEntity,ActResult.class);
        Assert.assertEquals(result.getBody().getCode(),0);
    }

정보 삭제
    @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 result = testRestTemplate.exchange("/test/delete?username={username}", HttpMethod.DELETE,formEntity,ActResult.class,urlVariables);
        Assert.assertEquals(result.getBody().getCode(),0);
    }

좋은 웹페이지 즐겨찾기