mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Ensure context caching works properly during AOT runtime in the TCF
Prior to this commit, the AOT runtime support in the Spring TestContext Framework (TCF) relied on the MergedContextConfiguration for a given test class being the same as during the AOT processing phase. However, this is not always the case. For example, Spring Boot "disables" selected `ContextCustomizer` implementations during AOT runtime execution. See https://github.com/spring-projects/spring-boot/commit/0f325f98b5f0ad07d404a41cc172ad33879a8ecf To address that, this commit ensures that context caching works properly during AOT runtime execution even if the MergedContextConfiguration differs from what was produced during the AOT processing phase. Specifically, this commit introduces AotMergedContextConfiguration which is a MergedContextConfiguration implementation based on an AOT-generated ApplicationContextInitializer. AotMergedContextConfiguration wraps the MergedContextConfiguration built during AOT runtime execution. Interactions with the ContextCache are performed using the AotMergedContextConfiguration; whereas, the ApplicationContext is loaded using the original MergedContextConfiguration. This commit also introduces a ContextCustomizerFactory that emulates the ImportsContextCustomizerFactory in Spring Boot's testing support. BasicSpringJupiterImportedConfigTests uses @Import to verify that the context customizer works, and AotIntegrationTests has been updated to execute BasicSpringJupiterImportedConfigTests after test classes whose MergedContextConfiguration is identical during AOT runtime execution. Without the fix in this commit, BasicSpringJupiterImportedConfigTests would fail in AOT runtime mode since its ApplicationContext would be pulled from the cache using an inappropriate cache key. Closes gh-29289
This commit is contained in:
+21
-14
@@ -33,31 +33,38 @@ abstract class AbstractAotTests {
|
||||
// Global
|
||||
"org/springframework/test/context/aot/AotTestContextInitializers__Generated.java",
|
||||
"org/springframework/test/context/aot/AotTestAttributes__Generated.java",
|
||||
// BasicSpringJupiterSharedConfigTests
|
||||
// BasicSpringJupiterImportedConfigTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java",
|
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext001_BeanDefinitions.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext001_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext001_BeanDefinitions.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterImportedConfigTests__TestContext001_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext001_BeanDefinitions.java",
|
||||
// BasicSpringJupiterTests -- not generated b/c already generated for BasicSpringJupiterSharedConfigTests.
|
||||
// BasicSpringJupiterTests.NestedTests
|
||||
// BasicSpringJupiterSharedConfigTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext002_BeanDefinitions.java",
|
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext002_BeanDefinitions.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext002_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext002_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext002_BeanDefinitions.java",
|
||||
// BasicSpringTestNGTests
|
||||
// BasicSpringJupiterTests -- not generated b/c already generated for BasicSpringJupiterSharedConfigTests.
|
||||
// BasicSpringJupiterTests.NestedTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext003_BeanDefinitions.java",
|
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext003_BeanDefinitions.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext003_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext003_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext003_BeanDefinitions.java",
|
||||
// BasicSpringVintageTests
|
||||
// BasicSpringTestNGTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext004_BeanDefinitions.java",
|
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext004_BeanDefinitions.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java"
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext004_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext004_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java",
|
||||
// BasicSpringVintageTests
|
||||
"org/springframework/context/event/DefaultEventListenerFactory__TestContext005_BeanDefinitions.java",
|
||||
"org/springframework/context/event/EventListenerMethodProcessor__TestContext005_BeanDefinitions.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext005_ApplicationContextInitializer.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext005_BeanFactoryRegistrations.java",
|
||||
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext005_BeanDefinitions.java"
|
||||
};
|
||||
|
||||
Stream<Class<?>> scan() {
|
||||
|
||||
+6
-1
@@ -39,6 +39,7 @@ import org.springframework.aot.generate.InMemoryGeneratedFiles;
|
||||
import org.springframework.aot.test.generate.CompilerFiles;
|
||||
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
|
||||
import org.springframework.core.test.tools.TestCompiler;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
|
||||
@@ -96,9 +97,13 @@ class AotIntegrationTests extends AbstractAotTests {
|
||||
// .printFiles(System.out)
|
||||
.compile(compiled ->
|
||||
// AOT RUN-TIME: EXECUTION
|
||||
runTestsInAotMode(5, List.of(
|
||||
runTestsInAotMode(6, List.of(
|
||||
BasicSpringJupiterSharedConfigTests.class,
|
||||
BasicSpringJupiterTests.class, // NestedTests get executed automatically
|
||||
// Run @Import tests AFTER the tests with otherwise identical config
|
||||
// in order to ensure that the other test classes are not accidentally
|
||||
// using the config for the @Import tests.
|
||||
BasicSpringJupiterImportedConfigTests.class,
|
||||
BasicSpringTestNGTests.class,
|
||||
BasicSpringVintageTests.class)));
|
||||
}
|
||||
|
||||
+2
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.CleanupMode;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
|
||||
@@ -49,6 +50,7 @@ class TestAotProcessorTests extends AbstractAotTests {
|
||||
// Limit the scope of this test by creating a new classpath root on the fly.
|
||||
Path classpathRoot = Files.createDirectories(tempDir.resolve("build/classes"));
|
||||
Stream.of(
|
||||
BasicSpringJupiterImportedConfigTests.class,
|
||||
BasicSpringJupiterSharedConfigTests.class,
|
||||
BasicSpringJupiterTests.class,
|
||||
BasicSpringJupiterTests.NestedTests.class,
|
||||
|
||||
+7
-2
@@ -18,6 +18,7 @@ package org.springframework.test.context.aot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
|
||||
@@ -37,6 +38,7 @@ class TestClassScannerTests extends AbstractAotTests {
|
||||
void scanBasicTestClasses() {
|
||||
assertThat(scan("org.springframework.test.context.aot.samples.basic"))
|
||||
.containsExactlyInAnyOrder(
|
||||
BasicSpringJupiterImportedConfigTests.class,
|
||||
BasicSpringJupiterSharedConfigTests.class,
|
||||
BasicSpringJupiterTests.class,
|
||||
BasicSpringJupiterTests.NestedTests.class,
|
||||
@@ -48,8 +50,9 @@ class TestClassScannerTests extends AbstractAotTests {
|
||||
@Test
|
||||
void scanTestSuitesForJupiter() {
|
||||
assertThat(scan("org.springframework.test.context.aot.samples.suites.jupiter"))
|
||||
.containsExactlyInAnyOrder(BasicSpringJupiterSharedConfigTests.class,
|
||||
BasicSpringJupiterTests.class, BasicSpringJupiterTests.NestedTests.class);
|
||||
.containsExactlyInAnyOrder(BasicSpringJupiterImportedConfigTests.class,
|
||||
BasicSpringJupiterSharedConfigTests.class, BasicSpringJupiterTests.class,
|
||||
BasicSpringJupiterTests.NestedTests.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -68,6 +71,7 @@ class TestClassScannerTests extends AbstractAotTests {
|
||||
void scanTestSuitesForAllTestEngines() {
|
||||
assertThat(scan("org.springframework.test.context.aot.samples.suites.all"))
|
||||
.containsExactlyInAnyOrder(
|
||||
BasicSpringJupiterImportedConfigTests.class,
|
||||
BasicSpringJupiterSharedConfigTests.class,
|
||||
BasicSpringJupiterTests.class,
|
||||
BasicSpringJupiterTests.NestedTests.class,
|
||||
@@ -80,6 +84,7 @@ class TestClassScannerTests extends AbstractAotTests {
|
||||
void scanTestSuitesWithNestedSuites() {
|
||||
assertThat(scan("org.springframework.test.context.aot.samples.suites.nested"))
|
||||
.containsExactlyInAnyOrder(
|
||||
BasicSpringJupiterImportedConfigTests.class,
|
||||
BasicSpringJupiterSharedConfigTests.class,
|
||||
BasicSpringJupiterTests.class,
|
||||
BasicSpringJupiterTests.NestedTests.class,
|
||||
|
||||
+2
-1
@@ -193,7 +193,8 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
|
||||
// ContextCustomizerFactory
|
||||
Stream.of(
|
||||
"org.springframework.test.context.support.DynamicPropertiesContextCustomizerFactory",
|
||||
"org.springframework.test.context.web.socket.MockServerContainerContextCustomizerFactory"
|
||||
"org.springframework.test.context.web.socket.MockServerContainerContextCustomizerFactory",
|
||||
"org.springframework.test.context.aot.samples.basic.ImportsContextCustomizerFactory"
|
||||
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
|
||||
|
||||
Stream.of(
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.aot.samples.basic;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterImportedConfigTests.ImportedConfig;
|
||||
import org.springframework.test.context.aot.samples.common.MessageService;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Uses configuration identical to {@link BasicSpringJupiterTests} and
|
||||
* {@link BasicSpringJupiterImportedConfigTests} EXCEPT that this class is
|
||||
* annotated with {@link Import @Import} to register an additional bean.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.0
|
||||
*/
|
||||
@SpringJUnitConfig(BasicTestConfiguration.class)
|
||||
@Import(ImportedConfig.class)
|
||||
@TestPropertySource(properties = "test.engine = jupiter")
|
||||
public class BasicSpringJupiterImportedConfigTests {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
MessageService messageService;
|
||||
|
||||
@Autowired
|
||||
String enigma;
|
||||
|
||||
@Value("${test.engine}")
|
||||
String testEngine;
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
void test() {
|
||||
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
|
||||
assertThat(enigma).isEqualTo("imported!");
|
||||
assertThat(testEngine).isEqualTo("jupiter");
|
||||
assertThat(context.getEnvironment().getProperty("test.engine"))
|
||||
.as("@TestPropertySource").isEqualTo("jupiter");
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class ImportedConfig {
|
||||
|
||||
@Bean
|
||||
String enigma() {
|
||||
return "imported!";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.aot.samples.basic;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.aot.AotDetector;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
import org.springframework.test.context.ContextCustomizerFactory;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
/**
|
||||
* Emulates {@code ImportsContextCustomizerFactory} from Spring Boot's testing support.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.0
|
||||
*/
|
||||
class ImportsContextCustomizerFactory implements ContextCustomizerFactory {
|
||||
|
||||
@Override
|
||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configAttributes) {
|
||||
|
||||
if (AotDetector.useGeneratedArtifacts()) {
|
||||
return null;
|
||||
}
|
||||
if (testClass.getName().startsWith("org.springframework.test.context.aot.samples") &&
|
||||
testClass.isAnnotationPresent(Import.class)) {
|
||||
return new ImportsContextCustomizer(testClass);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emulates {@code ImportsContextCustomizer} from Spring Boot's testing support.
|
||||
*/
|
||||
private static class ImportsContextCustomizer implements ContextCustomizer {
|
||||
|
||||
private final Class<?> testClass;
|
||||
|
||||
ImportsContextCustomizer(Class<?> testClass) {
|
||||
this.testClass = testClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
AnnotatedBeanDefinitionReader annotatedBeanDefinitionReader =
|
||||
new AnnotatedBeanDefinitionReader((GenericApplicationContext) context);
|
||||
Arrays.stream(this.testClass.getAnnotation(Import.class).value())
|
||||
.forEach(annotatedBeanDefinitionReader::register);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.cache;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.support.DelegatingSmartContextLoader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link AotMergedContextConfiguration}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 6.0
|
||||
*/
|
||||
class AotMergedContextConfigurationTests {
|
||||
|
||||
private final CacheAwareContextLoaderDelegate delegate =
|
||||
new DefaultCacheAwareContextLoaderDelegate(mock(ContextCache.class));
|
||||
|
||||
private final ContextLoader contextLoader = new DelegatingSmartContextLoader();
|
||||
|
||||
private final MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), null, null,
|
||||
Set.of(DemoApplicationContextInitializer.class), null, contextLoader);
|
||||
|
||||
private final AotMergedContextConfiguration aotMergedConfig1 = new AotMergedContextConfiguration(getClass(),
|
||||
DemoApplicationContextInitializer.class, mergedConfig, delegate);
|
||||
|
||||
private final AotMergedContextConfiguration aotMergedConfig2 = new AotMergedContextConfiguration(getClass(),
|
||||
DemoApplicationContextInitializer.class, mergedConfig, delegate);
|
||||
|
||||
|
||||
@Test
|
||||
void testEquals() {
|
||||
assertThat(aotMergedConfig1).isEqualTo(aotMergedConfig1);
|
||||
assertThat(aotMergedConfig1).isEqualTo(aotMergedConfig2);
|
||||
|
||||
assertThat(mergedConfig).isNotEqualTo(aotMergedConfig1);
|
||||
assertThat(aotMergedConfig1).isNotEqualTo(mergedConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHashCode() {
|
||||
assertThat(aotMergedConfig1).hasSameHashCodeAs(aotMergedConfig2);
|
||||
|
||||
assertThat(aotMergedConfig1).doesNotHaveSameHashCodeAs(mergedConfig);
|
||||
}
|
||||
|
||||
|
||||
static class DemoApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
|
||||
@Override
|
||||
public void initialize(GenericApplicationContext applicationContext) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user