Test Overview
This test demonstrates the use of the @AfterAll method in JUnit. It verifies that a cleanup method runs once after all tests complete.
This test demonstrates the use of the @AfterAll method in JUnit. It verifies that a cleanup method runs once after all tests complete.
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; public class AfterAllExampleTest { private static int resource; @BeforeAll public static void setup() { resource = 5; System.out.println("Setup done"); } @Test public void testAddition() { int result = resource + 10; Assertions.assertEquals(15, result); } @Test public void testMultiplication() { int result = resource * 2; Assertions.assertEquals(10, result); } @AfterAll public static void cleanup() { resource = 0; System.out.println("Cleanup done"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit starts test run and calls @BeforeAll method | Static variable 'resource' is set to 5 | - | PASS |
| 2 | JUnit runs testAddition method | resource = 5 | Check if resource + 10 equals 15 | PASS |
| 3 | JUnit runs testMultiplication method | resource = 5 | Check if resource * 2 equals 10 | PASS |
| 4 | JUnit calls @AfterAll method after all tests | resource is reset to 0 | - | PASS |