Before migrating to JUnit 5 from JUnit 4, let’s first check out the advantages of using JUnit 5 over JUnit 4.
- JUnit 4 entire framework was contained in a single JAR library. This means that we have to import the whole library even when only a particular feature set is to be used. In JUnit 5, we can get and import the components that we require for testing purpose.
- In JUnit 4 at a time only one test runner can execute tests (eg., SpringJUnit4ClassRunner or Parameterized). In JUnit 5, multiple runners are allowed to work simultaneously.
- JUnit 4 never advanced beyond Java 8. JUnit 5 on the other hand takes full advantages of all the features of Java 8.
As stated earlier, JUnit 5 is composed of several different modules from three different sub-projects.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform. Furthermore, the platform provides a Console Launcher to launch the platform from the command line and build plugins for Gradle and Maven as well as a JUnit 4 based Runner for running any TestEngine on the platform.
JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.
JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.
The following are things you have to watch out for when migrating existing JUnit 4 tests to JUnit 5.
Annotations in JUnit 5 reside in the
Assertions reside in
Assumptions reside in
Some of the Annotations that were changed in JUnit 5 are:
@Before no longer exist; use @BeforeEach instead.
@After no longer exist; use @AfterEach instead.
@BeforeClass no longer exist; use @BeforeAll instead.
@AfterClass no longer exist; use @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.