Overview of JUnit 5

1. Overview

JUnit 5 is the next generation of JUnit 4. The main goal of JUnit 5 to support Java 8 and above, as well as enabling many different styles of testing. JUnit 5 is the result of JUnit Lambda.

2. What is JUnit 5?

JUnit 5 is composed of several different modules from three different sub-projects.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

2.1 JUnit Platform

  • 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.
2.2 JUnit Jupiter
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.

2.3 JUnit Vintage

JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

2.4 Supported Java Versions

JUnit 5 requires Java 8 (or higher) at run-time. However, you can still test code that has been compiled with previous versions of the JDK.

2.5 How to Run JUnit 4 Tests on the JUnit Platform?

Just make sure that the junit-vintage-engine artifact is in your test runtime path. In that case JUnit 3 and JUnit 4 tests will automatically be picked up by the JUnit Platform launcher.

2.6 JUnit 5 Advantages

Let’s start with the previous version – JUnit 4 has some clear limitations:
  • The entire framework was contained in a single jar library. The whole library needs to be imported even when only a particular feature is required. In JUnit 5, we get more granularity and can import only what is necessary
  • One test runner can only execute tests in JUnit 4 at a time (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously
  • JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of Java 8 features The idea behind JUnit 5 was to completely rewrite JUnit 4 to solve most of these drawbacks

3. Dependencies and Setup

Installing JUnit 5 is pretty straightforward. Just add the following dependency to your pom.xml:
However, at the moment no IDEs support JUnit 5 yet, so you will also have to specify a build script:
It is important to note that this version requires Java 8 to work.
When creating a test, make sure you import org.junit.jupiter.api.Test, not org.junit.Test. Also, the test methods no longer need to be public; package local will do.

4. How to migrate JUnit 4 to JUnit 5?

JUnit provides a gentle migration path via a **JUnit ** Vintage test engine which allows existing tests based on JUnit 3 and JUnit 4 to be executed using the JUnit Platform infrastructure. Since all classes and annotations specific to JUnit Jupiter reside under a new org.junit.jupiter base package, having both JUnit 4 and JUnit Jupiter in the classpath does not lead to any conflicts.
It is therefore safe to maintain existing JUnit 4 tests alongside JUnit Jupiter 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 Jupiter on their own schedule.

4.1 Migration Tips

The following are things you have to watch out for when migrating existing JUnit 4 tests to JUnit Jupiter.
  • Annotations reside in the org.junit.jupiter.api package.
  • Assertions reside in org.junit.jupiter.api.Assertions.
  • Assumptions reside in org.junit.jupiter.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; see the following section for partial rule support.
Read more about migration from JUnit 4 to JUnit 5 on 

5. Conclusion

In this post, we have seen what are all difference between JUnit 4 and JUnit. What are the enhancement made in JUnit 5 that is JUnit supports Java 8 and above.