diff --git a/spring-framework-reference/src/testing.xml b/spring-framework-reference/src/testing.xml index 6ffcf9438e3..3ceb985e24b 100644 --- a/spring-framework-reference/src/testing.xml +++ b/spring-framework-reference/src/testing.xml @@ -1026,8 +1026,10 @@ public void testProcessRepeatedly() { DelegatingSmartContextLoader: the default loader which delegates internally to an AnnotationConfigContextLoader or a - GenericXmlContextLoader depending on - the configuration declared for the test class. + GenericXmlContextLoader depending + either on the configuration declared for the test class or on + the presence of default locations or configuration + classes. @@ -1101,18 +1103,23 @@ public class MyTest { merely by declaring the @ContextConfiguration annotation at the class level. If your test class does not explicitly declare - application context resource locations, the - configured ContextLoader determines how - and whether to load a context from a default location. For example, - GenericXmlContextLoader, which is the default - ContextLoader, generates a default - location based on the name of the test class. If your class is named - com.example.MyTest, - GenericXmlContextLoader loads your application - context from - "classpath:/com/example/MyTest-context.xml". + application context resource locations or + configuration classes, the configured + ContextLoader determines how to load a + context from a default location or default configuration + classes. - package com.example; +
+ XML-based configuration + + For example, GenericXmlContextLoader + generates a default location based on the name of the test class. If + your class is named com.example.MyTest, + GenericXmlContextLoader loads your + application context from + "classpath:/com/example/MyTest-context.xml". + + package com.example; @RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from "classpath:/com/example/MyTest-context.xml" @@ -1121,56 +1128,69 @@ public class MyTest { // class body... } - If the default location does not suit your needs, you can - explicitly configure the locations attribute of - @ContextConfiguration with an array - that contains the resource locations of XML configuration metadata - (assuming an XML-capable ContextLoader - has been configured, which is the default). A plain path, for example - "context.xml", will be treated as a classpath - resource from the same package in which the test class is defined. A - path starting with a slash is treated as a fully qualified classpath - location, for example "/org/example/config.xml". A - path which represents a 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. + If the default location does not suit your needs, you can + explicitly configure the locations attribute of + @ContextConfiguration with an array + that contains the resource locations of XML configuration metadata + (assuming an XML-capable + ContextLoader has been configured, + which is the default). A plain path, for example + "context.xml", will be treated as a classpath + resource from the same package in which the test class is defined. A + path starting with a slash is treated as a fully qualified classpath + location, for example "/org/example/config.xml". + A path which represents a 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. - @RunWith(SpringJUnit4ClassRunner.class) -// ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml" -// in the root of the classpath -@ContextConfiguration({"/applicationContext.xml", "/applicationContext-test.xml"}) + @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"}) public class MyTest { // class body... } - @ContextConfiguration supports an - alias for the locations attribute through the - standard value attribute. Thus, if you do not need - to configure a custom ContextLoader, - you can omit the declaration of the locations - attribute name and declare the resource locations by using the - shorthand format demonstrated in the following example. + @ContextConfiguration supports + an alias for the locations attribute through the + standard value attribute. Thus, if you do not + need to configure a custom + ContextLoader, you can omit the + declaration of the locations attribute name and + declare the resource locations by using the shorthand format + demonstrated in the following example. - @ContextConfiguration also - supports a boolean inheritLocations attribute that - denotes whether resource locations from superclasses should be - inherited. The default value is - true, which means that an annotated class inherits - the resource locations defined by an annotated superclass. - Specifically, the resource locations for an annotated class are - appended to the list of resource locations defined by an annotated - superclass. Thus, subclasses have the option of - extending the list of resource locations. In the - following example, the - ApplicationContext for - ExtendedTest is loaded from "/base-context.xml" - and "/extended-context.xml", in that - order. Beans defined in "/extended-context.xml" may therefore override - those defined in "/base-context.xml". + @RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({"/applicationContext.xml", "/applicationContext-test.xml"}) +public class MyTest { + // class body... +} +
- @RunWith(SpringJUnit4ClassRunner.class) +
+ Configuration inheritance + + @ContextConfiguration also + supports a boolean inheritLocations attribute + that denotes whether resource locations from superclasses should be + inherited. The default value is + true, which means that an annotated class + inherits the resource locations defined by an annotated superclass. + Specifically, the resource locations for an annotated class are + appended to the list of resource locations defined by an annotated + superclass. Thus, subclasses have the option of + extending the list of resource locations. In + the following example, the + ApplicationContext for + ExtendedTest is loaded from + "/base-context.xml" and + "/extended-context.xml", in that order. Beans defined in + "/extended-context.xml" may therefore override those defined in + "/base-context.xml". + + @RunWith(SpringJUnit4ClassRunner.class) // ApplicationContext will be loaded from "/base-context.xml" in the root of the classpath @ContextConfiguration("/base-context.xml") public class BaseTest { @@ -1184,23 +1204,28 @@ public class ExtendedTest extends BaseTest { // class body... } - If inheritLocations is set to - false, the resource locations for the annotated - class shadow and effectively replace any resource locations defined by - a superclass. + If inheritLocations is set to + false, the resource locations for the annotated + class shadow and effectively replace any resource locations defined + by a superclass. +
- By default, once loaded, the configured - ApplicationContext is reused for each - test. Thus the setup cost 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. +
+ Context caching + + By default, once loaded, the configured + ApplicationContext is reused for each + test. Thus the setup cost 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. +