Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News JUnit 5 Released

JUnit 5 Released

This item in japanese

JUnit 5 has just been released. This is the first major release since JUnit 4, which was released in 2006. According to the user guide, one of the major goals of JUnit 5 is to make future enhancements and evolution of JUnit easier.

With JUnit 4 a lot of stuff that was originally added as an internal construct only got used by external extension writers and tool builders. That made changing JUnit 4 especially difficult and sometimes impossible.

JUnit 5 team addressed this issue by redesigning the testing framework and dividing it into two main areas:

  1. A set of API for writing tests.
  2. JUnit platform launcher API that can be used by tool builders to discover, filter, and execute tests.

JUnit 5 is a collection of three sub-projects: JUnit Jupiter, JUnit Platform and JUnit Vintage (for backwards compatibility).

JUnit Jupiter provides the API for writing tests and extensions. It consists of some familiar annotations from JUnit 4 such as @Test, and some new annotations such as @DisplayName. Some annotations are renamed.

JUnit 5 JUnit 4
@BeforeEach @Before
@AfterEach @After
@BeforeAll @BeforeClass
@AfterAll @AfterClass
@Disabled @Ignore

Consult the user guide for a complete list of annotations available in JUnit 5.

JUnit 5 includes new assertions such as assertAll that work well with Java 8 lambdas.

public class SampleTest { 
    void groupedAssertions() {
        // In a grouped assertion all assertions are executed, and any
        // failures will be reported together.
            () -> assertEquals("John", person.getFirstName()),
            () -> assertEquals("Doe", person.getLastName())

JUnit 5 also introduces the concept of nested tests, which allows the developer to group related tests.

The extension model of JUnit 5 replaces Runner, @Rule, and @ClassRule extension points in JUnit 4 with a single Extension API. Extensions can be registered explicitly by using @ExtendWith or automatically via Java's ServiceLoader mechanism.

void mockTest() {
    // ...

JUnit Platform provides the foundation for discovering, filtering, and executing tests.

From the user guide:

One of the prominent goals of JUnit 5 is to make the interface between JUnit and its programmatic clients – build tools and IDEs – more powerful and stable. The purpose is to decouple the internals of discovering and executing tests from all the filtering and configuration that's necessary from the outside.

This goal is met by JUnit's Launcher API, which is typically used by IDEs and build tools for configuring and launching test plans. A third-party test library can also plug into JUnit Platform's launching infrastructure by providing a custom TestEngine.

ConsoleLauncher, a command-line Java application lets the developer launch the JUnit Platform from the console.

JUnit Vintage is a test engine that allows a developer to run tests written in JUnit 3 or JUnit 4, on the new platform. A developer must include junit-vintage-engine in the test runtime path, in which case the legacy tests will automatically be picked up by the JUnit Platform launcher.

This release requires Java 8 (or higher) at runtime, underlining the trend where modern versions of major libraries now consider Java 8 a minimum version requirement.

JUnit 5 is one of the first projects to be released under EPL v2.0 license.

Rate this Article