Video in progress

Strong Junior 🧪 Testing

What main changes do you run into when moving from JUnit 4 to JUnit 5?

The package org.junit became org.junit.jupiter.api, @Before/@After became @BeforeEach/@AfterEach, a single @ExtendWith replaced @RunWith and @Rule, and assertThrows replaced @Test(expected). Old tests can keep running alongside through the Vintage engine.

📖 Answer

JUnit 5 has three parts: Platform (the launcher), Jupiter (the new API) and Vintage (runs old JUnit 4 tests).

The main replacements:

JUnit 4 JUnit 5
@Before, @After @BeforeEach, @AfterEach
@BeforeClass, @AfterClass @BeforeAll, @AfterAll
@Ignore @Disabled
@RunWith(SpringRunner.class) @ExtendWith(SpringExtension.class)
@Rule TemporaryFolder @TempDir Path dir
@Test(expected = X.class) assertThrows(X.class, ...)

A small but distracting difference: in JUnit 5 test classes and methods don't have to be public.

Another trap: with Assert.assertEquals(message, expected, actual) the message comes first in JUnit 4 and last in JUnit 5. An automatic replacement turns this into a silent bug.

Since Spring Boot 2.2, spring-boot-starter-test comes with JUnit 5. From Boot 2.4 the Vintage engine is no longer included by default, so if you don't know you have old tests, they quietly stop running in CI. To migrate step by step, you can temporarily add junit-vintage-engine.