본문 바로가기

IT/개발

spring junit void test

맨 처음 테스트 케이스를 작성해본지 얼마지나지 않았을때
void test에 대해서 부딪힌적이 있습니다.
보통의 테스트는 

List<Student> student = service.findAll(1);
assertThat(list, is(notNullValue()));
assertThat(list.size(), is(6));
assertThat(list.get(1).getId(), is(1));

이런식으로 서비스단을 호출해서 얻은 결과를 가지고 정합성을 검사하는식인데
void 테스트의 경우 어떻게 테스트를 해야되는지 감이 안잡혔습니다.
검색해본결과 junit에서 지원해주는 ExpectedException class를 이용하면 가능했습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class ServiceTest{
    @Rule
    public ExpectedException expectedException = ExpectedException.none();
 
    @Test
    public void 체험기간정보_유효하지_않을때() {
        ApiResponse<CommonRes<ClientRes>> res = new ApiResponse<>(200"",
               CommonRes.<ClientRes>builder()
                       .list(Collections.singletonList(ClientRes.builder().result_de("2020-10-12 15:18:31").build()))
                        .total(1)
                        .build());
 
       given(userService.getUserInfo(anyInt())).willReturn(UserInfo.builder().build());
       given(client.getClient(anyString(), anyString(), anyInt(), anyInt())).willReturn(res);
       given(authService.getAuthInfo(anyInt())).willReturn(AuthInfo.builder().orderId(1L).build());
       given(infoService.getInfo(anyLong())).willReturn(LearnInfo.builder()
                .startDt(null)
                .endDt(null)
                .deviceNo(1)
                .build());
 
       ValidationService validation = new ValidationService(client, authService, infoService, userService);
 
        expectedException.expect(ValidationException.class);
        expectedException.expectMessage("체험기간에 대한 정보가 없습니다.");
 
       validation.validate(654654);
}
cs

 

'IT > 개발' 카테고리의 다른 글

spring test spy 활용하기  (0) 2021.03.04
JAVA 8 groupingBy, mapping, collectingAndThen  (0) 2021.01.19
spring file delete  (0) 2020.10.29
spring 파일 다운로드  (0) 2020.10.28
spring 파일 업로드  (0) 2020.10.27