[JUnit5] Lifecycle 호출 순서
Joonas' Note
[JUnit5] Lifecycle 호출 순서 본문
JUnit5에서는 4가지 Lifecycle 관리 annoation을 제공한다. JUnit4에서도 있었지만 이름이 바뀌었다.
- @BeforeAll - (JUnit4 @BeforeClass)
- @BeforeEach - (JUnit4 @Before)
- @AfterAll - (JUnit4 @AfterClass)
- @AfterEach - (JUnit4 @After)
Lifecycle 관리 단위를 클래스마다로 바꿀 수 있는데, 호출 순서가 조금 바뀐다.
![](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
클래스 인스턴스가 생성되고 호출되므로, @BeforeAll과 @AfterAll은 더 이상 static 함수가 아니어도 된다.
실제 코드는 아래와 같다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.*; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
public class LifeCycleTest { | |
private int count = 0; | |
LifeCycleTest() { | |
System.out.println("construct"); | |
} | |
@BeforeAll | |
static void beforeAll() { | |
System.out.println("@BeforeAll"); | |
} | |
@BeforeEach | |
void beforeEach() { | |
count += 1; | |
System.out.println("@BeforeEach (count += 1): " + count); | |
} | |
@AfterEach | |
void afterEach() { | |
System.out.println("@AfterEach"); | |
} | |
@AfterAll | |
static void afterAll() { | |
System.out.println("@AfterAll"); | |
} | |
@Test | |
void test1() { | |
assertEquals(count, 1); | |
System.out.println("@Test : test1"); | |
} | |
@Test | |
void test2() { | |
assertEquals(count, 1); | |
System.out.println("@Test : test2"); | |
} | |
@Test | |
void test3() { | |
assertEquals(count, 1); | |
System.out.println("@Test : test3"); | |
} | |
} | |
/* | |
[Output] | |
@BeforeAll | |
construct | |
@BeforeEach (count += 1): 1 | |
@Test : test1 | |
@AfterEach | |
construct | |
@BeforeEach (count += 1): 1 | |
@Test : test2 | |
@AfterEach | |
construct | |
@BeforeEach (count += 1): 1 | |
@Test : test3 | |
@AfterEach | |
@AfterAll | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.*; | |
import static org.junit.jupiter.api.Assertions.assertEquals; | |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
public class LifeCycleTest { | |
private int count = 0; | |
LifeCycleTest() { | |
System.out.println("construct"); | |
} | |
@BeforeAll | |
void beforeAll() { | |
System.out.println("@BeforeAll"); | |
} | |
@BeforeEach | |
void beforeEach() { | |
count += 1; | |
System.out.println("@BeforeEach (count += 1): " + count); | |
} | |
@AfterEach | |
void afterEach() { | |
System.out.println("@AfterEach"); | |
} | |
@AfterAll | |
void afterAll() { | |
System.out.println("@AfterAll"); | |
} | |
@Test | |
@Order(1) | |
void test1() { | |
assertEquals(count, 1); | |
System.out.println("@Test : test1"); | |
} | |
@Test | |
@Order(2) | |
void test2() { | |
assertEquals(count, 2); | |
System.out.println("@Test : test2"); | |
} | |
@Test | |
@Order(3) | |
void test3() { | |
assertEquals(count, 3); | |
System.out.println("@Test : test3"); | |
} | |
} | |
/* | |
[Output] | |
construct | |
@BeforeAll | |
@BeforeEach (count += 1): 1 | |
@Test : test1 | |
@AfterEach | |
@BeforeEach (count += 1): 2 | |
@Test : test2 | |
@AfterEach | |
@BeforeEach (count += 1): 3 | |
@Test : test3 | |
@AfterEach | |
@AfterAll | |
*/ |
출력이나 클래스 내부 변수의 상태를 확인해보면, 상태가 다른 것을 확인할 수 있다.
'개발 > Java' 카테고리의 다른 글
Java의 instanceof 결과 정리 (0) | 2024.07.31 |
---|---|
[Java/JavaDoc] @see, @inheritDoc (0) | 2022.04.15 |
[JAVA] 싱글톤 패턴 (Singleton Pattern) (0) | 2022.04.11 |
Google ARCore Sceneform archived 소식 (2) | 2020.05.30 |
자바에서 내부 클래스의 직렬화(Serialize) (0) | 2018.10.17 |