Created
July 27, 2022 06:33
-
-
Save joonas-yoon/09a4060919a06afa43ff52b953793a9a to your computer and use it in GitHub Desktop.
JUnit Lifecycle Test
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 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment