|
28 | 28 | * a hierarchy of {@link org.springframework.context.ApplicationContext
|
29 | 29 | * ApplicationContexts} for integration tests.
|
30 | 30 | *
|
| 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 | + * @RunWith(SpringJUnit4ClassRunner.class) |
| 46 | + * @WebAppConfiguration |
| 47 | + * @ContextHierarchy({ |
| 48 | + * @ContextConfiguration(classes = TestAppConfig.class), |
| 49 | + * @ContextConfiguration(classes = WebConfig.class) |
| 50 | + * }) |
| 51 | + * public class ControllerIntegrationTests { |
| 52 | + * |
| 53 | + * @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 | + * @RunWith(SpringJUnit4ClassRunner.class) |
| 75 | + * @WebAppConfiguration |
| 76 | + * @ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml") |
| 77 | + * public abstract class AbstractWebTests {} |
| 78 | + * |
| 79 | + * @ContextHierarchy(@ContextConfiguration("/spring/soap-ws-config.xml") |
| 80 | + * public class SoapWebServiceTests extends AbstractWebTests {} |
| 81 | + * |
| 82 | + * @ContextHierarchy(@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 | + * @RunWith(SpringJUnit4ClassRunner.class) |
| 102 | + * @ContextHierarchy({ |
| 103 | + * @ContextConfiguration(name = "parent", locations = "/app-config.xml"), |
| 104 | + * @ContextConfiguration(name = "child", locations = "/user-config.xml") |
| 105 | + * }) |
| 106 | + * public class BaseTests {} |
| 107 | + * |
| 108 | + * @ContextHierarchy( |
| 109 | + * @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 | + * @RunWith(SpringJUnit4ClassRunner.class) |
| 123 | + * @ContextHierarchy({ |
| 124 | + * @ContextConfiguration(name = "parent", locations = "/app-config.xml"), |
| 125 | + * @ContextConfiguration(name = "child", locations = "/user-config.xml") |
| 126 | + * }) |
| 127 | + * public class BaseTests {} |
| 128 | + * |
| 129 | + * @ContextHierarchy( |
| 130 | + * @ContextConfiguration(name = "child", locations = "/test-user-config.xml", inheritLocations=false) |
| 131 | + * ) |
| 132 | + * public class ExtendedTests extends BaseTests {}</pre> |
| 133 | + * |
31 | 134 | * @author Sam Brannen
|
32 | 135 | * @since 3.2.2
|
33 | 136 | * @see ContextConfiguration
|
|
47 | 150 | * of the context hierarchy within a test class hierarchy, you must explicitly
|
48 | 151 | * name that level by supplying the same value to the {@link ContextConfiguration#name
|
49 | 152 | * name} attribute in {@code @ContextConfiguration} at each level in the
|
50 |
| - * class hierarchy. |
| 153 | + * class hierarchy. See the class-level Javadoc for examples. |
51 | 154 | */
|
52 | 155 | ContextConfiguration[] value();
|
53 | 156 |
|
|
0 commit comments