Skip to content

Commit ea13238

Browse files
committed
Introduce AotTestExecutionListener API in the TestContext framework
This commit introduces an AotTestExecutionListener API that extends TestExecutionListener and allows a TestExecutionListener to opt in for AOT processing support, analogous to what the AotContextLoader API does for a SmartContextLoader. Closes gh-29070
1 parent c2dd666 commit ea13238

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.aot;
18+
19+
import org.springframework.aot.hint.RuntimeHints;
20+
import org.springframework.test.context.TestExecutionListener;
21+
22+
/**
23+
* {@code AotTestExecutionListener} is an extension of the {@link TestExecutionListener}
24+
* SPI that allows a listener to optionally provide ahead-of-time (AOT) support.
25+
*
26+
* @author Sam Brannen
27+
* @since 6.0
28+
*/
29+
public interface AotTestExecutionListener extends TestExecutionListener {
30+
31+
/**
32+
* Process the supplied test class ahead-of-time using the given
33+
* {@link RuntimeHints} instance.
34+
* <p>If possible, implementations should use the specified {@link ClassLoader}
35+
* to determine if hints have to be contributed.
36+
* @param testClass the test class to process
37+
* @param runtimeHints the {@code RuntimeHints} to use
38+
* @param classLoader the classloader to use
39+
*/
40+
void processAheadOfTime(Class<?> testClass, RuntimeHints runtimeHints, ClassLoader classLoader);
41+
42+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,12 @@ MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
210210
// @BootstrapWith
211211
registerDeclaredConstructors(testContextBootstrapper.getClass());
212212
// @TestExecutionListeners
213-
testContextBootstrapper.getTestExecutionListeners().stream()
214-
.map(Object::getClass)
215-
.forEach(this::registerDeclaredConstructors);
213+
testContextBootstrapper.getTestExecutionListeners().forEach(listener -> {
214+
registerDeclaredConstructors(listener.getClass());
215+
if (listener instanceof AotTestExecutionListener aotListener) {
216+
aotListener.processAheadOfTime(testClass, this.runtimeHints, getClass().getClassLoader());
217+
}
218+
});
216219
return testContextBootstrapper.buildMergedContextConfiguration();
217220
}
218221

0 commit comments

Comments
 (0)