This quick tutorial is going to cover how to set up and run a Java 9 example with Maven and JUnit 5. We will get started with adding configurations for Java 9 and Maven, then JUnit 5 capabilities.

1. Prerequisites

  • Java 9 installed in your environment. If you haven’t had it yet, below are tutorials you can get started with:

Install Oracle Java 9 on CentOS, RHEL 7

Install Oracle Java 9 on Ubuntu 16.04 LTS (Xenial Xerus)

Set Up Eclipse, IntelliJ And NetBeans For Java 9

Notice that we should set Java environment variable, JAVA_HOME=/path/to/jdk-9.

  • Maven (3.3.1 or later is recommended).

2. Java 9 Example With Maven And JUnit 5

2.1. Configure Maven Compiler Plugin

In order to use Maven to build Java 9 sources, we can configure the Maven Compiler Plugin as follow:

Notice that the version of the plugin is the latest version in current: 3.7.0 and the ${java.version} is set to the Java 9 as below:

If you just need a Java 9 and Maven example, then the above configuration is enough. However, if you need to add JUnit 5 capabilities, then you will need to add the JUnit 5 dependencies.

Here you can find the full Maven pom.xml in my Github project.

2.2. JUnit 5 Maven Dependency and Plugin

Let’s see Maven dependency for JUnit 5 as follows:

Next, we need to configure maven-surefire-plugin which is used during the test phase of the build lifecycle to execute the unit tests and generates result reports.

Where detail versions are configured as below:

If you want to learn more about JUnit 5, you can find more tutorials and examples at JUnit 5 Tutorial.

2.3. Java 9 And JUnit 5 Example

In this part, let’s see some basic examples of Java 9 and JUnit 5:

In the above example, we have to use a convenience static factory method on the List interface, which was added in JDK 9, let us easily create an immutable list. In addition, we also use the assertThrows annotation to assert whether a given exception is thrown or not. If you’re interested in JUnit 5, you can find more tutorials and examples at JUnit 5 Tutorial.

As mentioned above, you can find the full Maven pom.xml in my Github project.

2.4. Running Java 9 And JUnit 5 Tests With Maven

To run JUnit tests with Maven, we simply need to execute the mvn test command:

Let’s see the output:

Java 9 Example With Maven And JUnit 5 - Running JUnit Tests

JUnit 5 test results with Java 9 and Maven

4. Conclusions

The tutorial has illustrated how to quickly set up and run a Java 9 example with Maven and JUnit 5. The sources, sample project used in the tutorial can be found on my Github project. It’s a Maven based project, it should be easy to be imported into IDE such as Eclipse or IntelliJ.

Below are other related tutorials:

Private Interface Methods In Java 9

JUnit 5 Basic Introduction

JUnit 5 vs JUnit 4

JUnit 5 Exception Testing

JUnit 5 Nested Tests Examples

Timeout Test in JUnit 5

Tagging and Filtering in JUnit 5

Java 9 Tutorial

How To Compare Arrays In Java 9

0 0 vote
Article Rating