Skip to content

OLD Prototype JUnit4 Run And Migrate

Marc Philipp edited this page Feb 7, 2017 · 1 revision

Migrating from JUnit 4 to JUnit 5

Although the JUnit 5 programming model and extension model will not support JUnit 4 features such as Rules and Runners, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and custom build test infrastructure to migrate to JUnit 5.

Instead, JUnit 5 provides a gentle migration path via a JUnit 4 test engine which allows existing tests based on JUnit 4 to be executed using the JUnit 5 infrastructure. Since all classes and annotations specific to JUnit 5 reside under a new org.junit.gen5 base package, having both JUnit 4 and JUnit 5 in the classpath does not lead to any conflicts. It is therefore safe to maintain existing JUnit 4 tests alongside JUnit 5 tests. Furthermore, since the JUnit team will continue to provide maintenance and bug fix releases for the JUnit 4.x baseline, developers have plenty of time to migrate to JUnit 5 on their own schedule.

Running JUnit 4 Tests with JUnit 5

Just make sure that the junit4-engine artifact is in your test runtime path. In that case JUnit 4 tests will automatically be picked up by JUnit 5 test runners.

See the example projects in the junit5-samples repository to find out how this is done with Gradle and Maven.

Migration Tips

The following are things you have to watch out for when migrating existing JUnit 4 tests to JUnit 5.

  • Annotations reside in the org.junit.gen5.api package.
  • Assertions reside in org.junit.gen5.api.Assertions.
  • Assumptions reside in org.junit.gen5.api.Assumptions.
  • @Before and @After no longer exist; use @BeforeEach and @AfterEach instead.
  • @BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.
  • @Ignore no longer exists: use @Disabled instead.
  • @Category no longer exists; use @Tag instead.
  • @RunWith no longer exists; superseded by @ExtendWith.
  • @Rule and @ClassRule no longer exist; superseded by @ExtendWith.