JUnit User Manual

This manual covers JUnit 4 and JUnit 5 usage, including writing tests, assertions, test suites, and other core features.

JUnit 5 Basic Usage

Writing Test Classes

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {
    
    @Test
    void testAddition() {
        int result = 2 + 3;
        assertEquals(5, result);
    }
}

Common Assertions

import static org.junit.jupiter.api.Assertions.*;

// Equality assertions
assertEquals(expected, actual);
assertEquals(expected, actual, "Custom error message");

// Boolean assertions
assertTrue(condition);
assertFalse(condition);

// Null assertions
assertNull(object);
assertNotNull(object);

// Array assertions
assertArrayEquals(expectedArray, actualArray);

// Exception assertions
assertThrows(Exception.class, () -> {
    // Code that may throw exception
});

// Combined assertions
assertAll("Person properties",
    () -> assertEquals("John", person.getFirstName()),
    () -> assertEquals("Doe", person.getLastName())
);

Before and After Operations

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;

class MyTest {
    
    @BeforeEach
    void setUp() {
        // Runs before each test method
    }
    
    @AfterEach
    void tearDown() {
        // Runs after each test method
    }
}

Nested Tests

import org.junit.jupiter.api.Nested;

@Nested
class NestedTests {
    
    @Test
    void testInNestedClass() {
        // Tests in nested test class
    }
}

Parameterized Tests

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
void testWithMultipleValues(int number) {
    assertTrue(number > 0);
}

Dynamic Tests

import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.DynamicTest;
import java.util.Arrays;

@TestFactory
Collection<DynamicTest> dynamicTestsFromIterator() {
    return Arrays.asList(
        DynamicTest.dynamicTest("First dynamic test", () -> assertTrue(true)),
        DynamicTest.dynamicTest("Second dynamic test", () -> assertEquals(4, 2 + 2))
    );
}

JUnit 4 Basic Usage

Writing Test Classes

import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {
    
    @Test
    public void testAddition() {
        assertEquals(5, 2 + 3);
    }
}

Common Assertions

// Equality assertions
assertEquals(expected, actual);
assertEquals("message", expected, actual);

// Boolean assertions
assertTrue(condition);
assertFalse(condition);

// Null assertions
assertNull(object);
assertNotNull(object);

// Exception assertions
@Test(expected = NullPointerException.class)
public void testException() {
    Object obj = null;
    obj.toString();
}

Before and After

import org.junit.Before;
import org.junit.After;

public class MyTest {
    
    @Before
    public void setUp() {
        // Runs before each test method
    }
    
    @After
    public void tearDown() {
        // Runs after each test method
    }
    
    @BeforeClass
    public static void setUpClass() {
        // Runs once before all tests in the class
    }
    
    @AfterClass
    public static void tearDownClass() {
        // Runs once after all tests in the class
    }
}

Test Suites

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class, Test3.class})
public class AllTests {
    // Runs all tests in Test1, Test2, and Test3
}

Migrating from JUnit 4 to JUnit 5

Annotation Changes

JUnit 4JUnit 5
@Test@Test
@Before@BeforeEach
@After@AfterEach
@BeforeClass@BeforeAll
@AfterClass@AfterAll
@Ignore@Disabled
@SuiteClasses@SelectClasses

Assertion Changes

// JUnit 4
Assert.assertEquals(expected, actual);

// JUnit 5
Assertions.assertEquals(expected, actual);

Exception Testing Changes

// JUnit 4
@Test(expected = NullPointerException.class)
public void testException() {
    Object obj = null;
    obj.toString();
}

// JUnit 5
@Test
public void testException() {
    assertThrows(NullPointerException.class, () -> {
        Object obj = null;
        obj.toString();
    });
}

Running Tests

Command Line

# Maven
mvn test

# Gradle
gradle test

# Standalone Console
java -jar junit-platform-console-standalone-5.14.3.jar -c MyTestClass

IDE Running

  • IntelliJ IDEA: Right-click test class or method, select Run
  • Eclipse: Right-click test class or method, select Run As → JUnit Test
  • VS Code: Use JUnit extension to run tests