project-root/
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/example/app/
│ │ └── Application.java
│ └── test/
│ └── java/
│ └── com/example/app/
│ ├── tests/
│ │ └── ExampleTest.java
│ ├── utils/
│ │ └── TestUtils.java
│ └── config/
│ └── TestConfig.java
└── pom.xml
@AfterAll method in JUnit - Framework Patterns
- Test Classes: Contain test methods annotated with
@Test, setup with@BeforeAll, and cleanup with@AfterAll. - Test Utilities: Helper methods and reusable code for tests, e.g., resource management.
- Configuration: Holds environment settings, test parameters, and global constants.
- Application Code: The main source code under test.
Use @BeforeAll to initialize shared resources once before all tests run, and @AfterAll to clean up those resources after all tests complete.
Store environment variables and credentials in TestConfig.java or external files, loaded at runtime.
Example snippet for @AfterAll usage:
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class ExampleTest {
@BeforeAll
static void setup() {
// Initialize shared resources
}
@AfterAll
static void cleanup() {
// Release shared resources
}
@Test
void sampleTest() {
// Test code here
}
}
JUnit integrates with build tools like Maven or Gradle to generate test reports in XML or HTML format.
CI/CD pipelines (e.g., Jenkins, GitHub Actions) run tests automatically and publish reports.
@AfterAll ensures cleanup tasks run once after all tests, preventing resource leaks that could affect reporting or subsequent runs.
- Use
@AfterAllfor cleanup that must happen once after all tests, such as closing database connections or stopping servers. - Methods annotated with
@AfterAllmust bestaticunless using the@TestInstance(Lifecycle.PER_CLASS)annotation. - Keep
@AfterAllmethods simple and reliable to avoid hiding test failures. - Pair
@AfterAllwith@BeforeAllto manage shared test resources cleanly. - Do not put test assertions inside
@AfterAllmethods; use them only for cleanup.
Where in this folder structure would you add a new @AfterAll method to close a database connection used by multiple tests?
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
