Support classes AND locations in @ContextConfiguration
Prior to this commit, the Spring TestContext Framework did not support the declaration of both 'locations' and 'classes' within @ContextConfiguration at the same time. This commit addresses this in the following manner: - ContextConfigurationAttributes no longer throws an IllegalArgumentException if both 'locations' and 'classes' are supplied to its constructor. - Concrete SmartContextLoader implementations now validate the supplied MergedContextConfiguration before attempting to load the ApplicationContext. See validateMergedContextConfiguration(). - Introduced tests for hybrid context loaders like the one used in Spring Boot. See HybridContextLoaderTests. - Updated the Testing chapter of the reference manual so that it no longer states that locations and classes cannot be used simultaneously, mentioning Spring Boot as well. - The Javadoc for @ContextConfiguration has been updated accordingly. - Added hasLocations(), hasClasses(), and hasResources() convenience methods to MergedContextConfiguration. Issue: SPR-11634
This commit is contained in:
parent
8edbdf4ddb
commit
1f017c4acb
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beansProjectDescription>
|
||||
<version>1</version>
|
||||
<pluginVersion><![CDATA[3.1.0.201210040510-RELEASE]]></pluginVersion>
|
||||
<pluginVersion><![CDATA[3.5.0.201402030809-M2]]></pluginVersion>
|
||||
<configSuffixes>
|
||||
<configSuffix><![CDATA[xml]]></configSuffix>
|
||||
</configSuffixes>
|
||||
|
@ -11,7 +11,10 @@
|
|||
<config>src/test/java/org/springframework/test/context/junit4/aci/xml/MultipleInitializersXmlConfigTests-context.xml</config>
|
||||
<config>src/test/resources/org/springframework/test/context/web/RequestAndSessionScopedBeansWacTests-context.xml</config>
|
||||
<config>src/test/resources/org/springframework/test/context/hierarchies/web/DispatcherWacRootWacEarTests-context.xml</config>
|
||||
<config>src/test/java/org/springframework/test/context/junit4/hybrid/hybrid-config.xml</config>
|
||||
</configs>
|
||||
<autoconfigs>
|
||||
</autoconfigs>
|
||||
<configSets>
|
||||
</configSets>
|
||||
</beansProjectDescription>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -36,11 +36,14 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||
*
|
||||
* <p>Prior to Spring 3.1, only path-based resource locations were supported.
|
||||
* As of Spring 3.1, {@linkplain #loader context loaders} may choose to support
|
||||
* either path-based or class-based resources (but not both). Consequently
|
||||
* <em>either</em> path-based <em>or</em> class-based resources. As of Spring
|
||||
* 4.0.4, {@linkplain #loader context loaders} may choose to support path-based
|
||||
* <em>and</em> class-based resources simultaneously. Consequently
|
||||
* {@code @ContextConfiguration} can be used to declare either path-based
|
||||
* resource locations (via the {@link #locations} or {@link #value}
|
||||
* attribute) <i>or</i> annotated classes (via the {@link #classes}
|
||||
* attribute).
|
||||
* resource locations (via the {@link #locations} or {@link #value} attribute)
|
||||
* <em>or</em> annotated classes (via the {@link #classes} attribute). Note,
|
||||
* however, that most implementations of {@link SmartContextLoader} only support
|
||||
* a single resource type.
|
||||
*
|
||||
* <h3>Annotated Classes</h3>
|
||||
*
|
||||
|
@ -86,9 +89,8 @@ public @interface ContextConfiguration {
|
|||
/**
|
||||
* Alias for {@link #locations}.
|
||||
*
|
||||
* <p>This attribute may <strong>not</strong> be used in conjunction
|
||||
* with {@link #locations} or {@link #classes}, but it may be used
|
||||
* instead of {@link #locations}.
|
||||
* <p>This attribute may <strong>not</strong> be used in conjunction with
|
||||
* {@link #locations}, but it may be used instead of {@link #locations}.
|
||||
* @since 3.0
|
||||
* @see #inheritLocations
|
||||
*/
|
||||
|
@ -117,8 +119,7 @@ public @interface ContextConfiguration {
|
|||
* loaders.
|
||||
*
|
||||
* <p>This attribute may <strong>not</strong> be used in conjunction with
|
||||
* {@link #value} or {@link #classes}, but it may be used instead of
|
||||
* {@link #value}.
|
||||
* {@link #value}, but it may be used instead of {@link #value}.
|
||||
* @since 2.5
|
||||
* @see #inheritLocations
|
||||
*/
|
||||
|
@ -135,9 +136,6 @@ public @interface ContextConfiguration {
|
|||
* <em>annotated classes</em> are specified. See the documentation for
|
||||
* {@link #loader} for further details regarding default loaders.
|
||||
*
|
||||
* <p>This attribute may <strong>not</strong> be used in conjunction with
|
||||
* {@link #locations} or {@link #value}.
|
||||
*
|
||||
* @since 3.1
|
||||
* @see org.springframework.context.annotation.Configuration
|
||||
* @see org.springframework.test.context.support.AnnotationConfigContextLoader
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -29,9 +29,8 @@ import org.springframework.util.ObjectUtils;
|
|||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@code ContextConfigurationAttributes} encapsulates the context
|
||||
* configuration attributes declared on a test class via
|
||||
* {@link ContextConfiguration @ContextConfiguration}.
|
||||
* {@code ContextConfigurationAttributes} encapsulates the context configuration
|
||||
* attributes declared via {@link ContextConfiguration @ContextConfiguration}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
|
@ -111,8 +110,9 @@ public class ContextConfigurationAttributes {
|
|||
|
||||
/**
|
||||
* Construct a new {@link ContextConfigurationAttributes} instance for the
|
||||
* supplied {@link ContextConfiguration @ContextConfiguration} annotation and
|
||||
* the {@linkplain Class test class} that declared it.
|
||||
* supplied {@link AnnotationAttributes} (parsed from a
|
||||
* {@link ContextConfiguration @ContextConfiguration} annotation) and
|
||||
* the {@linkplain Class test class} that declared them.
|
||||
* @param declaringClass the test class that declared {@code @ContextConfiguration}
|
||||
* @param annAttrs the annotation attributes from which to retrieve the attributes
|
||||
*/
|
||||
|
@ -140,7 +140,7 @@ public class ContextConfigurationAttributes {
|
|||
* @param inheritLocations the {@code inheritLocations} flag declared via {@code @ContextConfiguration}
|
||||
* @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
|
||||
* @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
|
||||
* {@code null}, or if the {@code locations} and {@code classes} are both non-empty
|
||||
* {@code null}
|
||||
* @deprecated as of Spring 3.2, use
|
||||
* {@link #ContextConfigurationAttributes(Class, String[], Class[], boolean, Class[], boolean, String, Class)}
|
||||
* instead
|
||||
|
@ -165,7 +165,7 @@ public class ContextConfigurationAttributes {
|
|||
* @param inheritInitializers the {@code inheritInitializers} flag declared via {@code @ContextConfiguration}
|
||||
* @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
|
||||
* @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
|
||||
* {@code null}, or if the {@code locations} and {@code classes} are both non-empty
|
||||
* {@code null}
|
||||
*/
|
||||
public ContextConfigurationAttributes(Class<?> declaringClass, String[] locations, Class<?>[] classes,
|
||||
boolean inheritLocations,
|
||||
|
@ -190,7 +190,7 @@ public class ContextConfigurationAttributes {
|
|||
* @param name the name of level in the context hierarchy, or {@code null} if not applicable
|
||||
* @param contextLoaderClass the {@code ContextLoader} class declared via {@code @ContextConfiguration}
|
||||
* @throws IllegalArgumentException if the {@code declaringClass} or {@code contextLoaderClass} is
|
||||
* {@code null}, or if the {@code locations} and {@code classes} are both non-empty
|
||||
* {@code null}
|
||||
*/
|
||||
public ContextConfigurationAttributes(Class<?> declaringClass, String[] locations, Class<?>[] classes,
|
||||
boolean inheritLocations,
|
||||
|
@ -200,14 +200,13 @@ public class ContextConfigurationAttributes {
|
|||
Assert.notNull(declaringClass, "declaringClass must not be null");
|
||||
Assert.notNull(contextLoaderClass, "contextLoaderClass must not be null");
|
||||
|
||||
if (!ObjectUtils.isEmpty(locations) && !ObjectUtils.isEmpty(classes)) {
|
||||
String msg = String.format(
|
||||
if (!ObjectUtils.isEmpty(locations) && !ObjectUtils.isEmpty(classes) && logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'locations' (or 'value') %s "
|
||||
+ "and 'classes' %s attributes. Only one declaration of resources "
|
||||
+ "is permitted per @ContextConfiguration annotation.", declaringClass.getName(),
|
||||
ObjectUtils.nullSafeToString(locations), ObjectUtils.nullSafeToString(classes));
|
||||
logger.error(msg);
|
||||
throw new IllegalArgumentException(msg);
|
||||
+ "and 'classes' %s attributes. Most SmartContextLoader implementations support "
|
||||
+ "only one declaration of resources per @ContextConfiguration annotation.",
|
||||
declaringClass.getName(), ObjectUtils.nullSafeToString(locations),
|
||||
ObjectUtils.nullSafeToString(classes)));
|
||||
}
|
||||
|
||||
this.declaringClass = declaringClass;
|
||||
|
|
|
@ -226,6 +226,46 @@ public class MergedContextConfiguration implements Serializable {
|
|||
return classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this {@code MergedContextConfiguration} instance has
|
||||
* path-based resource locations.
|
||||
*
|
||||
* @return {@code true} if the {@link #getLocations() locations} array is not empty
|
||||
* @since 4.0.4
|
||||
* @see #hasResources()
|
||||
* @see #hasClasses()
|
||||
*/
|
||||
public boolean hasLocations() {
|
||||
return !ObjectUtils.isEmpty(getLocations());
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this {@code MergedContextConfiguration} instance has
|
||||
* class-based resources.
|
||||
*
|
||||
* @return {@code true} if the {@link #getClasses() classes} array is not empty
|
||||
* @since 4.0.4
|
||||
* @see #hasResources()
|
||||
* @see #hasLocations()
|
||||
*/
|
||||
public boolean hasClasses() {
|
||||
return !ObjectUtils.isEmpty(getClasses());
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this {@code MergedContextConfiguration} instance has
|
||||
* either path-based resource locations or class-based resources.
|
||||
*
|
||||
* @return {@code true} if either the {@link #getLocations() locations}
|
||||
* or the {@link #getClasses() classes} array is not empty
|
||||
* @since 4.0.4
|
||||
* @see #hasLocations()
|
||||
* @see #hasClasses()
|
||||
*/
|
||||
public boolean hasResources() {
|
||||
return hasLocations() || hasClasses();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the merged {@code ApplicationContextInitializer} classes for the
|
||||
* {@linkplain #getTestClass() test class}.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -68,6 +68,8 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
* <p>Implementation details:
|
||||
*
|
||||
* <ul>
|
||||
* <li>Calls {@link #validateMergedContextConfiguration(MergedContextConfiguration)}
|
||||
* to allow subclasses to validate the supplied configuration before proceeding.</li>
|
||||
* <li>Creates a {@link GenericApplicationContext} instance.</li>
|
||||
* <li>If the supplied {@code MergedContextConfiguration} references a
|
||||
* {@linkplain MergedContextConfiguration#getParent() parent configuration},
|
||||
|
@ -106,6 +108,8 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
mergedConfig));
|
||||
}
|
||||
|
||||
validateMergedContextConfiguration(mergedConfig);
|
||||
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
|
||||
ApplicationContext parent = mergedConfig.getParentApplicationContext();
|
||||
|
@ -123,6 +127,20 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the supplied {@link MergedContextConfiguration} with respect to
|
||||
* what this context loader supports.
|
||||
* <p>The default implementation is a <em>no-op</em> but can be overridden by
|
||||
* subclasses as appropriate.
|
||||
* @param mergedConfig the merged configuration to validate
|
||||
* @throws IllegalStateException if the supplied configuration is not valid
|
||||
* for this context loader
|
||||
* @since 4.0.4
|
||||
*/
|
||||
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a Spring ApplicationContext from the supplied {@code locations}.
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -76,9 +76,8 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
|||
*/
|
||||
@Override
|
||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||
if (ObjectUtils.isEmpty(configAttributes.getClasses()) && isGenerateDefaultLocations()) {
|
||||
Class<?>[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes.getDeclaringClass());
|
||||
configAttributes.setClasses(defaultConfigClasses);
|
||||
if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
|
||||
configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,6 +147,24 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
|||
|
||||
// --- AbstractGenericContextLoader ----------------------------------------
|
||||
|
||||
/**
|
||||
* Ensure that the supplied {@link MergedContextConfiguration} does not
|
||||
* contain {@link MergedContextConfiguration#getLocations() locations}.
|
||||
* @since 4.0.4
|
||||
* @see AbstractGenericContextLoader#validateMergedContextConfiguration
|
||||
*/
|
||||
@Override
|
||||
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
|
||||
if (mergedConfig.hasLocations()) {
|
||||
String msg = String.format(
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'locations' (or 'value') attribute %s, "
|
||||
+ "but %s does not support resource locations.", mergedConfig.getTestClass().getName(),
|
||||
ObjectUtils.nullSafeToString(mergedConfig.getLocations()), getClass().getSimpleName());
|
||||
logger.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register classes in the supplied {@link GenericApplicationContext context}
|
||||
* from the classes in the supplied {@link MergedContextConfiguration}.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -83,7 +83,7 @@ public abstract class AnnotationConfigContextLoaderUtils {
|
|||
* configuration classes that comply with the constraints required of
|
||||
* {@code @Configuration} class implementations. If a potential candidate
|
||||
* configuration class does not meet these requirements, this method will log a
|
||||
* warning, and the potential candidate class will be ignored.
|
||||
* debug message, and the potential candidate class will be ignored.
|
||||
* @param declaringClass the test class that declared {@code @ContextConfiguration}
|
||||
* @return an array of default configuration classes, potentially empty but
|
||||
* never {@code null}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,6 +21,8 @@ import java.util.Properties;
|
|||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link AbstractGenericContextLoader} that reads
|
||||
|
@ -45,8 +47,26 @@ public class GenericPropertiesContextLoader extends AbstractGenericContextLoader
|
|||
* Returns "{@code -context.properties}".
|
||||
*/
|
||||
@Override
|
||||
public String getResourceSuffix() {
|
||||
protected String getResourceSuffix() {
|
||||
return "-context.properties";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the supplied {@link MergedContextConfiguration} does not
|
||||
* contain {@link MergedContextConfiguration#getClasses() classes}.
|
||||
* @since 4.0.4
|
||||
* @see AbstractGenericContextLoader#validateMergedContextConfiguration
|
||||
*/
|
||||
@Override
|
||||
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
|
||||
if (mergedConfig.hasClasses()) {
|
||||
String msg = String.format(
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, "
|
||||
+ "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(),
|
||||
ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName());
|
||||
logger.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -19,6 +19,8 @@ package org.springframework.test.context.support;
|
|||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link AbstractGenericContextLoader} that reads
|
||||
|
@ -43,8 +45,26 @@ public class GenericXmlContextLoader extends AbstractGenericContextLoader {
|
|||
* Returns "{@code -context.xml}".
|
||||
*/
|
||||
@Override
|
||||
public String getResourceSuffix() {
|
||||
protected String getResourceSuffix() {
|
||||
return "-context.xml";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the supplied {@link MergedContextConfiguration} does not
|
||||
* contain {@link MergedContextConfiguration#getClasses() classes}.
|
||||
* @since 4.0.4
|
||||
* @see AbstractGenericContextLoader#validateMergedContextConfiguration
|
||||
*/
|
||||
@Override
|
||||
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
|
||||
if (mergedConfig.hasClasses()) {
|
||||
String msg = String.format(
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, "
|
||||
+ "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(),
|
||||
ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName());
|
||||
logger.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -20,7 +20,6 @@ import javax.servlet.ServletContext;
|
|||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
@ -59,7 +58,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
|
|||
*/
|
||||
public abstract class AbstractGenericWebContextLoader extends AbstractContextLoader {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(AbstractGenericWebContextLoader.class);
|
||||
protected static final Log logger = LogFactory.getLog(AbstractGenericWebContextLoader.class);
|
||||
|
||||
|
||||
// --- SmartContextLoader -----------------------------------------------
|
||||
|
@ -71,6 +70,8 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
|
|||
* <p>Implementation details:
|
||||
*
|
||||
* <ul>
|
||||
* <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
|
||||
* to allow subclasses to validate the supplied configuration before proceeding.</li>
|
||||
* <li>Creates a {@link GenericWebApplicationContext} instance.</li>
|
||||
* <li>If the supplied {@code MergedContextConfiguration} references a
|
||||
* {@linkplain MergedContextConfiguration#getParent() parent configuration},
|
||||
|
@ -114,6 +115,8 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
|
|||
webMergedConfig));
|
||||
}
|
||||
|
||||
validateMergedContextConfiguration(webMergedConfig);
|
||||
|
||||
GenericWebApplicationContext context = new GenericWebApplicationContext();
|
||||
|
||||
ApplicationContext parent = mergedConfig.getParentApplicationContext();
|
||||
|
@ -131,6 +134,20 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
|
|||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the supplied {@link WebMergedContextConfiguration} with respect to
|
||||
* what this context loader supports.
|
||||
* <p>The default implementation is a <em>no-op</em> but can be overridden by
|
||||
* subclasses as appropriate.
|
||||
* @param mergedConfig the merged configuration to validate
|
||||
* @throws IllegalStateException if the supplied configuration is not valid
|
||||
* for this context loader
|
||||
* @since 4.0.4
|
||||
*/
|
||||
protected void validateMergedContextConfiguration(WebMergedContextConfiguration mergedConfig) {
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures web resources for the supplied web application context (WAC).
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -18,9 +18,9 @@ package org.springframework.test.context.web;
|
|||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoaderUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
|
@ -77,9 +77,8 @@ public class AnnotationConfigWebContextLoader extends AbstractGenericWebContextL
|
|||
*/
|
||||
@Override
|
||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||
if (ObjectUtils.isEmpty(configAttributes.getClasses()) && isGenerateDefaultLocations()) {
|
||||
Class<?>[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes.getDeclaringClass());
|
||||
configAttributes.setClasses(defaultConfigClasses);
|
||||
if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
|
||||
configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,4 +169,22 @@ public class AnnotationConfigWebContextLoader extends AbstractGenericWebContextL
|
|||
new AnnotatedBeanDefinitionReader(context).register(annotatedClasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the supplied {@link WebMergedContextConfiguration} does not
|
||||
* contain {@link MergedContextConfiguration#getLocations() locations}.
|
||||
* @since 4.0.4
|
||||
* @see AbstractGenericWebContextLoader#validateMergedContextConfiguration
|
||||
*/
|
||||
@Override
|
||||
protected void validateMergedContextConfiguration(WebMergedContextConfiguration webMergedConfig) {
|
||||
if (webMergedConfig.hasLocations()) {
|
||||
String msg = String.format(
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'locations' (or 'value') attribute %s, "
|
||||
+ "but %s does not support resource locations.", webMergedConfig.getTestClass().getName(),
|
||||
ObjectUtils.nullSafeToString(webMergedConfig.getLocations()), getClass().getSimpleName());
|
||||
logger.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -17,6 +17,8 @@
|
|||
package org.springframework.test.context.web;
|
||||
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
|
||||
/**
|
||||
|
@ -46,4 +48,22 @@ public class GenericXmlWebContextLoader extends AbstractGenericWebContextLoader
|
|||
return "-context.xml";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the supplied {@link WebMergedContextConfiguration} does not
|
||||
* contain {@link MergedContextConfiguration#getClasses() classes}.
|
||||
* @since 4.0.4
|
||||
* @see AbstractGenericWebContextLoader#validateMergedContextConfiguration
|
||||
*/
|
||||
@Override
|
||||
protected void validateMergedContextConfiguration(WebMergedContextConfiguration webMergedConfig) {
|
||||
if (webMergedConfig.hasClasses()) {
|
||||
String msg = String.format(
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, "
|
||||
+ "but %s does not support annotated classes.", webMergedConfig.getTestClass().getName(),
|
||||
ObjectUtils.nullSafeToString(webMergedConfig.getClasses()), getClass().getSimpleName());
|
||||
logger.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -143,9 +143,26 @@ public class ContextLoaderUtilsConfigurationAttributesTests extends AbstractCont
|
|||
assertClassesFooAttributes(attributesList.get(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies change requested in <a href="https://jira.spring.io/browse/SPR-11634">SPR-11634</a>.
|
||||
* @since 4.0.4
|
||||
*/
|
||||
@Test
|
||||
public void resolveConfigAttributesWithLocationsAndClasses() {
|
||||
List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsAndClasses.class);
|
||||
assertNotNull(attributesList);
|
||||
assertEquals(1, attributesList.size());
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ContextConfiguration(value = "x", locations = "y")
|
||||
private static class ConflictingLocations {
|
||||
}
|
||||
|
||||
@ContextConfiguration(locations = "x", classes = Object.class)
|
||||
private static class LocationsAndClasses {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.junit4.hybrid;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.SmartContextLoader;
|
||||
import org.springframework.test.context.support.AbstractGenericContextLoader;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import static org.springframework.test.context.support.AnnotationConfigContextLoaderUtils.*;
|
||||
|
||||
/**
|
||||
* Hybrid {@link SmartContextLoader} that supports path-based and class-based
|
||||
* resources simultaneously.
|
||||
* <p>This test loader is inspired by Spring Boot.
|
||||
* <p>Detects defaults for XML configuration and annotated classes.
|
||||
* <p>Beans from XML configuration always override those from annotated classes.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.0.4
|
||||
*/
|
||||
public class HybridContextLoader extends AbstractGenericContextLoader {
|
||||
|
||||
@Override
|
||||
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
|
||||
Assert.isTrue(mergedConfig.hasClasses() || mergedConfig.hasLocations(), getClass().getSimpleName()
|
||||
+ " requires either classes or locations");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||
// Detect default XML configuration files:
|
||||
super.processContextConfiguration(configAttributes);
|
||||
|
||||
// Detect default configuration classes:
|
||||
if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
|
||||
configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
// Order doesn't matter: <bean> always wins over @Bean.
|
||||
new XmlBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
|
||||
new AnnotatedBeanDefinitionReader(context).register(mergedConfig.getClasses());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
|
||||
throw new UnsupportedOperationException(getClass().getSimpleName() + " doesn't support this");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getResourceSuffix() {
|
||||
return "-context.xml";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="fooFromXml" class="java.lang.String" c:_="XML" />
|
||||
|
||||
<bean id="enigma" class="java.lang.String" c:_="enigma from XML" />
|
||||
|
||||
</beans>
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.junit4.hybrid;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.SmartContextLoader;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Integration tests for hybrid {@link SmartContextLoader} implementations that
|
||||
* support path-based and class-based resources simultaneously, as is done in
|
||||
* Spring Boot.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.0.4
|
||||
* @see HybridContextLoader
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(loader = HybridContextLoader.class)
|
||||
public class HybridContextLoaderTests {
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
public String fooFromJava() {
|
||||
return "Java";
|
||||
}
|
||||
|
||||
@Bean
|
||||
public String enigma() {
|
||||
return "enigma from Java";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Autowired
|
||||
private String fooFromXml;
|
||||
|
||||
@Autowired
|
||||
private String fooFromJava;
|
||||
|
||||
@Autowired
|
||||
private String enigma;
|
||||
|
||||
|
||||
@Test
|
||||
public void verifyContentsOfHybridApplicationContext() {
|
||||
assertEquals("XML", fooFromXml);
|
||||
assertEquals("Java", fooFromJava);
|
||||
|
||||
// Note: the XML bean definition for "enigma" always wins since
|
||||
// ConfigurationClassBeanDefinitionReader.isOverriddenByExistingDefinition()
|
||||
// lets XML bean definitions override those "discovered" later via an
|
||||
// @Bean method.
|
||||
assertEquals("enigma from XML", enigma);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -16,10 +16,14 @@
|
|||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AnnotationConfigContextLoader}.
|
||||
|
@ -31,6 +35,25 @@ public class AnnotationConfigContextLoaderTests {
|
|||
|
||||
private final AnnotationConfigContextLoader contextLoader = new AnnotationConfigContextLoader();
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
|
||||
/**
|
||||
* @since 4.0.4
|
||||
*/
|
||||
@Test
|
||||
public void configMustNotContainLocations() throws Exception {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage(containsString("does not support resource locations"));
|
||||
|
||||
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
|
||||
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, contextLoader);
|
||||
contextLoader.loadContext(mergedConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void detectDefaultConfigurationClassesForAnnotatedInnerClass() {
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GenericPropertiesContextLoader}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.0.4
|
||||
*/
|
||||
public class GenericPropertiesContextLoaderTests {
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void configMustNotContainAnnotatedClasses() throws Exception {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage(containsString("does not support annotated classes"));
|
||||
|
||||
GenericPropertiesContextLoader loader = new GenericPropertiesContextLoader();
|
||||
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
|
||||
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
|
||||
loader.loadContext(mergedConfig);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GenericXmlContextLoader}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.0.4
|
||||
* @see GenericXmlContextLoaderResourceLocationsTests
|
||||
*/
|
||||
public class GenericXmlContextLoaderTests {
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void configMustNotContainAnnotatedClasses() throws Exception {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage(containsString("does not support annotated classes"));
|
||||
|
||||
GenericXmlContextLoader loader = new GenericXmlContextLoader();
|
||||
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
|
||||
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
|
||||
loader.loadContext(mergedConfig);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.web;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AnnotationConfigWebContextLoader}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.0.4
|
||||
*/
|
||||
public class AnnotationConfigWebContextLoaderTests {
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void configMustNotContainLocations() throws Exception {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage(containsString("does not support resource locations"));
|
||||
|
||||
AnnotationConfigWebContextLoader loader = new AnnotationConfigWebContextLoader();
|
||||
WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(),
|
||||
new String[] { "config.xml" }, EMPTY_CLASS_ARRAY, null, EMPTY_STRING_ARRAY, "resource/path", loader, null,
|
||||
null);
|
||||
loader.loadContext(mergedConfig);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.web;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GenericXmlWebContextLoader}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.0.4
|
||||
*/
|
||||
public class GenericXmlWebContextLoaderTests {
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void configMustNotContainAnnotatedClasses() throws Exception {
|
||||
expectedException.expect(IllegalStateException.class);
|
||||
expectedException.expectMessage(containsString("does not support annotated classes"));
|
||||
|
||||
GenericXmlWebContextLoader loader = new GenericXmlWebContextLoader();
|
||||
WebMergedContextConfiguration mergedConfig = new WebMergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
|
||||
new Class<?>[] { getClass() }, null, EMPTY_STRING_ARRAY, "resource/path", loader, null, null);
|
||||
loader.loadContext(mergedConfig);
|
||||
}
|
||||
|
||||
}
|
|
@ -17823,8 +17823,8 @@ default attribute values, attribute aliases, and so on.
|
|||
|
||||
Defines class-level metadata that is used to determine how to load and configure an
|
||||
`ApplicationContext` for integration tests. Specifically, `@ContextConfiguration`
|
||||
declares __either__ the application context resource `locations` __or__ the annotated
|
||||
`classes` that will be used to load the context.
|
||||
declares the application context resource `locations` or the annotated `classes`
|
||||
that will be used to load the context.
|
||||
|
||||
+
|
||||
|
||||
|
@ -18841,9 +18841,18 @@ It may sometimes be desirable to mix XML resources and annotated classes (i.e.,
|
|||
typically `@Configuration` classes) to configure an `ApplicationContext` for your tests.
|
||||
For example, if you use XML configuration in production, you may decide that you want to
|
||||
use `@Configuration` classes to configure specific Spring-managed components for your
|
||||
tests, or vice versa. As mentioned in <<integration-testing-annotations-spring>> the
|
||||
TestContext framework does not allow you to declare __both__ via
|
||||
`@ContextConfiguration`, but this does not mean that you cannot use both.
|
||||
tests, or vice versa.
|
||||
|
||||
Furthermore, some third-party frameworks (like Spring Boot) provide first-class
|
||||
support for loading an `ApplicationContext` from different types of resources
|
||||
simultaneously (e.g., XML configuration files and `@Configuration` classes). The Spring
|
||||
Framework historically has not supported this for standard deployments. Consequently,
|
||||
each of the `SmartContextLoader` implementations that the Spring Framework delivers in
|
||||
the `spring-test` module supports only one resource type per test context;
|
||||
however, this does not mean that you cannot use both. Third-party frameworks may
|
||||
choose to support the declaration of both `locations` and `classes` via
|
||||
`@ContextConfiguration`, and with the standard testing support in the TestContext
|
||||
framework, you have the following options.
|
||||
|
||||
If you want to use XML __and__ `@Configuration` classes to configure your tests, you
|
||||
will have to pick one as the __entry point__, and that one will have to include or
|
||||
|
@ -18977,14 +18986,14 @@ with Spring's `@Order` or the standard `@Priority` annotation.
|
|||
----
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
// ApplicationContext will be initialized by BaseInitializer
|
||||
**@ContextConfiguration(initializers=BaseInitializer.class)**
|
||||
**@ContextConfiguration(initializers = BaseInitializer.class)**
|
||||
public class BaseTest {
|
||||
// class body...
|
||||
}
|
||||
|
||||
// ApplicationContext will be initialized by BaseInitializer
|
||||
// and ExtendedInitializer
|
||||
**@ContextConfiguration(initializers=ExtendedInitializer.class)**
|
||||
**@ContextConfiguration(initializers = ExtendedInitializer.class)**
|
||||
public class ExtendedTest extends BaseTest {
|
||||
// class body...
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue