JUnit 5 lifecycle annotations are hooks that run code around your tests: @BeforeEach and @AfterEach run before and after every test method, while @BeforeAll and @AfterAll run once for the whole class. Use them to build fresh test data and to release resources. This guide explains the junit lifecycle annotations, the order they run in and the rules that apply to them.
The annotations live in org.junit.jupiter.api. They replace the JUnit 4 names @Before, @After, @BeforeClass and @AfterClass.
| Annotation | Runs | Typical use |
|---|---|---|
@BeforeAll | Once, before the first test | Start an expensive shared resource such as an embedded server |
@BeforeEach | Before every test method | Create a fresh object under test |
@AfterEach | After every test method | Clean up per-test state |
@AfterAll | Once, after the last test | Stop the shared resource |
Methods annotated with @BeforeAll and @AfterAll must be static by default. @BeforeEach and @AfterEach methods are instance methods. None of these methods may be private, and they must return void.
The output is:
JUnit does not guarantee the order of test methods, so first and second may swap. The hooks always wrap each test in the order shown.
By default JUnit creates a new instance of the test class for every test method. Instance fields therefore start fresh for each test, which keeps tests independent:
Both tests pass in any order because startsEmpty never sees the item added by the other test.
Annotate the class with @TestInstance(TestInstance.Lifecycle.PER_CLASS) and JUnit uses a single instance for all tests in the class. Then @BeforeAll and @AfterAll may be non-static, which is handy when they need instance fields or when you use Kotlin:
With PER_CLASS, fields are shared between tests, so reset mutable state in @BeforeEach yourself. You can also set the default for the whole project with the junit.jupiter.testinstance.lifecycle.default configuration parameter.
@BeforeAll and @BeforeEach, and after them for @AfterEach and @AfterAll.@Nested classes, the outer class's @BeforeEach runs before the inner one, which lets each nested scenario add its own setup.@BeforeEach method throws, the test method is skipped, but @AfterEach still runs, so cleanup is reliable.@AfterEach when setup lives in a @BeforeEach.For temporary files, JUnit provides the @TempDir annotation, which creates and deletes a directory for you and often removes the need for manual cleanup hooks.
Because JUnit creates a new test instance for each test method, there is no single instance at the time @BeforeAll runs. Use @TestInstance(PER_CLASS) if you need a non-static method.
Yes, but their relative order is deterministic yet intentionally not obvious. Keep one setup method per class to stay readable.
Yes. It runs after every test, whether the test passed, failed or threw an exception.
@BeforeEach and @AfterEach. @BeforeClass and @AfterClass became @BeforeAll and @AfterAll.
Use @BeforeEach and @AfterEach for per-test setup and cleanup, and @BeforeAll and @AfterAll for work done once per class. Hooks must not be private and the class-level ones are static unless you choose PER_CLASS. Because each test gets a new instance by default, tests stay independent and can run in any order.