Skip to content

Commit ea7a1d7

Browse files
committed
Resolve ContextLoader only once in AbstractTestContextBootstrapper
Closes gh-35994
1 parent 4ae471d commit ea7a1d7

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public final MergedContextConfiguration buildMergedContextConfiguration() {
231231
Class<?> declaringClass = reversedList.get(0).getDeclaringClass();
232232

233233
mergedConfig = buildMergedContextConfiguration(
234-
declaringClass, reversedList, parentConfig, cacheAwareContextLoaderDelegate, true);
234+
declaringClass, reversedList, null, parentConfig, cacheAwareContextLoaderDelegate, true);
235235
parentConfig = mergedConfig;
236236
}
237237

@@ -242,7 +242,7 @@ public final MergedContextConfiguration buildMergedContextConfiguration() {
242242
else {
243243
return buildMergedContextConfiguration(testClass,
244244
ContextLoaderUtils.resolveContextConfigurationAttributes(testClass),
245-
null, cacheAwareContextLoaderDelegate, true);
245+
null, null, cacheAwareContextLoaderDelegate, true);
246246
}
247247
}
248248

@@ -263,7 +263,7 @@ else if (logger.isDebugEnabled()) {
263263
"Neither @ContextConfiguration nor @ContextHierarchy found for test class [%s]: using %s",
264264
testClass.getSimpleName(), contextLoader.getClass().getSimpleName()));
265265
}
266-
return buildMergedContextConfiguration(testClass, defaultConfigAttributesList, null,
266+
return buildMergedContextConfiguration(testClass, defaultConfigAttributesList, contextLoader, null,
267267
cacheAwareContextLoaderDelegate, false);
268268
}
269269

@@ -277,6 +277,7 @@ else if (logger.isDebugEnabled()) {
277277
* specified test class, ordered <em>bottom-up</em> (i.e., as if we were
278278
* traversing up the class hierarchy and enclosing class hierarchy); never
279279
* {@code null} or empty
280+
* @param contextLoader a pre-resolved {@link ContextLoader} to use; may be {@code null}
280281
* @param parentConfig the merged context configuration for the parent application
281282
* context in a context hierarchy, or {@code null} if there is no parent
282283
* @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
@@ -294,13 +295,16 @@ else if (logger.isDebugEnabled()) {
294295
* @see MergedContextConfiguration
295296
*/
296297
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass,
297-
List<ContextConfigurationAttributes> configAttributesList, @Nullable MergedContextConfiguration parentConfig,
298+
List<ContextConfigurationAttributes> configAttributesList, @Nullable ContextLoader contextLoader,
299+
@Nullable MergedContextConfiguration parentConfig,
298300
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate,
299301
boolean requireLocationsClassesOrInitializers) {
300302

301303
Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty");
302304

303-
ContextLoader contextLoader = resolveContextLoader(testClass, configAttributesList);
305+
if (contextLoader == null) {
306+
contextLoader = resolveContextLoader(testClass, configAttributesList);
307+
}
304308
List<String> locations = new ArrayList<>();
305309
List<Class<?>> classes = new ArrayList<>();
306310
List<Class<?>> initializers = new ArrayList<>();
@@ -331,11 +335,12 @@ private MergedContextConfiguration buildMergedContextConfiguration(Class<?> test
331335
Set<ContextCustomizer> contextCustomizers = getContextCustomizers(testClass,
332336
Collections.unmodifiableList(configAttributesList));
333337

338+
ContextLoader effectivelyFinalContextLoader = contextLoader;
334339
Assert.state(!(requireLocationsClassesOrInitializers &&
335-
areAllEmpty(locations, classes, initializers, contextCustomizers)), () -> String.format(
336-
"%s was unable to detect defaults, and no ApplicationContextInitializers " +
337-
"or ContextCustomizers were declared for context configuration attributes %s",
338-
contextLoader.getClass().getSimpleName(), configAttributesList));
340+
areAllEmpty(locations, classes, initializers, contextCustomizers)), () -> """
341+
%s was unable to detect defaults, and no ApplicationContextInitializers \
342+
or ContextCustomizers were declared for context configuration attributes %s\
343+
""".formatted(effectivelyFinalContextLoader.getClass().getSimpleName(), configAttributesList));
339344

340345
MergedTestPropertySources mergedTestPropertySources =
341346
TestPropertySourceUtils.buildMergedTestPropertySources(testClass);

0 commit comments

Comments
 (0)