0
0
JunitHow-ToBeginner ยท 3 min read

How to Use @BeforeEach in JUnit for Setup Before Tests

Use the @BeforeEach annotation in JUnit to mark a method that runs before each test method in the class. This helps set up common test data or state so each test starts fresh. The method annotated with @BeforeEach must be non-static and void.
๐Ÿ“

Syntax

The @BeforeEach annotation is placed above a method that you want to run before every test method in the class. This method is used to prepare the test environment.

  • Method signature: The method must be void and take no parameters.
  • Non-static: The method cannot be static because it runs on the test instance.
  • Placement: Place it inside your test class.
java
import org.junit.jupiter.api.BeforeEach;

public class MyTest {

    @BeforeEach
    void setup() {
        // code to run before each test
    }

    // test methods
}
๐Ÿ’ป

Example

This example shows a test class where @BeforeEach initializes a list before each test method runs. This ensures each test starts with a fresh list.

java
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;

public class ListTest {

    private List<String> list;

    @BeforeEach
    void setup() {
        list = new ArrayList<>();
        list.add("apple");
    }

    @Test
    void testListStartsWithOneItem() {
        assertEquals(1, list.size());
        assertEquals("apple", list.get(0));
    }

    @Test
    void testAddItem() {
        list.add("banana");
        assertEquals(2, list.size());
        assertTrue(list.contains("banana"));
    }
}
Output
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
โš ๏ธ

Common Pitfalls

Common mistakes when using @BeforeEach include:

  • Making the setup method static, which causes JUnit to ignore it.
  • Using @BeforeEach on methods with parameters or non-void return types.
  • Not initializing or resetting shared test data, causing tests to affect each other.

Always ensure the setup method is void, non-static, and resets state properly.

java
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class WrongSetupTest {

    // Wrong: static method will NOT run before each test
    @BeforeEach
    static void setup() {
        // setup code
    }

    // Correct way:
    @BeforeEach
    void correctSetup() {
        // setup code
    }

    @Test
    void testSomething() {
        // test code
    }
}
๐Ÿ“Š

Quick Reference

  • @BeforeEach: Runs before each test method.
  • Method must be void and no parameters.
  • Method must NOT be static.
  • Use it to reset or initialize test data.
  • Helps keep tests independent and clean.
โœ…

Key Takeaways

Use @BeforeEach to run setup code before every test method in a class.
The method annotated with @BeforeEach must be void, non-static, and take no parameters.
Initialize or reset shared test data in @BeforeEach to keep tests independent.
Avoid making the setup method static or giving it parameters, or it won't run.
@BeforeEach helps ensure each test starts with a clean state.