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 enable 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:
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.1.0</version>
    <scope>test</scope>
</dependency>
However, at the moment no IDEs support JUnit 5 yet, so you will also have to specify a build script:
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.2</version>
        </dependency>
    </dependencies>
</plugin>
It is important to note that this version requires Java 8 to work.

4. Creating Our First JUnit Test

Let's use @Test annotation to identify the below test() method as JUnit test method so that the JUnit framework will execute as a test:
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class FirstJunit5Test {

	@Test
	void test() {
		fail();
	}

}

DEEP DIVE INTO JUNIT 5 FRAMEWORK

Comments