Browse Source

Register runtime hints for @TestPropertySource#locations

This commit introduces automatic registration of runtime hints for
classpath resources configured via the `locations` attribute in
@TestPropertySource as well as for detected default properties files.

Closes gh-29025
pull/29072/head
Sam Brannen 2 years ago
parent
commit
ef7784f85e
  1. 11
      spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java
  2. 4
      spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java
  3. 2
      spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.java
  4. 1
      spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties

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

@ -248,12 +248,21 @@ public class TestContextAotGenerator { @@ -248,12 +248,21 @@ public class TestContextAotGenerator {
// @ContextConfiguration(locations = ...)
registerHintsForClasspathResources(mergedConfig.getLocations());
// @TestPropertySource(locations = ... )
registerHintsForClasspathResources(mergedConfig.getPropertySourceLocations());
}
private void registerHintsForClasspathResources(String... locations) {
Arrays.stream(locations)
.filter(location -> location.startsWith(CLASSPATH_URL_PREFIX))
.map(location -> location.substring(CLASSPATH_URL_PREFIX.length()))
.map(location -> {
location = location.substring(CLASSPATH_URL_PREFIX.length());
if (!location.startsWith("/")) {
location = "/" + location;
}
return location;
})
.forEach(this.runtimeHints.resources()::registerPattern);
}

4
spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java

@ -171,6 +171,10 @@ class TestContextAotGeneratorTests extends AbstractAotTests { @@ -171,6 +171,10 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
// @ContextConfiguration(locations=...)
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/xml/test-config.xml"))
.accepts(runtimeHints);
// @TestPropertySource(locations = ... )
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
.accepts(runtimeHints);
}
private static void assertReflectionRegistered(RuntimeHints runtimeHints, String type, MemberCategory memberCategory) {

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

@ -42,7 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -42,7 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
// Override the default loader configured by the CustomXmlBootstrapper
@ContextConfiguration(classes = BasicTestConfiguration.class, loader = AnnotationConfigContextLoader.class)
@TestPropertySource(properties = "test.engine = vintage")
@TestPropertySource
public class BasicSpringVintageTests {
@Autowired

1
spring-test/src/test/resources/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties

@ -0,0 +1 @@ @@ -0,0 +1 @@
test.engine = vintage
Loading…
Cancel
Save