diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java b/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java
index 623f30b166..1638f28cc3 100644
--- a/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java
+++ b/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java
@@ -116,19 +116,19 @@ public @interface ContextConfiguration {
* {@link #classes configuration classes} from superclasses should be
* inherited.
*
- *
The default value is true, which means that an annotated
+ *
The default value is true. This means that an annotated
* class will inherit the resource locations or configuration
- * classes defined by an annotated superclass. Specifically, the resource
+ * classes defined by annotated superclasses. Specifically, the resource
* locations or configuration classes for an annotated class will be
* appended to the list of resource locations or configuration classes
- * defined by an annotated superclass. Thus, subclasses have the option of
+ * defined by annotated superclasses. Thus, subclasses have the option of
* extending the list of resource locations or configuration
* classes.
*
*
If inheritLocations is set to false, the
* resource locations or configuration classes for the annotated class
* will shadow and effectively replace any resource locations
- * or configuration classes defined by a superclass.
+ * or configuration classes defined by superclasses.
*
*
In the following example that uses path-based resource locations, the
* {@link org.springframework.context.ApplicationContext ApplicationContext}
diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java
index 0e71500a1a..44e18cf1ec 100644
--- a/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java
+++ b/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java
@@ -152,10 +152,10 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
/**
* Generate a modified version of the supplied locations array and return it.
- *
A plain path — for example, "context.xml" —
- * will be treated as a classpath resource from the same package in which
+ *
A plain path — for example, "context.xml" — will
+ * be treated as a classpath resource that is relative to the package in which
* the specified class is defined. A path starting with a slash is treated
- * as a fully qualified classpath location, for example:
+ * as an absolute classpath location, for example:
* "/org/springframework/whatever/foo.xml". A path which
* references a URL (e.g., a path prefixed with
* {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
diff --git a/spring-framework-reference/src/testing.xml b/spring-framework-reference/src/testing.xml
index 7df8c3902d..29ee349362 100644
--- a/spring-framework-reference/src/testing.xml
+++ b/spring-framework-reference/src/testing.xml
@@ -1101,8 +1101,8 @@ public class MyTest {
In contrast to the deprecated JUnit 3.8 legacy class hierarchy,
test classes that use the TestContext framework do not need to
override any protected instance methods to
- configure their application context. Rather, configuration is achieved
- merely by declaring the
+ configure their application context. Instead, configuration is
+ achieved merely by declaring the
@ContextConfiguration annotation at the
class level. If your test class does not explicitly declare
application context resource locations or
@@ -1111,9 +1111,9 @@ public class MyTest {
context from a default location or default configuration
classes.
- The following sections explain how to configure and manage
- ApplicationContexts via XML
- configuration files and @Configuration
+ The following sections explain how to configure and manage an
+ ApplicationContext via XML
+ configuration files or @Configuration
classes using Spring's
@ContextConfiguration
annotation.
@@ -1121,28 +1121,28 @@ public class MyTest {
Context management with XML resources
- To load an
- ApplicationContextAware for your
- tests from XML configuration files, annotate your test class with
- @ContextConfiguration and configure
- the locations attribute with an array that
- contains the resource locations of XML configuration metadata. A
- plain path — for example "context.xml" — will be
- treated as a classpath resource from the package in which the test
- class is defined. A path starting with a slash is treated as a fully
- qualified classpath location, for example
+ To load an ApplicationContext
+ for your tests from XML configuration files, annotate your test
+ class with @ContextConfiguration and
+ configure the locations attribute with an array
+ that contains the resource locations of XML configuration metadata.
+ A plain path — for example "context.xml" — will
+ be treated as a classpath resource that is relative to the package
+ in which the test class is defined. A path starting with a slash is
+ treated as an absolute classpath location, for example
"/org/example/config.xml". A path which
- represents a URL (i.e., a path prefixed with
+ represents a resource URL (i.e., a path prefixed with
classpath:, file:,
http:, etc.) will be used as
is. Alternatively, you can implement and configure your
- own custom ContextLoader for advanced
- use cases.
+ own custom ContextLoader or
+ SmartContextLoader for advanced use
+ cases.
@RunWith(SpringJUnit4ClassRunner.class)
-// ApplicationContext will be loaded from "/applicationContext.xml" and
-// "/applicationContext-test.xml" in the root of the classpath
-@ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"})
+// ApplicationContext will be loaded from "/app-config.xml" and
+// "/test-config.xml" in the root of the classpath
+@ContextConfiguration(locations={"/app-config.xml", "/test-config.xml"})
public class MyTest {
// class body...
}
@@ -1157,7 +1157,7 @@ public class MyTest {
demonstrated in the following example.
@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration({"/applicationContext.xml", "/applicationContext-test.xml"})
+@ContextConfiguration({"/app-config.xml", "/test-config.xml"})
public class MyTest {
// class body...
}
@@ -1187,20 +1187,20 @@ public class MyTest {
Context management with @Configuration classes
- To load an
- ApplicationContextAware for your
- tests from @Configuration classes,
- annotate your test class with
+ To load an ApplicationContext
+ for your tests from @Configuration
+ classes, annotate your test class with
@ContextConfiguration and configure
the classes attribute with an array that contains
class references to configuration classes. Alternatively, you can
implement and configure your own custom
- ContextLoader for advanced use
+ ContextLoader or
+ SmartContextLoader for advanced use
cases.
@RunWith(SpringJUnit4ClassRunner.class)
-// ApplicationContext will be loaded from AppConfig and TestAppConfig
-@ContextConfiguration(classes={AppConfig.class, TestAppConfig.class})
+// ApplicationContext will be loaded from AppConfig and TestConfig
+@ContextConfiguration(classes={AppConfig.class, TestConfig.class})
public class MyTest {
// class body...
}
@@ -1259,32 +1259,42 @@ public class OrderServiceTest {
a boolean inheritLocations attribute that denotes
whether resource locations or configuration classes declared by
superclasses should be inherited. The default
- value is true, which means that an annotated
- class inherits the resource locations or configuration classes
- declared by an annotated superclass. Specifically, the resource
- locations or configuration classes for an annotated test class are
- appended to the list of resource locations or configuration classes
- declared by an annotated superclass. Thus, subclasses have the
- option of extending the list of resource
- locations or configuration classes. In the following example that
- uses XML resource locations, the
+ value is true. This means that an annotated class
+ inherits the resource locations or configuration classes declared by
+ any annotated superclasses. Specifically, the resource locations or
+ configuration classes for an annotated test class are appended to
+ the list of resource locations or configuration classes declared by
+ annotated superclasses. Thus, subclasses have the option of
+ extending the list of resource locations or
+ configuration classes.
+
+ If @ContextConfiguration's
+ inheritLocations attribute is set to
+ false, the resource locations or configuration
+ classes for the annotated class shadow and
+ effectively replace any resource locations or configuration classes
+ defined by superclasses.
+
+ In the following example that uses XML resource locations, the
ApplicationContext for
ExtendedTest will be loaded from
- "base-context.xml" and
- "extended-context.xml", in that order. Beans defined in
- "extended-context.xml" may therefore override
- (i.e., replace) those defined in "base-context.xml".
+ "base-config.xml" and
+ "extended-config.xml", in that order. Beans
+ defined in "extended-config.xml" may therefore
+ override (i.e., replace) those defined in
+ "base-config.xml".
@RunWith(SpringJUnit4ClassRunner.class)
-// ApplicationContext will be loaded from "/base-context.xml" in the root of the classpath
-@ContextConfiguration("/base-context.xml")
+// ApplicationContext will be loaded from "/base-config.xml" in the root of the classpath
+@ContextConfiguration("/base-config.xml")
public class BaseTest {
// class body...
}
-// ApplicationContext will be loaded from "/base-context.xml" and "/extended-context.xml"
+// ApplicationContext will be loaded from "/base-config.xml" and "/extended-config.xml"
// in the root of the classpath
-@ContextConfiguration("/extended-context.xml")
+@ContextConfiguration("/extended-config.xml")
public class ExtendedTest extends BaseTest {
// class body...
}
@@ -1299,24 +1309,17 @@ public class ExtendedTest extends BaseTest {
replace) those defined in BaseConfig.
@RunWith(SpringJUnit4ClassRunner.class)
-// ApplicationContext will be loaded from "/base-context.xml" in the root of the classpath
+// ApplicationContext will be loaded from BaseConfig
@ContextConfiguration(classes=BaseConfig.class)
public class BaseTest {
// class body...
}
-// ApplicationContext will be loaded from "/base-context.xml" and "/extended-context.xml"
-// in the root of the classpath
+// ApplicationContext will be loaded from BaseConfig and ExtendedConfig
@ContextConfiguration(classes=ExtendedConfig.class)
public class ExtendedTest extends BaseTest {
// class body...
}
-
- If @ContextConfiguration's
- inheritLocations attribute is set to
- false, the resource locations or configuration
- classes for the annotated class shadow and effectively replace any
- resource locations defined by a superclass.
@@ -1327,20 +1330,23 @@ public class ExtendedTest extends BaseTest {
for a test it will be reused for all subsequent tests that declare the same
unique context configuration within the same process — for example,
- all tests run in a suite in an IDE or all tests run for the same
- project from a build framework like Ant or Maven. Thus the setup
- cost for loading the application context is incurred only once (per
+ all tests run in a suite within an IDE or all tests run for the same
+ project with a build framework like Ant or Maven. Thus the setup
+ cost for loading an application context is incurred only once (per
test suite), and subsequent test execution is much faster.
In the unlikely case that a test corrupts the application
context and requires reloading — for example, by modifying a bean
definition or the state of an application object — you can annotate
your test class or test method with
- @DirtiesContext (assuming
- DirtiesContextTestExecutionListener has been
- configured, which is the default). This instructs Spring to reload
- the configuration and rebuild the application context before
- executing the next test.
+ @DirtiesContext (see the discussion
+ of @DirtiesContext in ). This instructs
+ Spring to reload the configuration and rebuild the application
+ context before executing the next test. Note that support for the
+ @DirtiesContext annotation is enabled
+ via the DirtiesContextTestExecutionListener
+ which is enabled by default.