[ 
https://issues.apache.org/jira/browse/SUREFIRE-2191?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17766043#comment-17766043
 ] 

Henning Schmiedehausen commented on SUREFIRE-2191:
--------------------------------------------------

I have two modules, core and testing (these are defined modules, they have a 
module descriptor)
 
"core" is just a regular build, it uses junit5 as its test framework. 
 
surefire creates a classpath with all the surefire deps (surefire-api, 
surefire-extensions, surefire-junit-platform, surefire-logging-api, 
surefire-shared-utils) and the junit deps (junit-jupiter-api, 
junit-jupiter-engine, junit-jupiter-params, junit-platform-commons, 
junit-platform-engine, junit-platform-launcher). The module path only gets the 
actual module dependencies of module A
 
All works well in that case.
 
Now the "testing" module declares org.junit.jupiter.api as a dependency and has 
requires org.junit.jupiter.api in its module descriptor (transitive or not 
transitive does not matter) because there is code that implements some of those 
APIs.
 
surefire now creates a classpath with the same surefire deps and only a subset 
of junit deps. The missing deps (junit-jupiter-api and junit-platform-commons) 
are pulled onto the module path. 
 
This explains the error message: 
 
*java.lang.IllegalAccessError: class 
org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder (in unnamed 
module @0x6392827e) cannot access class 
org.junit.platform.commons.util.Preconditions (in module 
org.junit.platform.commons) because module org.junit.platform.commons does not 
export org.junit.platform.commons.util to unnamed module @0x6392827e*
 
The launcher (in junit-platform-launcher) lives on the classpath and is 
therefore in the unnamed module. But the launcher requires the 
org.junit.platform.commons module from the junit-platform-commons jar, which is 
on the module path. And the junit-platform-commons module has the following 
clause in its module descriptor:
 
exports org.junit.platform.commons.util to
    [...]
    org.junit.platform.launcher,
    [...];

 
and it does not export the package to the unnamed module. And now JVM fails 
with the error above.
 
I can not see any good solution:
 
- The current solution does not work (see above)
- The junit-jupiter-api which contains org.junit.jupiter.api can not go on the 
class path because now the main artifact which requires it can not find it. 
- It can not be patched into the main module because now the 
org.junit.jupiter.api module would be invisible to everything on the module 
path (packages can't exist twice)
- It can only stay on the module path if everything that depends on it gets 
pulled there as well. This requires a "bottom up" resolution of all modules 
that depend on this (in this case org.junit.jupiter.api) and may interfere with 
the ability of the surefire launcher to launch code.- We could fall back to 
"legacy" mode where everything is on the classpath and the module path is 
ignored. This is problematic as tests may pass and code later fails because of 
not matching entries in the module descriptor

> surefire crashes on a JPMS modularized project that uses junit5
> ---------------------------------------------------------------
>
>                 Key: SUREFIRE-2191
>                 URL: https://issues.apache.org/jira/browse/SUREFIRE-2191
>             Project: Maven Surefire
>          Issue Type: Bug
>          Components: JUnit 5.x support, process forking
>    Affects Versions: 3.1.2
>            Reporter: Henning Schmiedehausen
>            Priority: Major
>
> clone this repository: [https://github.com/hgschmie/surefire-2191 
> |https://github.com/hgschmie/surefire-2191] and build it.
> The build fails during testing with a large and cryptic error description.  
> The code only uses the junit jupiter APIs and imports the with {{  requires 
> transitive org.junit.jupiter.api;}}
> This is the surefireargs file that the plugin creates:
> {code}
> --module-path
> "/Users/henning/scratch/surefire-junit-bug/target/classes:/Users/henning/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.10.0/junit-jupiter-api-5.10.0.jar:/Users/henning/.m2/repository/org/opentest4j/opentest4j/1.3.0/opentest4j-1.3.0.jar:/Users/henning/.m2/repository/org/junit/platform/junit-platform-commons/1.10.0/junit-platform-commons-1.10.0.jar:/Users/henning/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar"
> --class-path
> "/Users/henning/.m2/repository/org/apache/maven/surefire/surefire-api/3.1.2/surefire-api-3.1.2.jar:/Users/henning/.m2/repository/org/apache/maven/surefire/surefire-booter/3.1.2/surefire-booter-3.1.2.jar:/Users/henning/.m2/repository/org/apache/maven/surefire/surefire-extensions-spi/3.1.2/surefire-extensions-spi-3.1.2.jar:/Users/henning/.m2/repository/org/apache/maven/surefire/surefire-logger-api/3.1.2/surefire-logger-api-3.1.2.jar:/Users/henning/.m2/repository/org/apache/maven/surefire/surefire-shared-utils/3.1.2/surefire-shared-utils-3.1.2.jar:/Users/henning/scratch/surefire-junit-bug/target/test-classes:/Users/henning/.m2/repository/org/assertj/assertj-core/3.24.2/assertj-core-3.24.2.jar:/Users/henning/.m2/repository/net/bytebuddy/byte-buddy/1.12.21/byte-buddy-1.12.21.jar:/Users/henning/.m2/repository/org/apache/maven/surefire/surefire-junit-platform/3.1.2/surefire-junit-platform-3.1.2.jar:/Users/henning/.m2/repository/org/apache/maven/surefire/common-java5/3.1.2/common-java5-3.1.2.jar:/Users/henning/.m2/repository/org/junit/platform/junit-platform-launcher/1.10.0/junit-platform-launcher-1.10.0.jar:/Users/henning/.m2/repository/org/junit/platform/junit-platform-engine/1.10.0/junit-platform-engine-1.10.0.jar:/Users/henning/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.10.0/junit-jupiter-engine-5.10.0.jar"
> --patch-module
> surefire.junit.bug="/Users/henning/scratch/surefire-junit-bug/target/test-classes"
> --add-opens
> surefire.junit.bug/demo=ALL-UNNAMED
> --add-modules
> surefire.junit.bug
> --add-reads
> surefire.junit.bug=ALL-UNNAMED
> org.apache.maven.surefire.booter.ForkedBooter
> {code}
> It is not clear to me on how to fix/mitigate this error. The same build works 
> fine without JPMS modules or with automatic versioned modules.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to