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:
1 2 3 4 5 6 7 8 9 10 11 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> |
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:
1 2 3 4 5 6 7 |
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- Java version--> <java.version>9</java.version> </properties> |
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:
1 2 3 4 5 6 |
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> |
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.
1 2 3 4 5 6 7 8 9 10 11 |
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>${junit.platform.version}</version> </dependency> </dependencies> </plugin> |
Where detail versions are configured as below:
1 2 3 4 5 6 |
<properties> <junit.jupiter.version>5.0.0</junit.jupiter.version> <junit.platform.version>1.0.0</junit.platform.version> </properties> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Test public void testImmutableCollections() { List<String> fruits = List.of("Mangosteen", "Durian fruit", "Longan"); assertThrows(UnsupportedOperationException.class, () -> { fruits.add("Mango"); fruits.remove(1); }); assertEquals(3, fruits.size()); } |
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:
1 |
mvn test |
Let’s see the output:
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
Tagging and Filtering in JUnit 5
How To Compare Arrays In Java 9