Java – Have Maven surefire run on the classpath where module-info.java files exist

Have Maven surefire run on the classpath where module-info.java files exist… here is a solution to the problem.

Have Maven surefire run on the classpath where module-info.java files exist

I’m trying to write a library that works without issues on the module path, but I’m testing with the org.testcontainers package and they have many dependencies that don’t work on the module path.

These are the errors I get when trying to run surefire with my module-info.java:

[WARNING] Can’t extract module name from visible-assertions-2.1.1.jar:
TtyCheck.class found in top-level directory (unnamed package not
allowed in module) [WARNING] Can’t extract module name from
native-lib-loader-2.0.2.jar: native.lib.loader: Invalid module name:
‘native’ is not a Java identifier [WARNING] Can’t extract module name
from junixsocket-native-common-2.0.4.jar: junixsocket.native.common:
Invalid module name: ‘native’ is not a Java identifier

These cause further problems where classes cannot be found.

Note that this is a runtime issue, the code compiles without issues and the generated jar works fine.

As far as I understand the surefire documentation, if the module-info.java file exists, it will try to run tests on the module path.

Is there any way to disable this behavior and avoid maven surefire running tests on the classpath instead of the module path?

Solution

I solved this by adding :

    <configuration>
      <forkCount>0</forkCount>
    </configuration>

To the maven-surefire-plugin plugin section, it gives me the warning [WARNING] useSystemClassloader setting has no effect when not forking.

Related Problems and Solutions