Skip to content

Commit

Permalink
Forbid loading of test ApplicationContext in AOT mode if AOT processi…
Browse files Browse the repository at this point in the history
…ng 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
  • Loading branch information
sbrannen committed Nov 26, 2022
1 parent 33d3380 commit 352087a
Showing 1 changed file with 12 additions and 2 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -239,14 +240,23 @@ private ContextLoader getContextLoader(MergedContextConfiguration mergedConfig)
* unmodified.
* <p>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<? extends ApplicationContextInitializer<?>> 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;
Expand Down

0 comments on commit 352087a

Please sign in to comment.