 ------
 Java Modularity (JPMS) in Tests
 ------
 dev@maven.apache.org
 ------
 2020-05-29
 ------

 ~~ Licensed to the Apache Software Foundation (ASF) under one
 ~~ or more contributor license agreements.  See the NOTICE file
 ~~ distributed with this work for additional information
 ~~ regarding copyright ownership.  The ASF licenses this file
 ~~ to you under the Apache License, Version 2.0 (the
 ~~ "License"); you may not use this file except in compliance
 ~~ with the License.  You may obtain a copy of the License at
 ~~
 ~~   http://www.apache.org/licenses/LICENSE-2.0
 ~~
 ~~ Unless required by applicable law or agreed to in writing,
 ~~ software distributed under the License is distributed on an
 ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 ~~ KIND, either express or implied.  See the License for the
 ~~ specific language governing permissions and limitations
 ~~ under the License.

 ~~ NOTE: For help with the syntax of this file, see:
 ~~ http://maven.apache.org/doxia/references/apt-format.html

Using Java Modularity (JPMS) in Tests

* Examples using TestNG

  The Surefire project provides the integration tests with
  {{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/surefire-1733-testng}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
  {{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/surefire-1733-junit4}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
  {{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/maven-multimodule-project-with-jpms}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>${project.version}</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;
}
+---+
