The @BeforeEach annotation in JUnit 5 (JUnit Jupiter) marks a method that runs before every single test method in the class. You use it to build a fresh, known starting state for each test, such as creating the object under test, resetting a list, or configuring a mock. It replaces @Before from JUnit 4.
Because JUnit creates a new instance of the test class for each test by default, @BeforeEach is the natural place to initialise instance fields so that tests never share state.
For every test method, JUnit Jupiter runs the lifecycle in this order:
PER_METHOD lifecycle).@BeforeEach method runs.@Test method runs.@AfterEach method runs.The class-level hooks @BeforeAll and @AfterAll run once around all of this. That is the key difference: @BeforeEach runs N times for N tests, @BeforeAll runs once.
| Annotation | Runs | Method must be static | JUnit 4 equivalent |
|---|---|---|---|
@BeforeEach | Before every test | No | @Before |
@AfterEach | After every test | No | @After |
@BeforeAll | Once before all tests | Yes (unless PER_CLASS) | @BeforeClass |
@AfterAll | Once after all tests | Yes (unless PER_CLASS) | @AfterClass |
Both tests pass regardless of execution order, because each one gets a new ArrayList containing only apple. The first test adding banana does not leak into the second.
JUnit does not guarantee the order of test methods unless you request one, so tests must never depend on a previous test's changes.
A @BeforeEach method must follow these rules, otherwise JUnit reports an error:
private. Package-private, protected or public all work, and package-private is the common style.static (unless you use it in a context that explicitly allows it, which @BeforeEach does not).void.ParameterResolver can supply them, for example TestInfo, TestReporter or a Mockito extension.Using TestInfo lets you see which test is about to run:
@BeforeEach methods in a superclass run before those in a subclass, so a shared base class can set up common fixtures and subclasses can add their own. Methods declared in the same class run in a deterministic but intentionally non-obvious order, so do not rely on the order between two @BeforeEach methods in one class. Put dependent steps in a single method instead.
A very common use is creating mocks and the object under test:
If you use the Mockito JUnit 5 extension (@ExtendWith(MockitoExtension.class) with @Mock and @InjectMocks), the extension creates mocks before each test for you and you often do not need a manual @BeforeEach for mocks.
By default a new test instance is created per method. You can switch to one instance per class:
With PER_CLASS, fields persist between tests, so @BeforeEach becomes the only thing keeping tests independent. In exchange, @BeforeAll and @AfterAll methods may be non-static. Stay with the default unless you have a reason, because it protects you from accidental shared state.
| JUnit 4 | JUnit 5 |
|---|---|
org.junit.Before | org.junit.jupiter.api.BeforeEach |
@Before public void setUp() | @BeforeEach void setUp() |
Test classes and methods must be public | Package-private is enough |
When migrating, change the import, drop public if you like, and make sure the build uses the JUnit Platform (for Maven, a current Surefire plugin with junit-jupiter on the test classpath). If you still have JUnit 4 tests, the Vintage engine can run them next to Jupiter tests.
Not exactly. With the default lifecycle both run once per test, but @BeforeEach can take injected parameters, can be inherited and layered across a class hierarchy, and is paired with @AfterEach for cleanup. Constructors are fine for final fields, though.
Yes. All of them run before each test, but the order within a single class is not something you should depend on. Combine dependent steps into one method.
Yes. It runs before every invocation, so a parameterized test with five arguments triggers it five times.
The test method is skipped and reported as failed, and @AfterEach methods still run so resources can be cleaned up.
@BeforeEach marks a JUnit 5 method that runs before every test, giving each test a fresh, isolated fixture. It is the replacement for JUnit 4's @Before, must be non-private, non-static and void, and pairs with @AfterEach for cleanup. Use @BeforeAll instead for expensive one-time setup, and keep tests independent of execution order.