Browse Source

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
pull/30915/head
Sam Brannen 2 years ago
parent
commit
317c6fbec2
  1. 20
      spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java

20
spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java

@ -85,6 +85,8 @@ public class TestContextAotGenerator { @@ -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 { @@ -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 { @@ -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);

Loading…
Cancel
Save