diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/MergedContextConfigurationRuntimeHints.java b/spring-test/src/main/java/org/springframework/test/context/aot/MergedContextConfigurationRuntimeHints.java index 173b914898d..22fa9eea663 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/MergedContextConfigurationRuntimeHints.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/MergedContextConfigurationRuntimeHints.java @@ -76,6 +76,12 @@ class MergedContextConfigurationRuntimeHints { for (PropertySourceDescriptor descriptor : mergedConfig.getPropertySourceDescriptors()) { // @TestPropertySource(locations = ...) registerClasspathResources(descriptor.locations().stream(), runtimeHints, classLoader); + + // @TestPropertySource(factory = ...) + Class factoryClass = descriptor.propertySourceFactory(); + if (factoryClass != null) { + registerDeclaredConstructors(factoryClass, runtimeHints); + } } // @WebAppConfiguration(value = ...) diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java index 0285c9c5121..ecdc96d7297 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java @@ -55,6 +55,7 @@ import org.springframework.test.context.aot.samples.web.WebSpringVintageTests; import org.springframework.test.context.aot.samples.xml.XmlSpringJupiterTests; import org.springframework.test.context.aot.samples.xml.XmlSpringTestNGTests; import org.springframework.test.context.aot.samples.xml.XmlSpringVintageTests; +import org.springframework.test.context.env.YamlPropertySourceFactory; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.function.ThrowingConsumer; import org.springframework.web.context.WebApplicationContext; @@ -213,8 +214,20 @@ class TestContextAotGeneratorTests extends AbstractAotTests { // @TestPropertySource(locations = ...) assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties")) + .as("@TestPropertySource(locations)") .accepts(runtimeHints); + // @YamlTestProperties(...) + assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test1.yaml")) + .as("@YamlTestProperties: test1.yaml") + .accepts(runtimeHints); + assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test2.yaml")) + .as("@YamlTestProperties: test2.yaml") + .accepts(runtimeHints); + + // @TestPropertySource(factory = ...) + assertReflectionRegistered(runtimeHints, YamlPropertySourceFactory.class.getName(), INVOKE_DECLARED_CONSTRUCTORS); + // @WebAppConfiguration(value = ...) assertThat(resource().forResource("META-INF/web-resources/resources/Spring.js")).accepts(runtimeHints); assertThat(resource().forResource("META-INF/web-resources/WEB-INF/views/home.jsp")).accepts(runtimeHints); diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests.java index 9a2b2619802..0d718798ee8 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests.java @@ -22,6 +22,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.aot.samples.common.MessageService; import org.springframework.test.context.aot.samples.management.ManagementConfiguration; +import org.springframework.test.context.env.YamlTestProperties; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import static org.assertj.core.api.Assertions.assertThat; @@ -34,9 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Sam Brannen * @since 6.0 + * @see BasicSpringJupiterTests */ @SpringJUnitConfig({BasicTestConfiguration.class, ManagementConfiguration.class}) @TestPropertySource(properties = "test.engine = jupiter") +// We cannot use `classpath*:` in AOT tests until gh-31088 is resolved. +// @YamlTestProperties("classpath*:org/springframework/test/context/aot/samples/basic/**/test?.yaml") +@YamlTestProperties({ + "classpath:org/springframework/test/context/aot/samples/basic/test1.yaml", + "classpath:org/springframework/test/context/aot/samples/basic/test2.yaml" +}) public class BasicSpringJupiterSharedConfigTests { @Autowired @@ -52,8 +60,7 @@ public class BasicSpringJupiterSharedConfigTests { void test() { assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); assertThat(testEngine).isEqualTo("jupiter"); - assertThat(context.getEnvironment().getProperty("test.engine")) - .as("@TestPropertySource").isEqualTo("jupiter"); + BasicSpringJupiterTests.assertEnvProperties(context); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests.java index a8cb4a83fd1..5230c24fcf6 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests.java @@ -21,12 +21,14 @@ import org.junit.jupiter.api.Nested; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests.DummyTestExecutionListener; import org.springframework.test.context.aot.samples.common.MessageService; import org.springframework.test.context.aot.samples.management.ManagementConfiguration; +import org.springframework.test.context.env.YamlTestProperties; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.support.AbstractTestExecutionListener; @@ -42,10 +44,17 @@ import static org.springframework.test.context.TestExecutionListeners.MergeMode. * * @author Sam Brannen * @since 6.0 + * @see BasicSpringJupiterSharedConfigTests */ @SpringJUnitConfig({BasicTestConfiguration.class, ManagementConfiguration.class}) @TestExecutionListeners(listeners = DummyTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS) @TestPropertySource(properties = "test.engine = jupiter") +// We cannot use `classpath*:` in AOT tests until gh-31088 is resolved. +// @YamlTestProperties("classpath*:org/springframework/test/context/aot/samples/basic/**/test?.yaml") +@YamlTestProperties({ + "classpath:org/springframework/test/context/aot/samples/basic/test1.yaml", + "classpath:org/springframework/test/context/aot/samples/basic/test2.yaml" +}) public class BasicSpringJupiterTests { @org.junit.jupiter.api.Test @@ -53,8 +62,7 @@ public class BasicSpringJupiterTests { @Value("${test.engine}") String testEngine) { assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!"); assertThat(testEngine).isEqualTo("jupiter"); - assertThat(context.getEnvironment().getProperty("test.engine")) - .as("@TestPropertySource").isEqualTo("jupiter"); + assertEnvProperties(context); } @Nested @@ -68,12 +76,19 @@ public class BasicSpringJupiterTests { assertThat(messageService.generateMessage()).isEqualTo("¡Hola, AOT!"); assertThat(foo).isEqualTo("bar"); assertThat(testEngine).isEqualTo("jupiter"); - assertThat(context.getEnvironment().getProperty("test.engine")) - .as("@TestPropertySource").isEqualTo("jupiter"); + assertEnvProperties(context); } } + + static void assertEnvProperties(ApplicationContext context) { + Environment env = context.getEnvironment(); + assertThat(env.getProperty("test.engine")).as("@TestPropertySource").isEqualTo("jupiter"); + assertThat(env.getProperty("test1.prop")).as("@TestPropertySource").isEqualTo("yaml"); + assertThat(env.getProperty("test2.prop")).as("@TestPropertySource").isEqualTo("yaml"); + } + public static class DummyTestExecutionListener extends AbstractTestExecutionListener { } diff --git a/spring-test/src/test/java/org/springframework/test/context/env/YamlPropertySourceFactory.java b/spring-test/src/test/java/org/springframework/test/context/env/YamlPropertySourceFactory.java index d499ac78b65..8cee40a3453 100644 --- a/spring-test/src/test/java/org/springframework/test/context/env/YamlPropertySourceFactory.java +++ b/spring-test/src/test/java/org/springframework/test/context/env/YamlPropertySourceFactory.java @@ -32,7 +32,7 @@ import org.springframework.util.StringUtils; * @author Sam Brannen * @since 6.1 */ -class YamlPropertySourceFactory implements PropertySourceFactory { +public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource createPropertySource(String name, EncodedResource encodedResource) { diff --git a/spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test1.yaml b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test1.yaml new file mode 100644 index 00000000000..c09cd34339e --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test1.yaml @@ -0,0 +1,2 @@ +test1: + prop: yaml diff --git a/spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test2.yaml b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test2.yaml new file mode 100644 index 00000000000..95211508690 --- /dev/null +++ b/spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/test2.yaml @@ -0,0 +1,2 @@ +test2: + prop: yaml