JUnit annotations

In JUnit (Java Unit Testing framework), annotations are special markers (prefixed with @) that tell JUnit how to run and organize your tests.

Let’s go over the most important JUnit annotations — for both JUnit 4 and JUnit 5 (Jupiter).


🧪 JUnit 5 (current standard)

JUnit 5 annotations come from the package:

import org.junit.jupiter.api.*;

Here’s a summary table first 👇

AnnotationPurpose
@TestMarks a method as a test case
@BeforeEachRuns before each test method
@AfterEachRuns after each test method
@BeforeAllRuns once before all tests in the class (must be static)
@AfterAllRuns once after all tests (must be static)
@DisplayNameCustom name for test display
@DisabledTemporarily disables a test
@TagAssigns a tag to group/filter tests
@ParameterizedTestRuns the same test with different inputs
@RepeatedTestRuns a test multiple times
@NestedGroups related tests inside an inner class
@TestInstanceChanges test instance lifecycle (PER_CLASS or PER_METHOD)
@TimeoutFails the test if it exceeds the time limit

✅ Example: Basic JUnit 5 Test Class

import org.junit.jupiter.api.*;

class CalculatorTest {

    private Calculator calculator;

    @BeforeAll
    static void setupAll() {
        System.out.println("Runs once before all tests.");
    }

    @BeforeEach
    void setup() {
        calculator = new Calculator();
        System.out.println("Runs before each test.");
    }

    @Test
    @DisplayName("Addition works correctly")
    void testAddition() {
        Assertions.assertEquals(5, calculator.add(2, 3));
    }

    @Test
    @Disabled("Feature not implemented yet")
    void testSubtraction() {
        Assertions.fail("This test is disabled and should not run");
    }

    @AfterEach
    void tearDown() {
        System.out.println("Runs after each test.");
    }

    @AfterAll
    static void tearDownAll() {
        System.out.println("Runs once after all tests.");
    }
}

class Calculator {
    int add(int a, int b) {
        return a + b;
    }
}

🧩 JUnit 4 (older version)

If you see imports from:

import org.junit.*;

then it’s JUnit 4.
The equivalent annotations are similar but slightly different in naming:

JUnit 4JUnit 5 EquivalentDescription
@Test@TestMarks a test method
@Before@BeforeEachRuns before each test
@After@AfterEachRuns after each test
@BeforeClass@BeforeAllRuns once before all tests (must be static)
@AfterClass@AfterAllRuns once after all tests (must be static)
@Ignore@DisabledSkips the test
(No direct equivalent)@DisplayNameCustom test name
(No direct equivalent)@TagTest tagging/filtering

✅ Example: JUnit 4 Test

import org.junit.*;

public class CalculatorTest {

    private Calculator calculator;

    @BeforeClass
    public static void initAll() {
        System.out.println("Before all tests");
    }

    @Before
    public void init() {
        calculator = new Calculator();
    }

    @Test
    public void testAdd() {
        Assert.assertEquals(5, calculator.add(2, 3));
    }

    @After
    public void tearDown() {
        System.out.println("After each test");
    }

    @AfterClass
    public static void tearDownAll() {
        System.out.println("After all tests");
    }
}

⚙️ JUnit 5 Imports Reference

Make sure you use these packages:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Assertions;

Leave a Reply