注释类型 ContextHierarchy
@Target(TYPE) @Retention(RUNTIME) @Documented @Inherited public @interface ContextHierarchy
@ContextHierarchy
is a class-level annotation that is used to define a hierarchy ofApplicationContexts
for integration tests.Examples
The following JUnit-based examples demonstrate common configuration scenarios for integration tests that require the use of context hierarchies.
Single Test Class with Context Hierarchy
ControllerIntegrationTests
represents a typical integration testing scenario for a Spring MVC web application by declaring a context hierarchy consisting of two levels, one for the rootWebApplicationContext
(withTestAppConfig
) and one for the dispatcher servletWebApplicationContext
(withWebConfig
). TheWebApplicationContext
that is autowired into the test instance is the one for the child context (i.e., the lowest context in the hierarchy).@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(classes = TestAppConfig.class), @ContextConfiguration(classes = WebConfig.class) }) public class ControllerIntegrationTests { @Autowired private WebApplicationContext wac; // ... }
Class Hierarchy with Implicit Parent Context
The following test classes define a context hierarchy within a test class hierarchy.
AbstractWebTests
declares the configuration for a rootWebApplicationContext
in a Spring-powered web application. Note, however, thatAbstractWebTests
does not declare@ContextHierarchy
; consequently, subclasses ofAbstractWebTests
can optionally participate in a context hierarchy or follow the standard semantics for@ContextConfiguration
.SoapWebServiceTests
andRestWebServiceTests
both extendAbstractWebTests
and define a context hierarchy via@ContextHierarchy
. The result is that three application contexts will be loaded (one for each declaration of@ContextConfiguration
, and the application context loaded based on the configuration inAbstractWebTests
will be set as the parent context for each of the contexts loaded for the concrete subclasses.@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml") public abstract class AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/soap-ws-config.xml") public class SoapWebServiceTests extends AbstractWebTests {} @ContextHierarchy(@ContextConfiguration("/spring/rest-ws-config.xml") public class RestWebServiceTests extends AbstractWebTests {}
Class Hierarchy with Merged Context Hierarchy Configuration
The following classes demonstrate the use of named hierarchy levels in order to merge the configuration for specific levels in a context hierarchy.
BaseTests
defines two levels in the hierarchy,parent
andchild
.ExtendedTests
extendsBaseTests
and instructs the Spring TestContext Framework to merge the context configuration for thechild
hierarchy level, simply by ensuring that the names declared viaContextConfiguration.name()
are both"child"
. The result is that three application contexts will be loaded: one for"/app-config.xml"
, one for"/user-config.xml"
, and one for{"/user-config.xml", "/order-config.xml"}
. As with the previous example, the application context loaded from"/app-config.xml"
will be set as the parent context for the contexts loaded from"/user-config.xml"
and{"/user-config.xml", "/order-config.xml"}
.@RunWith(SpringJUnit4ClassRunner.class) @ContextHierarchy({ @ContextConfiguration(name = "parent", locations = "/app-config.xml"), @ContextConfiguration(name = "child", locations = "/user-config.xml") }) public class BaseTests {} @ContextHierarchy( @ContextConfiguration(name = "child", locations = "/order-config.xml") ) public class ExtendedTests extends BaseTests {}
Class Hierarchy with Overridden Context Hierarchy Configuration
In contrast to the previous example, this example demonstrates how to override the configuration for a given named level in a context hierarchy by setting the
ContextConfiguration.inheritLocations()
flag tofalse
. Consequently, the application context forExtendedTests
will be loaded only from"/test-user-config.xml"
and will have its parent set to the context loaded from"/app-config.xml"
.@RunWith(SpringJUnit4ClassRunner.class) @ContextHierarchy({ @ContextConfiguration(name = "parent", locations = "/app-config.xml"), @ContextConfiguration(name = "child", locations = "/user-config.xml") }) public class BaseTests {} @ContextHierarchy( @ContextConfiguration(name = "child", locations = "/test-user-config.xml", inheritLocations=false) ) public class ExtendedTests extends BaseTests {}
As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.
- 从以下版本开始:
- 3.2.2
- 作者:
- Sam Brannen
- 另请参阅:
ContextConfiguration
,ApplicationContext
必需元素概要
所需元素 修饰符和类型 必需的元素 说明 ContextConfiguration[]
value
A list of@ContextConfiguration
instances, each of which defines a level in the context hierarchy.
元素详细资料
value
ContextConfiguration[] value
A list of@ContextConfiguration
instances, each of which defines a level in the context hierarchy.If you need to merge or override the configuration for a given level of the context hierarchy within a test class hierarchy, you must explicitly name that level by supplying the same value to the
name
attribute in@ContextConfiguration
at each level in the class hierarchy. See the class-level Javadoc for examples.