Skip to content

Commit ef7784f

Browse files
committed
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
1 parent dcf160e commit ef7784f

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,21 @@ private void registerHintsForMergedConfig(MergedContextConfiguration mergedConfi
248248

249249
// @ContextConfiguration(locations = ...)
250250
registerHintsForClasspathResources(mergedConfig.getLocations());
251+
252+
// @TestPropertySource(locations = ... )
253+
registerHintsForClasspathResources(mergedConfig.getPropertySourceLocations());
251254
}
252255

253256
private void registerHintsForClasspathResources(String... locations) {
254257
Arrays.stream(locations)
255258
.filter(location -> location.startsWith(CLASSPATH_URL_PREFIX))
256-
.map(location -> location.substring(CLASSPATH_URL_PREFIX.length()))
259+
.map(location -> {
260+
location = location.substring(CLASSPATH_URL_PREFIX.length());
261+
if (!location.startsWith("/")) {
262+
location = "/" + location;
263+
}
264+
return location;
265+
})
257266
.forEach(this.runtimeHints.resources()::registerPattern);
258267
}
259268

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ private static void assertRuntimeHints(RuntimeHints runtimeHints) {
171171
// @ContextConfiguration(locations=...)
172172
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/xml/test-config.xml"))
173173
.accepts(runtimeHints);
174+
175+
// @TestPropertySource(locations = ... )
176+
assertThat(resource().forResource("/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
177+
.accepts(runtimeHints);
174178
}
175179

176180
private static void assertReflectionRegistered(RuntimeHints runtimeHints, String type, MemberCategory memberCategory) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
@RunWith(SpringRunner.class)
4343
// Override the default loader configured by the CustomXmlBootstrapper
4444
@ContextConfiguration(classes = BasicTestConfiguration.class, loader = AnnotationConfigContextLoader.class)
45-
@TestPropertySource(properties = "test.engine = vintage")
45+
@TestPropertySource
4646
public class BasicSpringVintageTests {
4747

4848
@Autowired
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.engine = vintage

0 commit comments

Comments
 (0)