Uploaded image for project: 'Maven Surefire'
  1. Maven Surefire
  2. SUREFIRE-1420

Support running unit tests in named Java 9 modules

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      As far as I can tell, Surefire will run tests using the traditional classpath also on Java 9. When the project that is built is a named Java 9 module (i.e. it contains a module descriptor), it'd be desirable to have a way to run the tests using the module path instead.

      For that, the following things would be required:

      • Adding the currently built project and its dependencies to the module path instead of the class path when forking the java process running the tests
      • Adding a read edge from the currently built module to the used testing library, e.g. JUnit, if it's not present as a dependence in the module descriptor already (which it usually won't)
      • Patching the classes from target/test-classes into the currently built module (as represented by the classes under target/classes)
      • Opening up packages containing tests to the testing library so they can be accessed reflectively

      I could perform these steps manually by applying the following configuration for the Surefire plug-in:

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
          <configuration>
              <!-- Don't add any dependencies to the class path -->
              <classpathDependencyScopeExclude>test</classpathDependencyScopeExclude>
      
              <argLine>--module-path=target/classes:/path/to/junit/junit/4.12/junit-4.12.jar:
                                     /path/to/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
                       --add-modules com.example,junit,hamcrest.core
                       --add-reads com.example=junit
                       --add-opens com.example/com.example.internal=junit
                       --patch-module com.example=target/test-classes</argLine>
              
              <!-- Prevents target/classes to be added to the classpath -->                
              <classesDirectory>path/to/none</classesDirectory>
      
              <!-- Doing the same for test classes failed as it caused no tests to be found; it still works, 
                    I assume the --patch-module above takes precedence over their occurrence on the classpath -->
              <!-- <testClassesDirectory>path/to/none</testClassesDirectory> -->
          </configuration>
      </plugin>
      

      Ideally, an equivalent java invocation would be done by Surefire automatically when building a named module.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                rfscholte Robert Scholte
                Reporter:
                gunnar.morling Gunnar Morling
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: