Do not invoke AotTestExecutionListener for @⁠DisabledInAotMode tests

Closes gh-33589
This commit is contained in:
Sam Brannen 2024-09-24 16:47:44 +02:00
parent 602ac903ff
commit 5cc4d0a2ca
2 changed files with 19 additions and 4 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 registerDeclaredConstructors(testContextBootstrapper.getClass()); // @BootstrapWith
testContextBootstrapper.getTestExecutionListeners().forEach(listener -> { testContextBootstrapper.getTestExecutionListeners().forEach(listener -> {
registerDeclaredConstructors(listener.getClass()); // @TestExecutionListeners registerDeclaredConstructors(listener.getClass()); // @TestExecutionListeners
if (listener instanceof AotTestExecutionListener aotListener) { if (!isDisabledInAotMode.test(testClass) && listener instanceof AotTestExecutionListener aotListener) {
aotListener.processAheadOfTime(this.runtimeHints, testClass, getClass().getClassLoader()); aotListener.processAheadOfTime(this.runtimeHints, testClass, getClass().getClassLoader());
} }
}); });

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.junit.jupiter.api.Test;
import org.springframework.aot.AotDetector; import org.springframework.aot.AotDetector;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 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.DisabledInAotMode;
import org.springframework.test.context.aot.TestContextAotGenerator; 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.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import static org.assertj.core.api.Assertions.assertThat; 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 * {@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 * @author Sam Brannen
* @since 6.1 * @since 6.1
*/ */
@SpringJUnitConfig @SpringJUnitConfig
@DisabledInAotMode @DisabledInAotMode
@TestExecutionListeners(listeners = BrokenAotTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS)
public class DisabledInAotProcessingTests { public class DisabledInAotProcessingTests {
@Test @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");
}
}
} }