You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Classpath scanning does not work in tests using the module path [SPR-16977] #21515
in: coreIssues in core modules (aop, beans, core, context, expression)in: testIssues in the test modulestatus: supersededAn issue that has been superseded by another
It appears the classpath scanning doesn't work when Surefire launches the JVM configured to use the module path. target/classes is placed on the module path and target/test-classes is patched into this module but classpath scanning only finds classes in target/test-classes.
I have attached a minimal sample that should reproduce the problem when built (mvn test) with Java 10. The sysout from the test should show that only the test class has been found:
To my rather untrained eye, building with -X and examining the arguments that Surefire uses to launch the forked JVM (in target/surefire) suggests that Surefire's configuration of the JVM is correct.
When the sample is modified to work with Java 8 (remove module-info.java and change the compiler plugin configuration to remove <release>10</release>) the class in target/classes is also found:
This all depends on ClassLoader.getResources results for the given base package. The module system possibly only exposes the root URL for the patched part there? In any case, debugging ClassLoader.getResources results in both scenarios would help here...
I've reported a bug. It's in internal review at the moment (ID 9055803). I'll comment again here if it makes it out the other end and becomes public. For reference, this is the test case that I provided with the bug report. Unlike the sample attached to this issue, it does not use Spring Framework.
The bug is now public but has been closed as not an issue. The situation's now being discussed in this thread on the core libs dev OpenJDK mailing list.
The thread on the mailing list seems to have reached a conclusion. The following recommendation came from Alan Bateman:
With modules then it should be looking at the modules in the boot layer and using ModuleReader to get the contents. It can use the value of java.class.path to scan the class path.
Are there any plans to resolve this like recommended?
Would doFindAllClassPathResources(String path) be the point to put that? I don't clearly understand that suggestion, but maybe I could invest some time over the holidays.
Is there a workaround for this? I have a JavaFx project that I added Spring Boot to which works really great, but I am unable to run my @JsonTest.
As a workaround, I need to manually specify the application class + the json deserializers I want to test:
@JsonTest
class MyJsonDeserializerTest {
...
}
needs to become:
@JsonTest
@ContextConfiguration(classes = {MyApplication.class, MyJsonDeserializerTest.TestConfig.class})
class MyJsonDeserializerTest {
...
@TestConfiguration
static class TestConfig {
@Bean
public MyJsonDeserializer deserializer() {
return new MyJsonDeserializer();
}
}
}
(Note: It seems I only need to do this for Maven, IntelliJ seems to run the test fine, but I have to assume that IntelliJ is somehow "cheating" given this issue and the related spring-projects/spring-boot#13581 )
I have the opposite problem currently. Failsafe works just fine, apparently because they point to the packaged JAR instead of the exploded target/classes directory. However, IntelliJ wants to use the exploded directory when running integration tests in the IDE, so this fails unless I uncheck the module-path option (which I have to do for every run configuration separately).
Additionally, I can make Failsafe break the same way as the IDE by forcing it to use the exploded directory (via <classesDirectory>${project.build.outputDirectory}</classesDirectory>), so there's something magic about being in a JAR versus hanging around on the filesystem -- but I'm not clear on what.
Is this truly a JDK issue or a Spring issue? It doesn't seem that the core JDK team plans on changing behavior, thus it seems incumbent on the Spring team to work around this somehow (or at the very least, document that component scanning will break unexpectedly when using exploded classes versus packaged in JAR classes....)
@jhoeller@wilkinsona could this be fixed inside spring by making the component scanning code be more module path aware? Especially for running spring tests on module path using the surefire plugin or in intellij this is a problem.
For future reference; the only work around I found was to uncheck "use module path" in Templates > JUnit in your Run/Debug Generations to make sure it is unchecked for every new junit run config you create.
Disclaimer: This is in no way production ready code. This is 100% "proof of concept" code.
Now, having said that... with an updated version of the original example application from @wilkinsona (now using Java 17, Maven 3.8.5, Maven Surefire 3.0.0-M6, Spring Framework 5.3.20, and JUnit Jupiter 5.8.2), I have come up with the following.
[file [/Users/sbrannen/source/spring-issues/module-path-scanning/target/test-classes/com/example/ScanningTest.class]]
------------------------------------------------
com/example/One.class
com/example/ScanningTest.class
------------------------------------------------
class com.example.One
class com.example.ScanningTest
------------------------------------------------
file:/Users/sbrannen/source/spring-issues/module-path-scanning/target/classes/com/example/One.class
file:/Users/sbrannen/source/spring-issues/module-path-scanning/target/test-classes/com/example/ScanningTest.class
The first output demonstrates the issue: PathMatchingResourcePatternResolver only finds ScanningTest.class and not One.class.
The final output demonstrates that we can find resources (as instances of URI) using the Module APIs and successfully find both ScanningTest.class and One.class.
Kudos to @sormuras for providing inspirational use of the java.lang.module APIs in JUnit 5!
Where to go from here?
We may introduce a new ModulePathResource as a companion to ClassPathResource, where the logic in ModulePathResource is similar to the logic in the above proof of concept.
We may then introduce additional support in PathMatchingResourcePatternResolver that resolves ModulePathResource instances when appropriate.
That repository currently contains a maven-surefire-patched-module project which demonstrates support for @ComponentScan in a patched module using Maven Surefire.
in: coreIssues in core modules (aop, beans, core, context, expression)in: testIssues in the test modulestatus: supersededAn issue that has been superseded by another
Activity
spring-projects-issues commentedon Jun 26, 2018
Juergen Hoeller commented
This all depends on
ClassLoader.getResourcesresults for the given base package. The module system possibly only exposes the root URL for the patched part there? In any case, debuggingClassLoader.getResourcesresults in both scenarios would help here...spring-projects-issues commentedon Jun 27, 2018
Andy Wilkinson commented
On closer inspection, this appears to be a bug in the JDK. With the test changed to the following:
It produces output similar to this:
Note the two (slightly different) URLs that are both pointing to
target/test-classes. This explains why no classes intarget/classesare found.spring-projects-issues commentedon Jun 27, 2018
Juergen Hoeller commented
Looks like a bug in the
ClassLoaderimplementation indeed. They might not have fully tested this in module patch scenarios...Probably worth reporting to OpenJDK. Aside from a proper fix, maybe there's a workaround to discover in time for our GA still.
spring-projects-issues commentedon Jun 27, 2018
Andy Wilkinson commented
I've reported a bug. It's in internal review at the moment (ID 9055803). I'll comment again here if it makes it out the other end and becomes public. For reference, this is the test case that I provided with the bug report. Unlike the sample attached to this issue, it does not use Spring Framework.
spring-projects-issues commentedon Jul 3, 2018
Andy Wilkinson commented
The bug is now public but has been closed as not an issue. The situation's now being discussed in this thread on the core libs dev OpenJDK mailing list.
spring-projects-issues commentedon Jul 3, 2018
Andy Wilkinson commented
The thread on the mailing list seems to have reached a conclusion. The following recommendation came from Alan Bateman:
spring-projects-issues commentedon Dec 16, 2018
Thomas Kratz commented
Are there any plans to resolve this like recommended?
Would doFindAllClassPathResources(String path) be the point to put that? I don't clearly understand that suggestion, but maybe I could invest some time over the holidays.
I would still think this is a JDK issue.
wimdeblauwe commentedon Sep 9, 2019
Is there a workaround for this? I have a JavaFx project that I added Spring Boot to which works really great, but I am unable to run my
@JsonTest.As a workaround, I need to manually specify the application class + the json deserializers I want to test:
needs to become:
(Note: It seems I only need to do this for Maven, IntelliJ seems to run the test fine, but I have to assume that IntelliJ is somehow "cheating" given this issue and the related spring-projects/spring-boot#13581 )
eiswind commentedon Dec 28, 2019
Another year has passed.
To me this still is a showstopper.
tgolden-andplus commentedon Feb 19, 2020
I have the opposite problem currently. Failsafe works just fine, apparently because they point to the packaged JAR instead of the exploded
target/classesdirectory. However, IntelliJ wants to use the exploded directory when running integration tests in the IDE, so this fails unless I uncheck the module-path option (which I have to do for every run configuration separately).Additionally, I can make Failsafe break the same way as the IDE by forcing it to use the exploded directory (via
<classesDirectory>${project.build.outputDirectory}</classesDirectory>), so there's something magic about being in a JAR versus hanging around on the filesystem -- but I'm not clear on what.Is this truly a JDK issue or a Spring issue? It doesn't seem that the core JDK team plans on changing behavior, thus it seems incumbent on the Spring team to work around this somehow (or at the very least, document that component scanning will break unexpectedly when using exploded classes versus packaged in JAR classes....)
tomdw commentedon Feb 25, 2020
@jhoeller @wilkinsona could this be fixed inside spring by making the component scanning code be more module path aware? Especially for running spring tests on module path using the surefire plugin or in intellij this is a problem.
PeterMylemans commentedon Mar 7, 2020
@tgolden-andplus I'm having the same issue.
For future reference; the only work around I found was to uncheck "use module path" in Templates > JUnit in your Run/Debug Generations to make sure it is unchecked for every new junit run config you create.
21 remaining items
sbrannen commentedon May 16, 2022
Disclaimer: This is in no way production ready code. This is 100% "proof of concept" code.
Now, having said that... with an updated version of the original example application from @wilkinsona (now using Java 17, Maven 3.8.5, Maven Surefire 3.0.0-M6, Spring Framework 5.3.20, and JUnit Jupiter 5.8.2), I have come up with the following.
The output of running that is:
The first output demonstrates the issue:
PathMatchingResourcePatternResolveronly findsScanningTest.classand notOne.class.The final output demonstrates that we can find resources (as instances of
URI) using theModuleAPIs and successfully find bothScanningTest.classandOne.class.Kudos to @sormuras for providing inspirational use of the
java.lang.moduleAPIs in JUnit 5!Where to go from here?
We may introduce a new
ModulePathResourceas a companion toClassPathResource, where the logic inModulePathResourceis similar to the logic in the above proof of concept.We may then introduce additional support in
PathMatchingResourcePatternResolverthat resolvesModulePathResourceinstances when appropriate.sbrannen commentedon May 22, 2022
Current work on this issue can be viewed in the following feature branch.
https://github.com/sbrannen/spring-framework/commits/module-path-scanning
sbrannen commentedon May 23, 2022
Hi everybody,
Thanks for all of the feedback over the years!
I am now closing this issue since it has been superseded by the following issues.
ModuleResourcefor resources loaded from a givenModule#28507Please follow those issues for additional updates on module path scanning and resource support.
sbrannen commentedon May 24, 2022
If you've been following this issue, I'm happy to let you know that #28506 has been resolved for inclusion in 6.0 M5.
In the interim, feel free to try out 6.0 snapshots with modular testing using Maven.
I've created a new repository for demonstrating the use of the Spring Framework with the Java Module System: https://github.com/sbrannen/spring-module-system
That repository currently contains a
maven-surefire-patched-moduleproject which demonstrates support for@ComponentScanin a patched module using Maven Surefire.About as working as I can make things for now