What if your tests could clean up after themselves perfectly every time, without you lifting a finger?
Why @AfterAll method in JUnit? - Purpose & Use Cases
Imagine you run many tests on a web app. After all tests finish, you need to close the browser and clean up resources manually. You try to remember to do this every time, but sometimes you forget or do it too early.
Doing cleanup manually is slow and easy to forget. If you forget, leftover resources cause errors in later tests. Doing it too early breaks tests that still need those resources. This makes testing unreliable and frustrating.
The @AfterAll method runs once after all tests finish automatically. It ensures cleanup happens exactly once, at the right time, without you having to remember. This keeps tests clean and stable.
void cleanup() {
// manually call after all tests
closeBrowser();
releaseResources();
}@AfterAll
static void cleanup() {
closeBrowser();
releaseResources();
}It enables reliable, automatic cleanup after all tests, making your test suite stable and easy to maintain.
In a web app test suite, @AfterAll closes the browser once after all tests run, preventing memory leaks and test interference.
Automatic cleanup: Runs once after all tests finish.
Prevents errors: Avoids leftover resources causing test failures.
Saves time: No need to remember manual cleanup.
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
