dev/스프링

스프링부트3 - 테스트케이스 작성, junit @test작성, AssertJ

아디봉 2024. 2. 2. 16:09

 

Junit 

자바언어를 위한 단위테스트 프레임워크이다. 

단위테스트란, 작성한 코드가 의도대로 작동하는지 작은 단위로 검증하는 것을 의미 

import org.junit.jupiter.api.*;

public class JunitCycleTest {

    @DisplayName("전체 시작전 처음한번, ex) db연결, 테스트환경 초기화")
    @BeforeAll
    static void beforeAll(){
        System.out.println("@BeforeAll");

    }

    @DisplayName("매번실행")
    @BeforeEach
    public void beforeEach(){
        System.out.println("@BeforeEach");
    }

    @Test
    public void test1(){
        System.out.println("Test");
    }

    @Test
    public void test2(){
        System.out.println("test2");
    }

    @Test
    public void test3(){
        System.out.println("test2");
    }

    @DisplayName("종료전 한번만 실행 - db연결종료, 공통적 사용하는 자원 해제시 ")
    @AfterAll
    static void afterAll(){
        System.out.println("@AfterAll");
    }

    @DisplayName("종료전 매번 실행 - 특정데이터 삭제해야 하는 경우 ")
    @AfterEach
    public void afterEach(){
        System.out.println("@AfterEach");
    }

    /*
    * isEqualsTo(A)     A값과 같은지 검증
    * isNotEqualsTo(A)  A값과 다른지 검증
    * contains(A)   A값을 포함하는지 검증
    * doesNotcontain(A)     A값을 포함하지 않는지 검증
    * startWith(A)  접두사가 A인지 검증
    * endsWith(A)   접미사가 A인지 검증
    * isEmpty() 비어있는 값인지 검증
    * isNotEmpty()  비어있지 않은 값인지 검증
    * isPositive()  양수인지 검증
    * isNegative()  음수인지 검증
    * isGreaterThan(1)  1도다 큰값인지 검증
    * isLessThan(1)     1보다 작은지 검증
     */
}

 

위 실행순서는 아래와 같다.

 

@BeforeAll
@BeforeEach
Test
@AfterEach
@BeforeEach
test2
@AfterEach
@BeforeEach
test2
@AfterEach
@AfterAll

 

AssertJ 

junit 와 함께 사용해 검증문의 가독성을 높여주는 라이브러리이다.

소스와 함께 주석을 달아놨으니 같이 보면 이해가 빠르다.

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest //테스트용 애플리케이션 컨텍스트 생성
@AutoConfigureMockMvc //mockMvc를 생성하고 자동으로 구성하는 애너테이션
class HelloControllerTest {

    @Autowired
    protected MockMvc mockMvc;

    @Autowired
    private WebApplicationContext context;

    @Autowired
    private HelloRepository helloRepository;

    @BeforeEach //테스트 실행 전 실행하는 메서드
    public void mockMvoSetUp(){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @AfterEach //테스트 실행 후 실행하는 메서드
    public void cleanUp(){
        helloRepository.deleteAll();
    }

    /*
    * given - when - then패턴
    * given 테스트 실행을 준비하는 단계
    * when 테스트를 진행하는 단계
    * then 결과를 검증하는 단계
    * */

    @DisplayName("회원 조회에 성공한다.")
    @Test
    public void getAllMembers() throws Exception {
        //given
        final String url = "/hello";
        Member savedMember = helloRepository.save(new Member(1L, "손봉진"));

        //when
        //멤버리스트를 조회하는 api를 호출
        //perform - 요청을 전송하는 역할
        //accept - 요철을 보낼때 무슨 타입으로 응답을 받을지 결정하는 메서드
        final ResultActions result = mockMvc.perform(get(url)
                .accept(MediaType.APPLICATION_JSON));

        //then
        //응답코드가 200 OK이고, 반환값은 값 중에 0번째 요소의 id와 name이 저장된 값과 같은지 확인
        //andExpect - 응답을 검증 , isOk를 사용해 응답코드가 OK(200)인지 확인
        //jsonPath("$[0].${필드명}") - json응답값을 가져오는 역할을 하는 메서드
        result.andExpect(status().isOk())
                .andExpect(jsonPath("$[0].id").value(savedMember.getId()))
                .andExpect(jsonPath("$[0].name").value(savedMember.getName()));

        //200 OK            isOk()  http 응답코드가 200ok 인지 검증
        //201 Created       isCreated()     http 응답코드가 201 Created 인지 검증
        //400 Bad           isBadRequest()      http 응답코드가 400 Bad Request 인지 검증
        //403 Forbidden     isForbidden()   http 응답코드가 403 Forbidden인지 검증
        //404 not found     isNotFound()    http응답코드가 404 not found 인지 검증
        //400번대 응답 코드   is4xxclientError    http응답코드가 400번대 응답 코드인지 검증
        //500internal server error  isInternalServerError()        http 응답코드가 500 internal server error인지 거믖ㅇ
        //500번대 응답 코드   is5xxServerError    http응답코드가 500번대 응답코드인지 검증
    }

    

}