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 dd52fe4025..51c4ded7cc 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -370,7 +370,7 @@ public class TestContextAotGenerator { registerDeclaredConstructors(testContextBootstrapper.getClass()); // @BootstrapWith testContextBootstrapper.getTestExecutionListeners().forEach(listener -> { registerDeclaredConstructors(listener.getClass()); // @TestExecutionListeners - if (listener instanceof AotTestExecutionListener aotListener) { + if (!isDisabledInAotMode.test(testClass) && listener instanceof AotTestExecutionListener aotListener) { aotListener.processAheadOfTime(this.runtimeHints, testClass, getClass().getClassLoader()); } }); diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java index 80969737f2..6177b8adfa 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,26 +19,33 @@ package org.springframework.test.context.aot.samples.basic; import org.junit.jupiter.api.Test; import org.springframework.aot.AotDetector; +import org.springframework.aot.hint.RuntimeHints; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.aot.AotTestExecutionListener; import org.springframework.test.context.aot.DisabledInAotMode; import org.springframework.test.context.aot.TestContextAotGenerator; +import org.springframework.test.context.aot.samples.basic.DisabledInAotProcessingTests.BrokenAotTestExecutionListener; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.util.Assert; import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS; /** * {@code @DisabledInAotMode} test class which verifies that the application context - * for the test class is skipped during AOT processing. + * for the test class is skipped during AOT processing and that an + * {@link AotTestExecutionListener} is also not invoked. * * @author Sam Brannen * @since 6.1 */ @SpringJUnitConfig @DisabledInAotMode +@TestExecutionListeners(listeners = BrokenAotTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS) public class DisabledInAotProcessingTests { @Test @@ -64,4 +71,12 @@ public class DisabledInAotProcessingTests { } } + static class BrokenAotTestExecutionListener implements AotTestExecutionListener { + + @Override + public void processAheadOfTime(RuntimeHints runtimeHints, Class testClass, ClassLoader classLoader) { + throw new UnsupportedOperationException("Broken AotTestExecutionListener"); + } + } + }