JUnit 5 Quick Features and Getting Started Example

[Updated: Nov 11, 2017, Created: Nov 11, 2017]

JUnit 5 is an upgrade of JUnit 4 with an entirely separate codebase. It brings the following main features:

  • (1)Java 8 Support

    The new assert methods now uses functional parameters hence utilizing Java 8 lambadas for testing. JUnit 5 requires Java 8 (or higher) runtime. The code compiled in previous version of Java can still run with JUnit 5.

  • (2)Modules

    Unlike previous versions, JUnit 5 is composed of several different modules:
    • JUnit Platform: It is JUnit core which allows test execution.
    • JUnit Jupiter: JUnit 5 API (new) and a combination of new programming model and extension model for writing tests.
    • JUnit Vintage: It provides a TestEngine which bridges JUnit 3/JUnit 4 tests to JUnit 5 platform. This means previous versions of JUnit can coexist with JUnit 5.


  • (3)Extension Model

    JUnit 5 is a complete redesign. In contrast to the older Runner, @Rule, and @ClassRule concepts, the JUnit Jupiter extension model consists of a single, coherent concept: the Extension API. The new model resolves various shortcomings of older versions.

We will explore above features in details in future tutorials. Following is a JUnit 5 quick getting started example.

Example

Maven Dependency

pom.xml

<dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-engine</artifactId>
   <version>5.0.1</version>
</dependency>

The above dependency also pulls the JUnit 5's platform dependency transitively.

A Java class to test

package com.logicbig.example;

public class Calc {
  public int multiply(int a, int b) {
      return a * b;
  }
}

Writing tests

package com.logicbig.example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CalcTest {
  @Test
  public void testMultiply() {
      Calc c = new Calc();
      int x = 3;
      int y = 5;
      int expected = 15;
      int actual = c.multiply(x, y);
      Assertions.assertEquals(expected, actual,
              () -> String.format("%s * %s != %s", x, y, expected));
  }
}

As seen above, the utility class Assertions has replaced the old org.junit.Assert class. Assertions has various methods which accept Java 8 functional parameters (lambdas). We can see the advantage of initializing the error message lazily (only when the assertion fails) by using Java 8 lambda. Following is the method definition which we used above:

public static void assertEquals(int expected,
                                int actual,
                                Supplier<String> messageSupplier)

Running the test

Running in Intellij

The latest versions of Intellij already support JUnit 5. Running above test (IntelliJ Community version 2017.2)


Running in maven

D:\junit-5-getting-started>mvn -q test -Dtest=CalcTest

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.CalcTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Example Project

Dependencies and Technologies Used :

  • junit-jupiter-engine 5.0.1: Module "junit-jupiter-engine" of JUnit 5.
  • JDK 1.8
  • Maven 3.3.9

Getting Started with JUnit 5 Example Select All Download
  • junit-5-getting-started
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example

See Also