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 👇
Annotation | Purpose |
---|---|
@Test | Marks a method as a test case |
@BeforeEach | Runs before each test method |
@AfterEach | Runs after each test method |
@BeforeAll | Runs once before all tests in the class (must be static ) |
@AfterAll | Runs once after all tests (must be static ) |
@DisplayName | Custom name for test display |
@Disabled | Temporarily disables a test |
@Tag | Assigns a tag to group/filter tests |
@ParameterizedTest | Runs the same test with different inputs |
@RepeatedTest | Runs a test multiple times |
@Nested | Groups related tests inside an inner class |
@TestInstance | Changes test instance lifecycle (PER_CLASS or PER_METHOD ) |
@Timeout | Fails 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 4 | JUnit 5 Equivalent | Description |
---|---|---|
@Test | @Test | Marks a test method |
@Before | @BeforeEach | Runs before each test |
@After | @AfterEach | Runs after each test |
@BeforeClass | @BeforeAll | Runs once before all tests (must be static) |
@AfterClass | @AfterAll | Runs once after all tests (must be static) |
@Ignore | @Disabled | Skips the test |
(No direct equivalent) | @DisplayName | Custom test name |
(No direct equivalent) | @Tag | Test 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;