JUnit 5 is the most widely used testing environment for Java applications. JUnit has been doing a great job for a long time.

Meanwhile, JDK 8 brought some interesting features to java and most notably lambda expressions. JUnit 5 was aimed at adapting the Java 8 programming style; this is why Java 8 is the minimum required version to create and run tests in JUnit 5 (although you can run tests written with JUnit 3 or JUnit 4 for backward compatibility).

JUnit 5 architecture

Compared to JUnit 4, JUnit 5 consists of several different modules from three different subprojects:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit Jupiter: includes new programming models and extensions for writing tests. It has all new junit annotations and TestEngine implementation to run tests written with these annotations.

JUnit Platform: to be able to run junit tests, IDEs, build tools or plugins must include and extend the platform API. It defines a TestEngine API for developing new testing frameworks that run on the platform. It also provides a console launcher to run the platform from the command line and create plugins for Gradle and Maven.

JUnit Vintage: its main goal is to support the execution on the JUnit 5 platform of tests written for JUnit 3 and JUnit4 There is backward compatibility.

Installation

You can use JUnit 5 in your Maven or Gradle project by including at least the following dependencies:

junit-jupiter-api: it is the main module where all the main annotations such as @Test, annotations and lifecycle method statements are located.

junit-jupiter-engine: it has the test engine implementation which is required at runtime to execute tests.

junit-platform-suite: the @Suite support provided by this module to make the JUnitPlatform runtime tool deprecated.

Writing test suites

Using JUnit 5 test suites, you can run tests distributed across multiple test classes and different packages. JUnit 5 provides these annotations for creating test suites.

Assumptions

The Assumptions class provides stats methods to support the execution of a conditional test based on assumptions. An unsuccessful assumption causes the test to abort.

Assumptions are typically used whenever it does not make sense to continue executing a given test method. These tests will be marked as passed in the test report.