From 317c6fbec254ae15931349416024947cfb815053 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 15 Jul 2023 10:18:51 +0200 Subject: [PATCH] Introduce failOnError flag in TestContextAotGenerator This commit introduces a `failOnError` flag in TestContextAotGenerator. When set to `true`, any error encountered during AOT processing will result in an exception that fails the overall process. When set to `false` (the default), the previous behavior remains unchanged: a DEBUG or WARN message will be logged, and processing will continue. This feature is currently only used for internal testing. See gh-30861 Closes gh-30898 --- .../context/aot/TestContextAotGenerator.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java index 20c686e562a..b01dfce9999 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java @@ -85,6 +85,8 @@ public class TestContextAotGenerator { private final RuntimeHints runtimeHints; + private final boolean failOnError; + /** * Create a new {@link TestContextAotGenerator} that uses the supplied @@ -102,9 +104,23 @@ public class TestContextAotGenerator { * @param runtimeHints the {@code RuntimeHints} to use */ public TestContextAotGenerator(GeneratedFiles generatedFiles, RuntimeHints runtimeHints) { + this(generatedFiles, runtimeHints, false); + } + + /** + * Create a new {@link TestContextAotGenerator} that uses the supplied + * {@link GeneratedFiles}, {@link RuntimeHints}, and {@code failOnError} flag. + * @param generatedFiles the {@code GeneratedFiles} to use + * @param runtimeHints the {@code RuntimeHints} to use + * @param failOnError {@code true} if errors encountered during AOT processing + * should result in an exception that fails the overall process + * @since 6.0.12 + */ + TestContextAotGenerator(GeneratedFiles generatedFiles, RuntimeHints runtimeHints, boolean failOnError) { this.testRuntimeHintsRegistrars = AotServices.factories().load(TestRuntimeHintsRegistrar.class); this.generatedFiles = generatedFiles; this.runtimeHints = runtimeHints; + this.failOnError = failOnError; } @@ -210,6 +226,10 @@ public class TestContextAotGenerator { generationContext.writeGeneratedContent(); } catch (Exception ex) { + if (this.failOnError) { + throw new IllegalStateException("Failed to generate AOT artifacts for test classes " + + testClasses.stream().map(Class::getName).toList(), ex); + } if (logger.isDebugEnabled()) { logger.debug("Failed to generate AOT artifacts for test classes " + testClasses.stream().map(Class::getName).toList(), ex);