Skip to content

Commit 4e7098d

Browse files
committed
Document context hierarchy support in the TCF
This commit adds examples to the Javadoc for @ContextHierarchy and updates the Javadoc for @ContextConfiguration accordingly. Issue: SPR-10357
1 parent 3eb3610 commit 4e7098d

File tree

2 files changed

+111
-7
lines changed

2 files changed

+111
-7
lines changed

spring-test/src/main/java/org/springframework/test/context/ContextConfiguration.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,14 @@
287287
/**
288288
* The name of the context hierarchy level represented by this configuration.
289289
*
290-
* <p>If not specified the name will be inferred based on the numerical level within all
291-
* declared contexts within the hierarchy.
290+
* <p>If not specified the name will be inferred based on the numerical level
291+
* within all declared contexts within the hierarchy.
292292
*
293-
* <p>This attribute is only applicable when used within a test class hierarchy that is
294-
* configured using {@link ContextHierarchy @ContextHierarchy}, in which case the name
295-
* can be used for merging or overriding this configuration with configuration of the
296-
* same name in hierarchy levels defined in superclasses.
293+
* <p>This attribute is only applicable when used within a test class hierarchy
294+
* that is configured using {@code @ContextHierarchy}, in which case the name
295+
* can be used for <em>merging</em> or <em>overriding</em> this configuration
296+
* with configuration of the same name in hierarchy levels defined in superclasses.
297+
* See the Javadoc for {@link ContextHierarchy @ContextHierarchy} for details.
297298
*
298299
* @since 3.2.2
299300
*/

