diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java index 295ce15d9f..37046eb931 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java @@ -56,6 +56,110 @@ abstract class ContextLoaderUtils { /* no-op */ } + /** + * Resolve the {@link ContextLoader} {@link Class class} to use for the + * supplied {@link Class testClass} and then instantiate and return that + * {@code ContextLoader}. + *
If the supplied defaultContextLoaderClassName
is
+ * null
or empty, the standard
+ * default context loader class name {@value #DEFAULT_CONTEXT_LOADER_CLASS_NAME}
+ * will be used. For details on the class resolution process, see
+ * {@link #resolveContextLoaderClass()}.
+ * @param testClass the test class for which the {@code ContextLoader}
+ * should be resolved (must not be null
)
+ * @param defaultContextLoaderClassName the name of the default
+ * {@code ContextLoader} class to use (may be null
)
+ * @return the resolved {@code ContextLoader} for the supplied
+ * testClass
(never null
)
+ * @see #resolveContextLoaderClass()
+ */
+ static ContextLoader resolveContextLoader(Class> testClass, String defaultContextLoaderClassName) {
+ Assert.notNull(testClass, "Test class must not be null");
+
+ if (!StringUtils.hasText(defaultContextLoaderClassName)) {
+ defaultContextLoaderClassName = DEFAULT_CONTEXT_LOADER_CLASS_NAME;
+ }
+
+ Class extends ContextLoader> contextLoaderClass = resolveContextLoaderClass(testClass,
+ defaultContextLoaderClassName);
+
+ return (ContextLoader) BeanUtils.instantiateClass(contextLoaderClass);
+ }
+
+ /**
+ * Resolve the {@link ContextLoader} {@link Class} to use for the supplied
+ * {@link Class testClass}.
+ *
loader
class is not specified, the class hierarchy
+ * will be traversed to find a parent class annotated with
+ * {@code @ContextConfiguration}; go to step #1.loader
class is found after traversing
+ * the class hierarchy, an attempt will be made to load and return the class
+ * with the supplied defaultContextLoaderClassName
.null
+ * @param defaultContextLoaderClassName the name of the default
+ * {@code ContextLoader} class to use; must not be null
or empty
+ * @return the {@code ContextLoader} class to use for the supplied test class
+ * @throws IllegalArgumentException if {@code @ContextConfiguration} is not
+ * present on the supplied test class
+ * @throws IllegalStateException if the default {@code ContextLoader} class
+ * could not be loaded
+ */
+ @SuppressWarnings("unchecked")
+ static Class extends ContextLoader> resolveContextLoaderClass(Class> testClass,
+ String defaultContextLoaderClassName) {
+ Assert.notNull(testClass, "Class must not be null");
+ Assert.hasText(defaultContextLoaderClassName, "Default ContextLoader class name must not be null or empty");
+
+ Classnull
)
- * @throws IllegalArgumentException if the supplied class is null
- * or if {@link ContextConfiguration @ContextConfiguration} is not
- * present on the supplied class
+ * @throws IllegalArgumentException if the supplied class is null
or
+ * if {@code @ContextConfiguration} is not present on the supplied class
*/
static ListIf the supplied This method will iterate over the supplied configuration attributes
- * and execute the following algorithm:
- * Note that the {@link ActiveProfiles#inheritProfiles inheritProfiles}
@@ -286,8 +285,8 @@ abstract class ContextLoaderUtils {
* @param defaultContextLoaderClassName the name of the default
* {@code ContextLoader} class to use (may be defaultContextLoaderClassName
is
- * null
or empty, the standard
- * default context loader class name {@value #DEFAULT_CONTEXT_LOADER_CLASS_NAME}
- * will be used. For details on the class resolution process, see
- * {@link #resolveContextLoaderClass()}.
- * @param testClass the test class for which the {@code ContextLoader}
- * should be resolved (must not be null
)
- * @param configAttributesList the resolved configuration attributes for the
- * test class hierarchy
- * @param defaultContextLoaderClassName the name of the default
- * {@code ContextLoader} class to use (may be null
)
- * @return the resolved {@code ContextLoader} for the supplied
- * testClass
(never null
)
- * @see #resolveContextLoaderClass()
- * @see #resolveContextConfigurationAttributes()
- */
- static ContextLoader resolveContextLoader(Class> testClass,
- List
- *
- * @param testClass the class for which to resolve the {@code ContextLoader}
- * class; used solely for logging purposes; must not be loader
class is found after processing
- * all {@link ContextConfigurationAttributes} instances, an attempt will be
- * made to load and return the class with the supplied
- * defaultContextLoaderClassName
.null
- * @param configAttributesList the resolved configuration attributes for the
- * test class hierarchy; must not be null
or empty
- * @param defaultContextLoaderClassName the name of the default
- * {@code ContextLoader} class to use; must not be null
or empty
- * @return the {@code ContextLoader} class to use for the specified class
- * (never null
)
- * @throws IllegalArgumentException if {@code @ContextConfiguration} is not
- * present on the supplied test class
- * @see #resolveContextLoader()
- * @see #resolveContextConfigurationAttributes()
- */
- @SuppressWarnings("unchecked")
- static Class extends ContextLoader> resolveContextLoaderClass(Class> testClass,
- Listnull
)
* @return the merged context configuration
- * @see #resolveContextConfigurationAttributes()
* @see #resolveContextLoader()
+ * @see #resolveContextConfigurationAttributes()
* @see SmartContextLoader#processContextConfiguration()
* @see ContextLoader#processLocations()
* @see #resolveActiveProfiles()
@@ -296,16 +295,8 @@ abstract class ContextLoaderUtils {
static MergedContextConfiguration buildMergedContextConfiguration(Class> testClass,
String defaultContextLoaderClassName) {
- List