From 5cc4d0a2ca2a30ce11f98495e0eaca4247aeaf0f Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Tue, 24 Sep 2024 16:47:44 +0200 Subject: [PATCH] =?UTF-8?q?Do=20not=20invoke=20AotTestExecutionListener=20?= =?UTF-8?q?for=20@=E2=81=A0DisabledInAotMode=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes gh-33589 --- .../context/aot/TestContextAotGenerator.java | 4 ++-- .../basic/DisabledInAotProcessingTests.java | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) 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"); + } + } + }