From 352087a637905e2c3c110ce20a278f572f6a416d Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 26 Nov 2022 15:32:16 +0100 Subject: [PATCH] Forbid loading of test ApplicationContext in AOT mode if AOT processing failed Prior to this commit, if AOT processing of a test's ApplicationContext failed, the TestContext Framework (TCF) still attempted to load the context via standard (JVM) mechanisms when running in AOT mode. For example, if a test class uses Spring Boot's @MockBean, AOT processing of that test's context will fail with a WARN log message, and there will no mapping from that test class to an AOT-generated ApplicationContextInitializer (ACI). Consequently, when the test suite is run in AOT mode that particular test class will currently fail with a confusing stack trace due to the fact that Spring Boot's SpringApplication attempts to locate a "main" ACI instead of the missing "test" ACI (missing because it was not generated during AOT processing). In general, the TCF should not attempt to load an ApplicationContext in "JVM mode" while running in "AOT mode". This commit therefore reworks the logic in DefaultCacheAwareContextLoaderDelegate to fail fast (with a meaningful error message) if an AOT-generated ACI cannot be found while running in AOT mode. This avoids the aforementioned confusion when @MockBean tests fail in AOT mode (on the JVM or within a native image), and it also helps to diagnose build problems if AOT processing has not yet been performed for the project's test suite. Closes gh-29579 --- .../DefaultCacheAwareContextLoaderDelegate.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java index 608b78b30f67..1278b3388e16 100644 --- a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java +++ b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.aot.AotDetector; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -239,14 +240,23 @@ private ContextLoader getContextLoader(MergedContextConfiguration mergedConfig) * unmodified. *

This allows for transparent {@link org.springframework.test.context.cache.ContextCache ContextCache} * support for AOT-optimized application contexts. + * @param mergedConfig the original {@code MergedContextConfiguration} + * @return {@code AotMergedContextConfiguration} or the original {@code MergedContextConfiguration} + * @throws IllegalStateException if running in AOT mode and the test class does not + * have an AOT-optimized {@code ApplicationContext} * @since 6.0 */ @SuppressWarnings("unchecked") private MergedContextConfiguration replaceIfNecessary(MergedContextConfiguration mergedConfig) { - Class testClass = mergedConfig.getTestClass(); - if (this.aotTestContextInitializers.isSupportedTestClass(testClass)) { + if (AotDetector.useGeneratedArtifacts()) { + Class testClass = mergedConfig.getTestClass(); Class> contextInitializerClass = this.aotTestContextInitializers.getContextInitializerClass(testClass); + Assert.state(contextInitializerClass != null, () -> """ + Failed to load AOT ApplicationContextInitializer class for test class [%s]. \ + This can occur if AOT processing has not taken place for the test suite. It \ + can also occur if AOT processing failed for the test class, in which case you \ + can consult the logs generated during AOT processing.""".formatted(testClass.getName())); return new AotMergedContextConfiguration(testClass, contextInitializerClass, mergedConfig, this); } return mergedConfig;