inblog logo
|
zerfa
    springboot

    CRUD V1-08. JUNIT 단위 테스트(2)

    kimsoapsoap's avatar
    kimsoapsoap
    Aug 26, 2024
    CRUD V1-08. JUNIT 단위 테스트(2)
    Contents
    1. 테스트 설정2. 테스트 진행
    💡
    given, when, then으로 테스트를 진행 해볼 것이다 기존의 given, when, see 와 마지막 부분이 다른데 see는 눈으로. 즉 콘솔로 확인했다면 then은 코드로 검증할 것이다. //given 단위테스트의 메서드는 매개변수를 못 받기 때문에 필요한 변수들 설정 //when 코드 실행 //then 코드로 확인
     
     

    1. 테스트 설정

    given, when, then으로 나눠서 검증할 것이다.
     
    이때 then의 Assertions은 org.assertj.core.api 패키지의 Assertions 를 import 해준다.(더 많은 기능)
    notion image
     
     
    @Test public void save_test() { // given String title = "제목1"; String content = "내용1"; // when boardRepository.save(title, content); //then Assertions.assertThat(boardRepository.findById(1).getTitle()).isEqualTo(title); Assertions.assertThat(boardRepository.findById(1).getContent()).isEqualTo(content); }
     
    Assertions.assertThat(A).isEqulTo(B) : A(실제)와 B(예상)가 같느냐 하는 것이다.
    일치하면 테스트 통과이다.
     
    given에서 변수 설정을 해줬고
    when에서 게시글 저장하는 코드를 설정
    then에서 저장한 게시글을 조회했을 때 우리가 넣은 내용과 일치하는지 확인했다.
     
     
     
     
     

    2. 테스트 진행

     

    2-1. 테스트 성공

    위의 설정대로 테스트하면 성공이다.
    notion image
     
     
     

    2-2. 테스트 실패

    테스트는 성공도 중요하지만 의도한 실패로 검증이 잘 되나 확인 하는 것이 매우 중요하다.
     
    예상하는 값을 제목없음 으로 테스트해보면
    예상 값은 제목없음 인데
    실제 값은 제목1 이라서 테스트 실패가 나온다.
     
    notion image
     
     
    기능 추가시 가능하면 Assertions.assertThat()으로 코드로 검증하면서 넘어가면 좋다.
    Share article
    Contents
    1. 테스트 설정2. 테스트 진행

    zerfa

    RSS·Powered by Inblog