CodeBlanks Logo Icon CodeBlanks

@BeforeEach and @AfterEach

Learn setup with @BeforeEach and @AfterEach.

@BeforeEach and @AfterEach

Code that should run before each test method is marked with @BeforeEach; code that runs after each test is marked with @AfterEach. These methods handle common setup and cleanup.

import org.junit.jupiter.api.*;

class CartTest {
    List<String> cart;

    @BeforeEach
    void setup() { cart = new ArrayList<>(); }

    @AfterEach
    void cleanup() { cart.clear(); }
}

Each test now runs independently; side effects from the previous test do not leak. Setup lives in one place and is not repeated. This keeps tests fast and reliable.

Class-level setup with @BeforeAll

For heavy resources (database connection, file) opened once, use @BeforeAll. This method must be static; the framework calls it once per class.

@BeforeAll
static void openDatabase() {
    // connection opened once
}

@AfterAll
static void closeDatabase() {
    // connection closed once
}

This shortens test time. But make sure tests do not depend on each other; otherwise errors appear when order changes. @BeforeEach is usually preferred.

Practice Use @BeforeEach in a cart test

Write a CartTest class. Declare a List<String> cart field. In a @BeforeEach method setup, do cart = new ArrayList<>(). Write a @Test method testAdd that calls cart.add("apple") and verifies with assertEquals(1, cart.size()). Add a main method, add one item and print size().

Starter code

Main.java
import org.junit.jupiter.api.*;
import java.util.*;

// Define the CartTest class

// cart field, setup annotation with setup, test annotation with testAdd

// Add a main method that prints cart.size()

Sample solution

Main.java
import org.junit.jupiter.api.*;
import java.util.*;

class CartTest {
    List<String> cart;

    @BeforeEach
    void setup() {
        cart = new ArrayList<>();
    }

    @Test
    void testAdd() {
        cart.add("apple");
        assertEquals(1, cart.size());
    }

    public static void main(String[] args) {
        CartTest t = new CartTest();
        t.setup();
        t.cart.add("apple");
        System.out.println(t.cart.size());
    }
}

Expected output

1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match lifecycle annotations

Term Meaning
@BeforeEach Before each test
@AfterEach After each test
@BeforeAll Before the class (static)
@AfterAll After the class (static)

2 Complete the BeforeAll method

Question
@BeforeAll
____ void openResource() {
    // tek seferlik kurulum
}
Answer
@BeforeAll
static void openResource() {
    // tek seferlik kurulum
}

Explanation: @BeforeAll methods must be static.

3 What is the difference between @BeforeEach and @BeforeAll?

  • BeforeEach runs before each test, BeforeAll before the class Correct answer
  • BeforeAll runs before each test, BeforeEach before the class
  • They are the same, only the names differ
  • BeforeAll runs only on error

Explanation: BeforeEach runs before each test, BeforeAll once before the class.

All lessons in Advanced