@AfterAll method in JUnit - Build an Automation Script
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class AfterAllExampleTest { private static boolean afterAllExecuted = false; @Test void testOne() { Assertions.assertFalse(afterAllExecuted, "@AfterAll should not run before tests"); } @Test void testTwo() { Assertions.assertFalse(afterAllExecuted, "@AfterAll should not run before tests"); } @AfterAll static void cleanup() { afterAllExecuted = true; System.out.println("@AfterAll method executed"); } @Test void verifyAfterAllExecuted() { // This test runs before @AfterAll, so we cannot assert here // Instead, we rely on external verification or test runner output } }
This test class has two test methods testOne and testTwo. Both assert that the afterAllExecuted flag is false, ensuring the @AfterAll method has not run yet.
The @AfterAll method cleanup is static as required by JUnit 5. It sets the flag to true and prints a message.
Since @AfterAll runs after all tests, the flag will be true only after all tests complete. We cannot assert the flag inside tests because they run before @AfterAll.
To verify the @AfterAll method ran, you can check the console output or use a test runner that supports lifecycle callbacks.
Now add a test class using @TestInstance(Lifecycle.PER_CLASS) to allow a non-static @AfterAll method
Practice
@AfterAll annotation in JUnit 5?Solution
Step 1: Understand the role of
The@AfterAll@AfterAllannotation is designed to mark a method that runs once after all test methods in the class have completed.Step 2: Compare with other lifecycle annotations
@BeforeEachand@AfterEachrun before and after each test method respectively, not once after all tests.Final Answer:
To run a method once after all tests in the class have finished -> Option BQuick Check:
@AfterAllruns once after all tests [OK]
- Confusing @AfterAll with @AfterEach
- Thinking it runs before tests
- Assuming it runs multiple times
@AfterAll in JUnit 5?Solution
Step 1: Recall method requirements for
In JUnit 5, methods annotated with@AfterAll@AfterAllmust be static and usually public to be accessible.Step 2: Check each option
public void cleanup() { ... } is non-static, so invalid. private static void cleanup() { ... } is private, so invalid. public static void cleanup() { ... } is public static, which is correct. private void cleanup() { ... } is private and non-static, invalid.Final Answer:
public static void cleanup() { ... } -> Option CQuick Check:
@AfterAllmethod must be static and public [OK]
- Forgetting to make the method static
- Using private access modifier
- Omitting the static keyword
import org.junit.jupiter.api.*;
public class TestExample {
static int count = 0;
@Test
void testOne() {
count++;
}
@Test
void testTwo() {
count++;
}
@AfterAll
public static void afterAll() {
System.out.println("Count: " + count);
}
}What will be printed when the tests run?
Solution
Step 1: Analyze test method executions
BothtestOne()andtestTwo()increment the static variablecountby 1 each. So after both tests run,countwill be 2.Step 2: Understand
The@AfterAlloutputafterAll()method runs once after all tests and prints the current value ofcount, which is 2.Final Answer:
Count: 2 -> Option AQuick Check:
Both tests increment count, printed after all tests [OK]
- Assuming count resets between tests
- Thinking @AfterAll runs before tests
- Confusing instance and static variables
import org.junit.jupiter.api.*;
public class SampleTest {
@AfterAll
void cleanup() {
System.out.println("Cleaning up");
}
@Test
void test() {
Assertions.assertTrue(true);
}
}What is the problem with this code?
Solution
Step 1: Check
In JUnit 5,@AfterAllmethod signature@AfterAllmethods must be static unless the test class is annotated with@TestInstance(Lifecycle.PER_CLASS). Here, the method is non-static and no such annotation is present.Step 2: Validate other options
The method does not need to return a value, printing is allowed, and@Testmethods do not need to be static.Final Answer:
The@AfterAllmethod must be static -> Option AQuick Check:
@AfterAllrequires static method unless PER_CLASS used [OK]
- Forgetting static requirement for @AfterAll
- Thinking @AfterAll must return a value
- Confusing @Test method static rules
@AfterAll?Solution
Step 1: Understand resource cleanup timing
Shared resources like a database connection should be closed once after all tests finish, not before or during each test.Step 2: Apply
The cleanup method must be static to run after all tests. Closing the connection in a static@AfterAllrules@AfterAllmethod ensures proper cleanup.Final Answer:
Declare a static@AfterAllmethod that closes the connection -> Option DQuick Check:
Static @AfterAll method cleans shared resources after tests [OK]
- Making @AfterAll method non-static
- Closing resources before tests start
- Closing resources inside each test
Ready to prove it?
Pass the test and this pattern moves unknown
