spring-framework: Classpath scanning does not work in tests using the module path [SPR-16977]

Andy Wilkinson opened SPR-16977 and commented

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:

[INFO] Running com.example.ScanningTest
[file [/Users/awilkinson/dev/temp/module-path-scanning/target/test-classes/com/example/ScanningTest.class]]

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:

[INFO] Running com.example.ScanningTest
[file [/Users/awilkinson/dev/temp/module-path-scanning/target/test-classes/com/example/ScanningTest.class], file [/Users/awilkinson/dev/temp/module-path-scanning/target/classes/com/example/One.class]]

Affects: 5.0.7

Reference URL: https://github.com/spring-projects/spring-boot/issues/13581

Attachments:

Issue Links:

  • #20937 Compatibility with JDK 11

2 votes, 7 watchers

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 26 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I had the same (or very similar) issue running tests with maven-failsafe-plugin 3.0.0-M5 on AdoptOpen JDK 11. The work around is to disable using module path in plugin’s configuration <useModulePath>false</useModulePath>

Another year has passed. To me this still is a showstopper.

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-module project which demonstrates support for @ComponentScan in a patched module using Maven Surefire.

I should mention that I am also suffering from this issue and it would really be appreciated if this could be solved by Spring if it is not going to be solved by Java itself.

FYI, I had similar classpath issues when I introduced java modules in my project and executed the Spring Boot unit tests.

07:28:35.820 [main] ERROR - o.s.test.context.TestContextManager - prepareTestInstance - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@63e5b8aa] to prepare test instance [...]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '...': Unsatisfied dependency expressed through field '...'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '...' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I eventually got the test executed successfully by including within the configuration of SureFire version 2.22.x <forkCount>0</forkCount>.

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.

Please follow those issues for additional updates on module path scanning and resource support.

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.

class ScanningTest {

	@Test
	void scanningTest() throws Exception {
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		System.err.println(Arrays.toString(resolver.getResources("classpath*:com/example/**/*.class")));

		System.err.println("------------------------------------------------");

		String moduleName = getClass().getModule().getName();
		List<String> resourceNames = streamResolvedModules(Predicate.isEqual(moduleName))//
				.map(ResolvedModule::reference)//
				.map(moduleReference -> scanForNames(moduleReference, "^com/example/.+\\.class$"))//
				.flatMap(List::stream)//
				.toList();

		resourceNames.forEach(System.err::println);

		System.err.println("------------------------------------------------");

		resourceNames.forEach(resouceName -> {
			String className = ClassUtils.convertResourcePathToClassName(resouceName);
			className = className.substring(0, className.length() - ".class".length());
			try {
				Class<?> clazz = ClassUtils.forName(className, getClass().getClassLoader());
				System.err.println(clazz);
			}
			catch (Exception ex) {
				throw new RuntimeException("Failed to load class %s".formatted(className), ex);
				// ex.printStackTrace(System.err);
			}
		});

		System.err.println("------------------------------------------------");

		List<URI> resources = streamResolvedModules(Predicate.isEqual(moduleName))//
				.map(ResolvedModule::reference)//
				.map(moduleReference -> scanForResources(moduleReference, "^com/example/.+\\.class$"))//
				.flatMap(List::stream)//
				.toList();

		resources.forEach(resource -> {
			try {
				System.err.println(resource.toURL());
			}
			catch (MalformedURLException ex) {
				ex.printStackTrace();
			}
		});
	}

	private Stream<ResolvedModule> streamResolvedModules(Predicate<String> moduleNamePredicate) {
		ModuleLayer layer = getClass().getModule().getLayer();
		if (layer == null) {
			layer = ModuleLayer.boot();
		}
		return layer.configuration().modules().stream()//
				.filter(module -> moduleNamePredicate.test(module.name()));
	}

	private List<String> scanForNames(ModuleReference reference, String regex) {
		try (ModuleReader reader = reference.open()) {
			try (Stream<String> names = reader.list()) {
				return names.filter(name -> name.matches(regex)).toList();
			}
		}
		catch (IOException ex) {
			throw new UncheckedIOException("Failed to read contents of " + reference, ex);
		}
	}

	private List<URI> scanForResources(ModuleReference reference, String regex) {
		try (ModuleReader reader = reference.open()) {
			try (Stream<String> names = reader.list()) {
				return names.filter(name -> name.matches(regex))//
						.map(name -> {
							try {
								return reader.find(name);
							}
							catch (IOException ex) {
								ex.printStackTrace();
							}
							return Optional.<URI> empty();
						})//
						.filter(Optional::isPresent)//
						.map(Optional::get).toList();
			}
		}
		catch (IOException ex) {
			throw new UncheckedIOException("Failed to read contents of " + reference, ex);
		}
	}

}

The output of running that is:

[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.

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.