Running a WebMvcTest in a native image fails with
Failures (1):
JUnit Jupiter:DemoControllerTests
ClassSource [className = 'com.example.demo.DemoControllerTests', filePosition = null]
=> java.lang.IllegalStateException: Unable to instantiate DeterminableImportSelector org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelector
org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.instantiate(ImportsContextCustomizer.java:316)
org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.determineImports(ImportsContextCustomizer.java:296)
org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.determineImports(ImportsContextCustomizer.java:276)
org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.<init>(ImportsContextCustomizer.java:236)
org.springframework.boot.test.context.ImportsContextCustomizer.<init>(ImportsContextCustomizer.java:71)
[...]
Caused by: java.lang.NoSuchMethodException: org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelector.<init>()
java.lang.Class.getConstructor0(DynamicHub.java:3585)
java.lang.Class.getDeclaredConstructor(DynamicHub.java:2754)
org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.instantiate(ImportsContextCustomizer.java:311)
[...]
This can be reproduced using this sample. This code seems to be required to build a suitable MergedContextConfiguration but I wonder if it should be running at all since the context has already been fully prepared at this point. See also #32420
Comment From: snicoll
A note that Spring Native has an hard-coded list of ContextCustomizer implementations to ignore at runtime:
private static final Predicate<SpringFactory> CONTEXT_CUSTOMIZER_FACTORY = factoryEntry(
"org.springframework.test.context.ContextCustomizerFactory",
"org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory",
"org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory",
"org.springframework.boot.test.context.ImportsContextCustomizerFactory");
Comment From: snicoll
So it turns out that disabling the 3 implementations above were enough to fix the sample. It's a good first step in the right direction but we should be discussing how to make that a bit more declarative/obvious. This should be addressed by https://github.com/spring-projects/spring-boot/issues/32262