Skip to content

Commit 5cc4d0a

Browse files
committed
Do not invoke AotTestExecutionListener for @⁠DisabledInAotMode tests
Closes gh-33589
1 parent 602ac90 commit 5cc4d0a

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -370,7 +370,7 @@ private MergedContextConfiguration buildMergedContextConfiguration(Class<?> test
370370
registerDeclaredConstructors(testContextBootstrapper.getClass()); // @BootstrapWith
371371
testContextBootstrapper.getTestExecutionListeners().forEach(listener -> {
372372
registerDeclaredConstructors(listener.getClass()); // @TestExecutionListeners
373-
if (listener instanceof AotTestExecutionListener aotListener) {
373+
if (!isDisabledInAotMode.test(testClass) && listener instanceof AotTestExecutionListener aotListener) {
374374
aotListener.processAheadOfTime(this.runtimeHints, testClass, getClass().getClassLoader());
375375
}
376376
});

spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/DisabledInAotProcessingTests.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,26 +19,33 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.aot.AotDetector;
22+
import org.springframework.aot.hint.RuntimeHints;
2223
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
2425
import org.springframework.context.annotation.Bean;
2526
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.test.context.TestExecutionListeners;
28+
import org.springframework.test.context.aot.AotTestExecutionListener;
2629
import org.springframework.test.context.aot.DisabledInAotMode;
2730
import org.springframework.test.context.aot.TestContextAotGenerator;
31+
import org.springframework.test.context.aot.samples.basic.DisabledInAotProcessingTests.BrokenAotTestExecutionListener;
2832
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2933
import org.springframework.util.Assert;
3034

3135
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS;
3237

3338
/**
3439
* {@code @DisabledInAotMode} test class which verifies that the application context
35-
* for the test class is skipped during AOT processing.
40+
* for the test class is skipped during AOT processing and that an
41+
* {@link AotTestExecutionListener} is also not invoked.
3642
*
3743
* @author Sam Brannen
3844
* @since 6.1
3945
*/
4046
@SpringJUnitConfig
4147
@DisabledInAotMode
48+
@TestExecutionListeners(listeners = BrokenAotTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS)
4249
public class DisabledInAotProcessingTests {
4350

4451
@Test
@@ -64,4 +71,12 @@ static BeanFactoryPostProcessor bfppBrokenDuringAotProcessing() {
6471
}
6572
}
6673

74+
static class BrokenAotTestExecutionListener implements AotTestExecutionListener {
75+
76+
@Override
77+
public void processAheadOfTime(RuntimeHints runtimeHints, Class<?> testClass, ClassLoader classLoader) {
78+
throw new UnsupportedOperationException("Broken AotTestExecutionListener");
79+
}
80+
}
81+
6782
}

0 commit comments

Comments
 (0)