`@ApplicationModuleTest` throws a `NullPointerException` when a candidate type has no `@SpringBootApplication` above it
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:
- A test outside the packages of the application does not get the intended message
Couldn't find @SpringBootApplication traversing .... - A test with a
classestype 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 modulecom.example.app.order. com.example.other.OutsideApplicationTestshas@ApplicationModuleTest. No@SpringBootApplicationis incom.example.otheror in its parent packages.com.example.app.order.ClassesOutsideApplicationTestshas@ApplicationModuleTest(classes = TestConfiguration.class).com.example.config.TestConfigurationis a@Configurationin 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
OutsideApplicationTestsfails withIllegalStateException: Couldn't find @SpringBootApplication traversing com.example.other.OutsideApplicationTests.ClassesOutsideApplicationTestscontinues with the next candidate, the test class, findscom.example.app.Application, and passes.
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?