spring-test/src/main/java/org/springframework/test/context/ContextHierarchy.java

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,109 @@
2828
* a hierarchy of {@link org.springframework.context.ApplicationContext
2929
* ApplicationContexts} for integration tests.
3030
*
31+
* <h2>Examples</h2>
32+
* <p>The following JUnit-based examples demonstrate common configuration
33+
* scenarios for integration tests that require the use of context hierarchies.
34+
*
35+
* <h3>Single Test Class with Context Hierarchy</h3>
36+
* <p>{@code ControllerIntegrationTests} represents a typical integration testing
37+
* scenario for a Spring MVC web application by declaring a context hierarchy
38+
* consisting of two levels, one for the <em>root</em> {@code WebApplicationContext}
39+
* (with {@code TestAppConfig}) and one for the <em>dispatcher servlet</em>
40+
* {@code WebApplicationContext} (with {@code WebConfig}). The {@code
41+
* WebApplicationContext} that is <em>autowired</em> into the test instance is
42+
* the one for the child context (i.e., the lowest context in the hierarchy).
43+
*
44+
* <pre class="code">
45+
* &#064;RunWith(SpringJUnit4ClassRunner.class)
46+
* &#064;WebAppConfiguration
47+
* &#064;ContextHierarchy({
48+
* &#064;ContextConfiguration(classes = TestAppConfig.class),
49+
* &#064;ContextConfiguration(classes = WebConfig.class)
50+
* })
51+
* public class ControllerIntegrationTests {
52+
*
53+
* &#064;Autowired
54+
* private WebApplicationContext wac;
55+
*
56+
* // ...
57+
* }</pre>
58+
*
59+
* <h3>Class Hierarchy with Implicit Parent Context</h3>
60+
* <p>The following test classes define a context hierarchy within a test class
61+
* hierarchy. {@code AbstractWebTests} declares the configuration for a root
62+
* {@code WebApplicationContext} in a Spring-powered web application. Note,
63+
* however, that {@code AbstractWebTests} does not declare {@code @ContextHierarchy};
64+
* consequently, subclasses of {@code AbstractWebTests} can optionally participate
65+
* in a context hierarchy or follow the standard semantics for {@code @ContextConfiguration}.
66+
* {@code SoapWebServiceTests} and {@code RestWebServiceTests} both extend
67+
* {@code AbstractWebTests} and define a context hierarchy via {@code @ContextHierarchy}.
68+
* The result is that three application contexts will be loaded (one for each
69+
* declaration of {@code @ContextConfiguration}, and the application context
70+
* loaded based on the configuration in {@code AbstractWebTests} will be set as
71+
* the parent context for each of the contexts loaded for the concrete subclasses.
72+
*
73+
* <pre class="code">
74+
* &#064;RunWith(SpringJUnit4ClassRunner.class)
75+
* &#064;WebAppConfiguration
76+
* &#064;ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml")
77+
* public abstract class AbstractWebTests {}
78+
*
79+
* &#064;ContextHierarchy(&#064;ContextConfiguration("/spring/soap-ws-config.xml")
80+
* public class SoapWebServiceTests extends AbstractWebTests {}
81+
*
82+
* &#064;ContextHierarchy(&#064;ContextConfiguration("/spring/rest-ws-config.xml")
83+
* public class RestWebServiceTests extends AbstractWebTests {}</pre>
84+
*
85+
* <h3>Class Hierarchy with Merged Context Hierarchy Configuration</h3>
86+
* <p>The following classes demonstrate the use of <em>named</em> hierarchy levels
87+
* in order to <em>merge</em> the configuration for specific levels in a context
88+
* hierarchy. {@code BaseTests} defines two levels in the hierarchy, {@code parent}
89+
* and {@code child}. {@code ExtendedTests} extends {@code BaseTests} and instructs
90+
* the Spring TestContext Framework to merge the context configuration for the
91+
* {@code child} hierarchy level, simply by ensuring that the names declared via
92+
* {@link ContextConfiguration#name} are both {@code "child"}. The result is that
93+
* three application contexts will be loaded: one for {@code "/app-config.xml"},
94+
* one for {@code "/user-config.xml"}, and one for <code>{"/user-config.xml",
95+
* "/order-config.xml"}</code>. As with the previous example, the application
96+
* context loaded from {@code "/app-config.xml"} will be set as the parent context
97+
* for the contexts loaded from {@code "/user-config.xml"} and <code>{"/user-config.xml",
98+
* "/order-config.xml"}</code>.
99+
*
100+
* <pre class="code">
101+
* &#064;RunWith(SpringJUnit4ClassRunner.class)
102+
* &#064;ContextHierarchy({
103+
* &#064;ContextConfiguration(name = "parent", locations = "/app-config.xml"),
104+
* &#064;ContextConfiguration(name = "child", locations = "/user-config.xml")
105+
* })
106+
* public class BaseTests {}
107+
*
108+
* &#064;ContextHierarchy(
109+
* &#064;ContextConfiguration(name = "child", locations = "/order-config.xml")
110+
* )
111+
* public class ExtendedTests extends BaseTests {}</pre>
112+
*
113+
* <h3>Class Hierarchy with Overridden Context Hierarchy Configuration</h3>
114+
* <p>In contrast to the previous example, this example demonstrates how to
115+
* <em>override</em> the configuration for a given named level in a context hierarchy
116+
* by setting the {@link ContextConfiguration#inheritLocations} flag to {@code false}.
117+
* Consequently, the application context for {@code ExtendedTests} will be loaded
118+
* only from {@code "/test-user-config.xml"} and will have its parent set to the
119+
* context loaded from {@code "/app-config.xml"}.
120+
*
121+
* <pre class="code">
122+
* &#064;RunWith(SpringJUnit4ClassRunner.class)
123+
* &#064;ContextHierarchy({
124+
* &#064;ContextConfiguration(name = "parent", locations = "/app-config.xml"),
125+
* &#064;ContextConfiguration(name = "child", locations = "/user-config.xml")
126+
* })
127+
* public class BaseTests {}
128+
*
129+
* &#064;ContextHierarchy(
130+
* &#064;ContextConfiguration(name = "child", locations = "/test-user-config.xml", inheritLocations=false)
131+
* )
132+
* public class ExtendedTests extends BaseTests {}</pre>
133+
*
31134
* @author Sam Brannen
32135
* @since 3.2.2
33136
* @see ContextConfiguration
@@ -47,7 +150,7 @@
47150
* of the context hierarchy within a test class hierarchy, you must explicitly
48151
* name that level by supplying the same value to the {@link ContextConfiguration#name
49152
* name} attribute in {@code @ContextConfiguration} at each level in the
50-
* class hierarchy.
153+
* class hierarchy. See the class-level Javadoc for examples.
51154
*/
52155
ContextConfiguration[] value();
53156

0 commit comments

Comments
 (0)