더 자바, 애플리케이션을 테스트하는 다양한 방법

Ch01. JUnit5 - JUnit5 시작하기

webmaster 2022. 1. 3. 16:22
728x90
  • SpringBoot 2.2 이상부터는 SpringBootStarter가 제공하는 JUnit 버전이 변경되었다(4 -> 5)
  • JUnit5부터는 public 제약이 사라졌다
  • 기본 애노테이션
    • @Test 
    • @BeforeAll / @AfterAll 
      • private X, default O, return Type 존재 X
      • 테스트를 실행할 때 딱 1번만 실행된다.
    • @BeforeEach / @AfterEach
      • 테스트가 실행될 때마다 실행된다.
    • @Disabled
      • 실행하고 싶은 Test가 존재할 경우 실행한다.
  • class StudyTest {
    
        @Test
        void create(){ //JUNIT5 부터는 public 제약이 사라졋다
            Study study = new Study();
            assertNotNull(study);
            System.out.println("create");
        }
    
        @Test
        @Disabled //실행하고 싶지 않은 테스트에 표기한다.
        public void create1(){
            System.out.println("create1");
        }
    
        @BeforeAll //private X, default O, return Type 존재 X
        static void beforeAll(){ //테스트를 실행할때 딱 1번만 실행된다.
            System.out.println("before all");
        }
    
        @AfterAll
        static void afterAll(){
            System.out.println("after all");
        }
    
        @BeforeEach
        void beforeEach(){ //테스트를 실행 할 떄마다 실행된다.
            System.out.println("BeforeEach each");
        }
    
        @AfterEach
        void afterEach(){
            System.out.println("After each");
        }
    }

 

728x90