Using Java Modularity (JPMS) in Tests
Examples using TestNG
The Surefire project provides the integration tests with TestNG demonstrating the Java Modularity (JPMS).
The JDK version must be 9 or higher. The POM contains the dependency org.testng:testng:7.1.0
which is an automatic module. It activates the internal provider surefire-testng
in the plugin. The frameworks with assertions API are used, i.e. org.hamcrest:hamcrest:2.2
(automatic module) and org.assertj:assertj-core:3.16.1
(named module).
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>${java.specification.version}</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.16.1</version>
<scope>test</scope>
</dependency>
</dependencies>
The most important parts are the module descriptors. This is the module descriptor located in src/main/java
:
module main
{
exports main;
}
This is the module descriptor in src/test/java
:
module test
{
requires main;
requires org.testng;
requires org.hamcrest;
requires org.assertj.core;
exports test to org.testng;
}
Examples using JUnit4
The Surefire project provides the integration tests with JUnit4 demonstrating the Java Modularity (JPMS).
The JDK version must be 9 or higher. The POM contains the dependency junit:junit:4.13
which is an automatic module. It activates the internal provider surefire-junit4
or surefire-junit47
in the plugin.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>${java.specification.version}</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
The most important parts are the module descriptors. This is the module descriptor located in src/main/java
:
module main
{
exports main;
}
This is the module descriptor in src/test/java
:
module test
{
requires main;
requires junit;
requires org.hamcrest;
exports test to junit;
}
Examples using JUnit5
The Surefire project provides the integration tests with JUnit5 demonstrating the Java Modularity (JPMS) in Maven multi-module project.
The JDK version must be 9 or higher. The POM contains the dependency org.junit.jupiter:junit-jupiter-engine:5.9.1
which is named module. It activates the internal provider surefire-junit-platform
in the plugin.
<artifactId>com.foo.impl</artifactId>
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>com.foo.api</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
The most important parts are the module descriptors. This is the module descriptor located in src/main/java
:
module com.foo.impl
{
exports com.foo.impl;
requires com.foo.api;
requires org.slf4j;
requires org.slf4j.simple;
}
This is the module descriptor in src/test/java
:
open module com.foo.test
{
exports com.foo.implt;
requires com.foo.impl;
requires org.slf4j;
requires org.slf4j.simple;
requires transitive org.junit.jupiter.engine;
requires transitive org.junit.jupiter.api;
}