In JUnit 5, @BeforeEach runs a method before every test in the class and @AfterEach runs one after every test. They create a clean test environment for each test and release resources afterward, which keeps tests independent and repeatable. Both come from the org.junit.jupiter.api package and replace JUnit 4's @Before and @After.
This answer focuses on practical patterns for them with JUnit 5: mocks, databases, ordering, and what to avoid.
@BeforeEach@AfterEachA JUnit 5 test class gets a new instance for every test method by default. Around each test, JUnit calls the lifecycle methods in this order:
@BeforeEach methods@Test method@AfterEach methods@AfterEach runs even when the test fails. The annotated methods must not be private or static, and they must return void.
@BeforeEachCommon uses are creating the object under test, stubbing mocks, and loading fixture data. With Mockito's JUnit 5 extension, mocks are created before @BeforeEach runs, so you can stub them there:
Mockito's strict stubs report unused stubbings, so stub in @BeforeEach only what every test needs, and stub the rest inside the test.
@AfterEachUse @AfterEach for resources that outlive a test: open connections, started servers, files outside a temp directory, or static state you changed.
For Spring tests, @Transactional on the test class rolls back each test automatically, which often replaces a manual @AfterEach.
| Situation | Order |
|---|---|
| Base class and subclass | Base @BeforeEach first, then subclass; teardown is the reverse |
| Several methods in one class | Deterministic but not intended to be relied on; merge them if order matters |
Failing @BeforeEach | The test is skipped, @AfterEach still runs |
Prefer composition and extensions over deep test base classes. A @RegisterExtension or a custom Extension implementing BeforeEachCallback and AfterEachCallback can package setup and cleanup for reuse across many classes.
@BeforeEach small. If most tests ignore half of the setup, split the class or use a nested class with its own @BeforeEach.@BeforeAll.@AfterEach for the behavior under test; they hide the test's intent.@AfterEach to repair a broken test environment. Fix the isolation problem.@Nested classes. Outer @BeforeEach methods run first, then the nested one.@AfterEach for every test class?No. Because a new instance is created for each test, plain fields do not need resetting. Add @AfterEach only for external resources.
@BeforeEach method?Yes, but the order between them is not something to depend on. A single method is clearer.
@Before and @BeforeEach interchangeable?No. @Before is JUnit 4 and is ignored by the Jupiter engine unless the vintage engine runs the class.
@AfterEach if the test throws?It still runs, so cleanup is guaranteed.
@BeforeEach and @AfterEach wrap every test in JUnit 5 with setup and teardown, giving isolated and repeatable tests. Use them for mocks, fixtures, and external resources, keep them small, prefer @TempDir, extensions, and transactions where they fit, and use @BeforeAll for expensive once-per-class work.