개요
jUnit 의 Suite Class 기능을 이용하여 여러개의 테스트 클래스를 만들때 Suite 내에서 한번만 호출되는 클래스를 만들어야 하는 경우가 있다.
Before annotation 은 class 에서만 한번 호출되는 것이므로 적당하지 않다.
해결
Suite 로 설정한 class 에 BeforeClass 를 만들어 준다.
import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ testOPP.class, PoolTest.class }) public class AllTests { @BeforeClass public static void globalBeforeClass() { System.err.println("globalBeforeClass"); } @AfterClass public static void globalAfterClass() { System.err.println("globalAfterClass"); } }
maven-surefire-plugin 과 연계
surefire-plugin 을 사용할 경우 Suite class 를 제일 먼저 실행하지 않으므로 위의 BeforeClass 가 호출되지 않는다.
다음