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: • JUnit 5's modular design separates the test-running platform from the Jupiter programming model, allowing other testing frameworks to plug into the same platform. • JUnit 5 supports Java 8+ features natively, including lambda expressions in assertions like assertThrows and assertAll. • The extension model (@ExtendWith) replaces JUnit 4's Runners and Rules with a single, more flexible mechanism. • JUnit 5 has first-class support for parameterized tests via @ParameterizedTest and dynamic tests via @TestFactory. • The Vintage engine lets JUnit 5 still run existing JUnit 4 tests, easing incremental migration.
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.
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."