Register hints for @TestPropertySource factory

This commit updates MergedContextConfigurationRuntimeHints so that it
registers hints for a custom PropertySourceFactory.

Closes gh-31160
This commit is contained in:
Sam Brannen
2023-09-03 16:53:53 +02:00
parent 7882d265c6
commit e8fd29091c
7 changed files with 52 additions and 7 deletions

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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 {
}

View File

@@ -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) {