import org.junit.jupiter.api.*; @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class SampleTest { @BeforeAll void beforeAll() { System.out.print("A"); } @BeforeEach void beforeEach() { System.out.print("B"); } @Test void test1() { System.out.print("C"); } @Test void test2() { System.out.print("D"); } @AfterEach void afterEach() { System.out.print("E"); } @AfterAll void afterAll() { System.out.print("F"); } }
The order is:
- BeforeAll runs once: prints 'A'
- BeforeEach before test1: prints 'B'
- test1 runs: prints 'C'
- AfterEach after test1: prints 'E'
- BeforeEach before test2: prints 'B'
- test2 runs: prints 'D'
- AfterEach after test2: prints 'E'
- AfterAll runs once: prints 'F'
Combined output: ABCEBDEF
import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; public class CounterTest { static int beforeEachCount = 0; @BeforeEach void beforeEach() { beforeEachCount++; } @Test void testOne() {} @Test void testTwo() {} }
There are two tests, so @BeforeEach runs twice, incrementing beforeEachCount to 2.
Option D correctly asserts the count is 2.
Other options either check wrong values or irrelevant conditions.
import org.junit.jupiter.api.*; public class ErrorTest { @BeforeAll static void setup() { System.out.println("Setup"); } @Test void test() { System.out.println("Test"); } }
In JUnit 5, @BeforeAll methods must be static unless the test class uses @TestInstance(Lifecycle.PER_CLASS).
Here, setup() is non-static and no @TestInstance annotation is present, causing the error.
@TestInstance(Lifecycle.PER_CLASS) creates one instance of the test class for all tests.
This allows @BeforeAll and @AfterAll methods to be non-static because they run on the single instance.
Other options are incorrect because @BeforeEach/@AfterEach still run before/after each test, @AfterAll is not disabled, and test methods do not need to be static.
import org.junit.jupiter.api.*; @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class OuterTest { @BeforeAll void outerBeforeAll() { System.out.print("1"); } @BeforeEach void outerBeforeEach() { System.out.print("2"); } @Test void outerTest() { System.out.print("3"); } @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) class InnerTest { @BeforeAll void innerBeforeAll() { System.out.print("4"); } @BeforeEach void innerBeforeEach() { System.out.print("5"); } @Test void innerTest() { System.out.print("6"); } @AfterEach void innerAfterEach() { System.out.print("7"); } @AfterAll void innerAfterAll() { System.out.print("8"); } } @AfterEach void outerAfterEach() { System.out.print("9"); } @AfterAll void outerAfterAll() { System.out.print("0"); } }
Execution order:
- Outer @BeforeAll: prints '1'
- Outer @BeforeEach before outerTest: '2'
- outerTest: '3'
- Outer @AfterEach after outerTest: '9'
- Inner @BeforeAll: '4'
- Outer @BeforeEach before innerTest: '2'
- Inner @BeforeEach: '5'
- innerTest: '6'
- Inner @AfterEach: '7'
- Outer @AfterEach after innerTest: '9'
- Inner @AfterAll: '8'
- Outer @AfterAll: '0'
Combined output: 123942567980