Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support WebApplicationContext hierarchies in the TestContext Framework [SPR-9863] #14496

spring-issuemaster opened this issue Oct 7, 2012 · 2 comments


Copy link

@spring-issuemaster spring-issuemaster commented Oct 7, 2012

Sam Brannen opened SPR-9863 and commented


This issue picks up where #9917 left off. Specifically this issue focuses on adding support for WebApplicationContext hierarchies within single test classes as well as within test class hierarchies.

Consequently, this issue is heavily dependent on the completion of both #9917 and #10284.


  • Ensure that common WAC hierarchies can be configured in integration tests (e.g., root and dispatcher WACs in a parent-child relationship).


  1. Ensure that hierarchies of WACs can be configured using @ContextHierarchy (see #10284)
  2. Ensure that the type of the root WAC can differ from that of its parent (e.g., a standard ApplicationContext loaded for an EAR)
  3. Set a loaded context as the ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE in the MockServletContext when context hierarchies are used:
    • if an ApplicationContext has no parent and the context is a WAC, set the context as the root WAC
    • if an ApplicationContext has a parent that is not a WAC and the context itself is a WAC, set the context as the root WAC

Pseudocode Examples

Root WAC & Dispatcher WAC
@WebAppConfiguration // path defaults to "file:src/main/webapp"
    @ContextConfiguration(name="root", classes = WebAppConfig.class),
    @ContextConfiguration(name="dispatcher", locations="/spring/dispatcher-config.xml")
public class ControllerIntegrationTests {}

Class & Context Hierarchies
public abstract class AbstractWebTests {}

public class SoapWebServiceTests extends AbstractWebTests {}

public class RestWebServiceTests extends AbstractWebTests {}

Class & Context Hierarchies with Merged and Overridden Configuration

In ExtendedControllerIntegrationTests the configuration for the root WAC will be overridden, and the configuration for the dispatcher WAC will be merged.

@WebAppConfiguration // defaults to "file:src/main/webapp"
    @ContextConfiguration(name="root", classes = WebAppConfig.class),
    @ContextConfiguration(name="dispatcher", locations="/spring/user-config.xml")
public class ControllerIntegrationTests {}

    @ContextConfiguration(name="root", locations="/spring/root2.xml", inheritLocations=false),
    @ContextConfiguration(name="dispatcher", locations="/spring/orders-config.xml"),
public class ExtendedControllerIntegrationTests extends ControllerIntegrationTests {}

Affects: 2.5 final

Issue Links:

  • #9917 Support loading WebApplicationContexts with the TestContext Framework ("depends on")
  • #10284 Provide support for context hierarchies in the TestContext Framework ("depends on")
  • #14989 Document context hierarchy support in the TestContext framework ("is depended on by")
  • #9309 Load dedicated child ApplicationContext for test instance in the TestContext framework

Referenced from: commits 98074e7

3 votes, 7 watchers

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 6, 2013

Sam Brannen commented

Submitted pull request: #247

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Mar 6, 2013

Sam Brannen commented

Completed as described in the comments for GitHub commit 4c5d771764:

Provide support for context hierarchies in the TCF

Prior to this commit the Spring TestContext Framework supported creating
only flat, non-hierarchical contexts. There was no easy way to create
contexts with parent-child relationships.

This commit addresses this issue by introducing a new @ContextHierarchy
annotation that can be used in conjunction with @ContextConfiguration
for declaring hierarchies of application contexts, either within a
single test class or within a test class hierarchy. In addition,
@DirtiesContext now supports a new 'hierarchyMode' attribute for
controlling context cache clearing for context hierarchies.

  • Introduced a new @ContextHierarchy annotation.
  • Introduced 'name' attribute in @ContextConfiguration.
  • Introduced 'name' property in ContextConfigurationAttributes.
  • TestContext is now aware of @ContextHierarchy in addition to
  • Introduced findAnnotationDeclaringClassForTypes() in AnnotationUtils.
  • Introduced resolveContextHierarchyAttributes() in ContextLoaderUtils.
  • Introduced buildContextHierarchyMap() in ContextLoaderUtils.
  • @ContextConfiguration and @ContextHierarchy may not be used as
    top-level, class-level annotations simultaneously.
  • Introduced reference to the parent configuration in
    MergedContextConfiguration and WebMergedContextConfiguration.
  • Introduced overloaded buildMergedContextConfiguration() methods in
    ContextLoaderUtils in order to handle context hierarchies separately
    from conventional, non-hierarchical contexts.
  • Introduced hashCode() and equals() in ContextConfigurationAttributes.
  • ContextLoaderUtils ensures uniqueness of @ContextConfiguration
    elements within a single @ContextHierarchy declaration.
  • Introduced CacheAwareContextLoaderDelegate that can be used for
    loading contexts with transparent support for interacting with the
    context cache -- for example, for retrieving the parent application
    context in a context hierarchy.
  • TestContext now delegates to CacheAwareContextLoaderDelegate for
    loading contexts.
  • Introduced getParentApplicationContext() in MergedContextConfiguration
  • The loadContext(MergedContextConfiguration) methods in
    AbstractGenericContextLoader and AbstractGenericWebContextLoader now
    set the parent context as appropriate.
  • Introduced 'hierarchyMode' attribute in @DirtiesContext with a
    corresponding HierarchyMode enum that defines EXHAUSTIVE and
    CURRENT_LEVEL cache removal modes.
  • ContextCache now internally tracks the relationships between contexts
    that make up a context hierarchy. Furthermore, when a context is
    removed, if it is part of a context hierarchy all corresponding
    contexts will be removed from the cache according to the supplied
  • AbstractGenericWebContextLoader will set a loaded context as the
    context hierarchies are used if the context has no parent or if the
    context has a parent that is not a WAC.
  • Where appropriate, updated Javadoc to refer to the
    ServletTestExecutionListener, which was introduced in 3.2.0.
  • Updated Javadoc to avoid and/or suppress warnings in spring-test.
  • Suppressed remaining warnings in code in spring-test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.