[Junit5] @BeforeAll, @AfterAll 사용시 JUnitException

이상훈·2021년 12월 18일
0

테스트 코드 작성중 오류가 발생했다.

각 테스트에 공통으로 사용되는 객체를 정의하기위해 @BeforeAll 어노테이션을 사용하려 했다.

class HelloTest {

    ...
    @BeforeAll
    void beforeAll(){
    	System.out.println("beforeAll");  
    }
    
    ...

위와같은 형식으로 테스트를 실행하니

org.junit.platform.commons.JUnitException: @BeforeAll method '...' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS)

JUnitException이 발생했다.

설명을 읽어보니 static 혹은 @TestInstance(Lifecycle.PER_CLASS) 붙여달라고 한다.

beforeAll 메서드 앞에 static을 붙여주거나

HelloTest위에 @TestInstance(Lifecycle.PER_CLASS)를 붙여주면된다.

profile
Hello!

0개의 댓글