@@ -231,7 +231,7 @@ public final MergedContextConfiguration buildMergedContextConfiguration() {
231231Class <?> declaringClass = reversedList .get (0 ).getDeclaringClass ();
232232
233233mergedConfig = buildMergedContextConfiguration (
234- declaringClass , reversedList , parentConfig , cacheAwareContextLoaderDelegate , true );
234+ declaringClass , reversedList , null , parentConfig , cacheAwareContextLoaderDelegate , true );
235235parentConfig = mergedConfig ;
236236}
237237
@@ -242,7 +242,7 @@ public final MergedContextConfiguration buildMergedContextConfiguration() {
242242else {
243243return buildMergedContextConfiguration (testClass ,
244244ContextLoaderUtils .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" ,
264264testClass .getSimpleName (), contextLoader .getClass ().getSimpleName ()));
265265}
266- return buildMergedContextConfiguration (testClass , defaultConfigAttributesList , null ,
266+ return buildMergedContextConfiguration (testClass , defaultConfigAttributesList , contextLoader , null ,
267267cacheAwareContextLoaderDelegate , 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 */
296297private MergedContextConfiguration buildMergedContextConfiguration (Class <?> testClass ,
297- List <ContextConfigurationAttributes > configAttributesList , @ Nullable MergedContextConfiguration parentConfig ,
298+ List <ContextConfigurationAttributes > configAttributesList , @ Nullable ContextLoader contextLoader ,
299+ @ Nullable MergedContextConfiguration parentConfig ,
298300CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate ,
299301boolean requireLocationsClassesOrInitializers ) {
300302
301303Assert .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+ }
304308List <String > locations = new ArrayList <>();
305309List <Class <?>> classes = new ArrayList <>();
306310List <Class <?>> initializers = new ArrayList <>();
@@ -331,11 +335,12 @@ private MergedContextConfiguration buildMergedContextConfiguration(Class<?> test
331335Set <ContextCustomizer > contextCustomizers = getContextCustomizers (testClass ,
332336Collections .unmodifiableList (configAttributesList ));
333337
338+ ContextLoader effectivelyFinalContextLoader = contextLoader ;
334339Assert .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
340345MergedTestPropertySources mergedTestPropertySources =
341346TestPropertySourceUtils .buildMergedTestPropertySources (testClass );
0 commit comments