spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

`@ApplicationModuleTest` throws a `NullPointerException` when a candidate type has no `@SpringBootApplication` above it

Open nikolamicic21 opened this issue 2 weeks ago • 1 comments

ModuleTestExecution looks for the @SpringBootApplication class from the classes of @ApplicationModuleTest and from the test class. When the lookup finds no application for the first candidate, the test fails with a bare NullPointerException. This has two effects:

  1. A test outside the packages of the application does not get the intended message Couldn't find @SpringBootApplication traversing ....
  2. A test with a classes type outside the packages of the application fails, although the test class itself finds the application.

Reproducer

A minimal Maven project, attached as spring-modulith-application-lookup-npe-repro.zip. Run mvn test.

spring-modulith-application-lookup-npe-repro.zip

  • The application is com.example.app.Application, with the module com.example.app.order.
  • com.example.other.OutsideApplicationTests has @ApplicationModuleTest. No @SpringBootApplication is in com.example.other or in its parent packages.
  • com.example.app.order.ClassesOutsideApplicationTests has @ApplicationModuleTest(classes = TestConfiguration.class). com.example.config.TestConfiguration is a @Configuration in the test sources, outside the packages of the application.

Actual result

Both tests fail with the same exception:

java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:220)
	at java.base/java.util.Optional.of(Optional.java:113)
	at java.base/java.util.stream.FindOps$FindSink$OfRef.get(FindOps.java:194)
	...
	at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:683)
	at org.springframework.modulith.test.ModuleTestExecution.scanForClassAnnotatedWithSpringBoot(ModuleTestExecution.java:317)
	at org.springframework.modulith.test.ModuleTestExecution.lambda$findSpringBootApplicationByClasses$8(ModuleTestExecution.java:301)
	at java.base/java.util.Optional.or(Optional.java:313)
	at org.springframework.modulith.test.ModuleTestExecution.findSpringBootApplicationByClasses(ModuleTestExecution.java:301)
	at org.springframework.modulith.test.ModuleTestExecution.lambda$of$7(ModuleTestExecution.java:139)
	at org.springframework.util.function.SingletonSupplier.get(SingletonSupplier.java:114)
	at org.springframework.modulith.test.ModuleTypeExcludeFilter.hashCode(ModuleTypeExcludeFilter.java:78)
	at org.springframework.boot.test.context.filter.annotation.TypeExcludeFiltersContextCustomizer.instantiateTypeExcludeFilters(TypeExcludeFiltersContextCustomizer.java:43)
	...

The exception has no message. It occurs while Spring builds the test context configuration, so JUnit reports it on the test class.

Expected result

  1. OutsideApplicationTests fails with IllegalStateException: Couldn't find @SpringBootApplication traversing com.example.other.OutsideApplicationTests.
  2. ClassesOutsideApplicationTests continues with the next candidate, the test class, finds com.example.app.Application, and passes.

nikolamicic21 avatar Nov 19 '26 13:11 nikolamicic21

I looked into this, and the NPE seems to come from ModuleTestExecution.scanForClassAnnotatedWithSpringBoot(…). lookupSpringBootApplicationAnnotation(…) returns null when AnnotatedClassFinder can’t find a @SpringBootApplication above a candidate. That null then reaches Stream.findFirst(), which throws an NPE before the orElseThrow(…) in findSpringBootApplicationByClasses(…) can produce the intended IllegalStateException.

A possible fix:

Filter out null lookup results, e.g. .map(…).filter(Objects::nonNull).findFirst(). Unusable candidates are then skipped and the test class is still tried, which covers the second scenario. If nothing is found, the existing “Couldn’t find @SpringBootApplication traversing …” message is reported, which covers the first scenario. Add tests for both scenarios: a classes type outside the application’s packages that falls back to the test class, and no application found at all, which should raise the IllegalStateException.

If this is open for contribution, I’d be happy to submit a PR. Would that be welcome?

MinChul-Son avatar Nov 20 '26 01:11 MinChul-Son