JUnit FAQ

Should I Use JUnit 4 or JUnit 5?

For new projects, JUnit 5 is strongly recommended. It provides richer features including nested tests, parameterized tests, dynamic tests, and better extensibility. For existing JUnit 4 projects, you can use JUnit Vintage to run tests on the JUnit 5 platform and migrate gradually.

How to Run JUnit 4 Tests in JUnit 5?

Add the JUnit Vintage engine dependency:

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.14.3</version>
    <scope>test</scope>
</dependency>

Why Are My Tests Not Running?

Common reasons include: missing @Test annotation on test class, test methods not public, missing maven-surefire-plugin or not configured useJUnitPlatform(), Java version does not meet requirements.

What Is the Minimum Java Version for JUnit 5?

JUnit 5 requires Java 8 or higher. JUnit 4 requires Java 5 or higher.

How to Skip Certain Tests?

Use @Disabled annotation to skip individual tests or entire test classes:

@Disabled("Temporarily disabled")
@Test
void testSkipped() {
    // Will not execute
}

How to Test Private Methods?

Unit tests should only test public methods. If you need to test private methods, refactor the code to move the logic to be tested into separate public methods, or use reflection (not recommended).

How to Get Detailed Information When Assertion Fails?

Provide custom error messages using the third parameter:

assertEquals(expected, actual, "Calculation result is incorrect");

How to Test Exceptions?

Use the assertThrows method:

@Test
void testException() {
    assertThrows(IllegalArgumentException.class, () -> {
        throw new IllegalArgumentException("error message");
    });
}

How to Run Tests in Parallel?

Configure parallel execution in JUnit 5:

import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
class ConcurrentTests {
    // Tests will run in parallel
}

What Is the Difference Between @Before and @BeforeEach?

@Before is a JUnit 4 annotation, @BeforeEach is a JUnit 5 annotation. They serve the same purpose: running before each test method. JUnit 5 also supports @BeforeAll and @AfterAll for setup/teardown that runs once for the entire test class.

How to Create Parameterized Tests?

Use @ParameterizedTest annotation with @ValueSource:

@ParameterizedTest
@ValueSource(strings = {"a", "b", "c"})
void testWithParams(String param) {
    assertNotNull(param);
}

What If Tests Time Out?

Use @Timeout annotation to set timeout:

@Test
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
void testWithTimeout() {
    // Will fail if exceeds 100 milliseconds
}

How to Repeat Tests?

Use @RepeatedTest annotation:

@RepeatedTest(10)
void testRepeated() {
    // Will repeat 10 times
}

What Is the JUnit 5 Extension Model?

JUnit 5 uses @ExtendWith annotation to provide extension mechanism for more flexible test customization:

@ExtendWith(MyExtension.class)
@Test
void testWithExtension() {
    // Uses custom extension
}