What is the difference between JUnit 4 and JUnit 5, and why might you choose one over the other?

JUnit 5 is a ground-up rewrite of JUnit 4 with a modular architecture split into the Platform, Jupiter, and Vintage engines, offering modern Java support, a richer extension model, and better support for parameterized and dynamic tests.

Key Points

Example

A JUnit 4 test using @RunWith(SpringRunner.class) and @Rule for temporary folders becomes a JUnit 5 test using @ExtendWith(SpringExtension.class) and the built-in @TempDir annotation, with cleaner, more composable syntax throughout.

// JUnit 4
@RunWith(SpringRunner.class)
@SpringBootTest
public class PriceServiceTest {
    @Rule public TemporaryFolder tmp = new TemporaryFolder();

    @Test(expected = IllegalArgumentException.class)
    public void rejectsNegativePrice() { priceService.set(-1); }
}

// JUnit 5 (Jupiter)
@SpringBootTest                                  // already includes @ExtendWith(SpringExtension.class)
class PriceServiceTest {
    @TempDir Path tmp;

    @Test
    void rejectsNegativePrice() {
        assertThrows(IllegalArgumentException.class, () -> priceService.set(-1));
    }

    @ParameterizedTest
    @CsvSource({"100, 18, 118", "200, 5, 210"})
    void addsTax(int price, int taxPercent, int expected) {
        assertEquals(expected, priceService.withTax(price, taxPercent));
    }
}

Interview Tip

A concise interview answer is

JUnit 5 splits into Platform, Jupiter, and Vintage, giving it a more modular and extensible architecture than JUnit 4, plus native support for Java 8 lambdas, a unified @ExtendWith extension model, and better parameterized and dynamic test support. I'd choose JUnit 5 for any new project for those reasons, especially given JUnit 4 is now in maintenance mode.

Last updated: 1 November 2026

Previous question: What are alternatives of @Autowired?

Next question: Can you use both application.yml and application.properties in a Spring Boot project? If so, how are they prioritized?

More Spring Boot - Level IV (Advanced) interview questions

All Spring Boot - Level IV (Advanced) questions