In JUnit 5 (JUnit Jupiter), @BeforeEach marks a method that runs before every test method in a class, and @AfterEach marks a method that runs after every test method. Together they give each test a fresh, isolated setup and a reliable cleanup, so tests do not depend on the order in which they run.
They replace the JUnit 4 annotations @Before and @After. Both annotations live in the org.junit.jupiter.api package.
@BeforeEach and @AfterEach work@TempDir and resources@BeforeEach and @AfterEach workFor each @Test method, JUnit creates a new instance of the test class (the default lifecycle is per-method), runs all @BeforeEach methods, runs the test, and then runs all @AfterEach methods. The @AfterEach methods run even if the test fails or throws.
The pair runs once for each of the two tests. Lifecycle methods can be package-private; they do not need to be public, but they must not be private or static.
Resetting state before each test gives you three guarantees:
Use @AfterEach for real cleanup, such as closing files or connections, deleting temporary data, or restoring static state. Setting a field to null at the end, as many examples do, is rarely needed because a new test instance is created for every test.
@TempDir and resourcesFor temporary files, JUnit manages cleanup for you:
No @AfterEach is required because the temporary directory is deleted after the test. Reserve @AfterEach for resources JUnit does not manage, such as an open network connection or an embedded server.
| JUnit 5 | JUnit 4 | Runs |
|---|---|---|
@BeforeEach | @Before | Before every test method |
@AfterEach | @After | After every test method |
@BeforeAll | @BeforeClass | Once before all tests in the class, static by default |
@AfterAll | @AfterClass | Once after all tests in the class, static by default |
@BeforeAll and @AfterAll methods must be static unless you switch to @TestInstance(Lifecycle.PER_CLASS), in which case one instance is shared by all tests and non-static methods are allowed. With per-class lifecycle, state is shared between tests, so @BeforeEach becomes more important for resetting it.
When test classes extend a base class, @BeforeEach methods of superclasses run before those of subclasses, and @AfterEach methods of subclasses run before those of superclasses. Multiple @BeforeEach methods in one class run in a deterministic but intentionally non-obvious order, so avoid depending on it; combine them into one method when order matters.
Lifecycle methods can also take parameters, resolved by extensions, for example TestInfo to read the current test name:
@BeforeEach?@Before. Likewise @AfterEach replaces @After. The packages differ, so mixing them in one class does not work.
@AfterEach run when a test fails?Yes. It runs after a failed test or an exception, which is why it suits cleanup. If @BeforeEach itself throws, @AfterEach still runs for that test.
@BeforeEach methods be static?No. They must be non-static, non-private instance methods. Use @BeforeAll for static, once-per-class setup.
@BeforeAll?@BeforeEach runs before each test, while @BeforeAll runs once before the first test in the class.
@BeforeEach and @AfterEach run around every test method to build a fresh fixture and clean it up, keeping tests isolated and order-independent. They are the JUnit 5 successors to @Before and @After. Use @TempDir and extensions to reduce manual cleanup, and use @BeforeAll and @AfterAll for expensive once-per-class setup.