Lifecycle hooks help prepare things before tests run and clean up after tests finish. This keeps tests neat and avoids repeated code.
Why lifecycle hooks manage setup and teardown in JUnit
@BeforeEach
void setup() {
// code to run before each test
}
@AfterEach
void teardown() {
// code to run after each test
}@BeforeEach runs before every test method.
@AfterEach runs after every test method.
@BeforeEach
void setup() {
System.out.println("Starting test");
}
@AfterEach
void teardown() {
System.out.println("Test finished");
}@BeforeAll
static void initAll() {
System.out.println("Setup once before all tests");
}
@AfterAll
static void tearDownAll() {
System.out.println("Cleanup once after all tests");
}This test class uses @BeforeEach and @AfterEach to print messages around each test. It shows setup and teardown happening for every test method.
import org.junit.jupiter.api.*; public class SampleTest { @BeforeEach void setup() { System.out.println("Setup before test"); } @AfterEach void teardown() { System.out.println("Teardown after test"); } @Test void testOne() { System.out.println("Running testOne"); Assertions.assertTrue(true); } @Test void testTwo() { System.out.println("Running testTwo"); Assertions.assertEquals(2, 1 + 1); } }
Use lifecycle hooks to avoid repeating setup and cleanup code in every test.
Make sure @BeforeEach and @AfterEach methods do not fail, or tests may be affected.
@BeforeAll and @AfterAll must be static methods in JUnit 5.
Lifecycle hooks run code before and after tests to manage setup and teardown.
This keeps tests clean and avoids repeating code.
@BeforeEach/@AfterEach run before/after each test; @BeforeAll/@AfterAll run once for all tests.
Practice
@BeforeEach and @AfterEach in JUnit tests?Solution
Step 1: Understand lifecycle hooks purpose
Lifecycle hooks like@BeforeEachand@AfterEachrun code before and after each test method to prepare and clean up the test environment.Step 2: Identify correct use case
This avoids repeating setup and teardown code inside every test, keeping tests clean and consistent.Final Answer:
To run setup and cleanup code before and after each test automatically -> Option BQuick Check:
Lifecycle hooks = setup/teardown automation [OK]
- Thinking lifecycle hooks skip assertions
- Confusing lifecycle hooks with parallel test execution
- Assuming lifecycle hooks generate reports
Solution
Step 1: Identify the annotation for running once before all tests
The@BeforeAllannotation runs a method once before any tests in the class run.Step 2: Check method signature requirements
Methods with@BeforeAllmust be static in JUnit 5, sopublic static void init()is correct.Final Answer:
@BeforeAll public static void init() {} -> Option CQuick Check:
@BeforeAll + static = run once before all tests [OK]
- Using @BeforeEach instead of @BeforeAll for once-only setup
- Not making @BeforeAll method static
- Confusing @AfterEach with setup annotations
class SampleTest {
static int counter = 0;
@BeforeEach
void beforeEach() {
counter++;
}
@Test
void testOne() {}
@Test
void testTwo() {}
@AfterAll
static void afterAll() {
System.out.println(counter);
}
}What will be printed when the tests run?
Solution
Step 1: Analyze the lifecycle annotations and method signatures
The@AfterAllmethod must be static, which it is. However,counteris static and incremented in@BeforeEachwhich is non-static, so no issue there.Step 2: Check for JUnit 5 rules on lifecycle methods
JUnit 5 requires@BeforeEachand@AfterEachmethods to be non-static, which is correct here. But@AfterAllis static, so no problem.Step 3: Identify the problem
However, the class is missing the@TestInstance(Lifecycle.PER_CLASS)annotation if we want to use static fields properly with lifecycle hooks. Without it, static fields are fine but the test runner may not allow static@AfterAllwithout this annotation in some setups.Step 4: Confirm if code compiles
Actually, this code compiles and runs fine. The counter increments twice (once per test), so2is printed.Final Answer:
2 -> Option AQuick Check:
Counter increments twice = 2 printed [OK]
- Assuming @AfterAll cannot access static fields
- Thinking counter resets between tests
- Confusing static and instance method requirements
class TestExample {
@BeforeAll
void setup() {
System.out.println("Setup");
}
@Test
void test() {
System.out.println("Test running");
}
}Solution
Step 1: Check @BeforeAll method requirements
In JUnit 5, methods annotated with@BeforeAllmust be static unless the test class is annotated with@TestInstance(Lifecycle.PER_CLASS).Step 2: Verify method signature
Thesetup()method is not static, and the class lacks@TestInstance, so this causes a runtime error.Final Answer:
Method annotated with @BeforeAll must be static -> Option AQuick Check:
@BeforeAll requires static method without @TestInstance [OK]
- Thinking @Test methods must be static
- Believing missing @AfterEach is an error
- Ignoring static requirement for @BeforeAll
Solution
Step 1: Understand resource management scope
Opening a database connection once before all tests and closing it once after all tests saves time and resources.Step 2: Choose correct lifecycle hooks
@BeforeAlland@AfterAllrun once per test class, ideal for this purpose. They must be static methods unless using@TestInstance(Lifecycle.PER_CLASS).Final Answer:
Use @BeforeAll static method to open and @AfterAll static method to close the connection -> Option DQuick Check:
Open/close once with @BeforeAll/@AfterAll static methods [OK]
- Using @BeforeEach/@AfterEach causing repeated open/close
- Opening/closing inside each test wastes resources
- Using non-static @BeforeAll/@AfterAll without @TestInstance
Ready to prove it?
Pass the test and this pattern moves unknown
