|
1 | 1 | [[testcontext-ctx-management-mixed-config]] |
2 | | -= Mixing XML, Groovy Scripts, and Component Classes |
| 2 | += Mixing Component Classes, XML, and Groovy Scripts |
3 | 3 |
|
4 | | -It may sometimes be desirable to mix XML configuration files, Groovy scripts, and |
5 | | -component classes (typically `@Configuration` classes) to configure an |
6 | | -`ApplicationContext` for your tests. For example, if you use XML configuration in |
7 | | -production, you may decide that you want to use `@Configuration` classes to configure |
| 4 | +It may sometimes be desirable to mix component classes (typically `@Configuration` |
| 5 | +classes), XML configuration files, or Groovy scripts to configure an `ApplicationContext` |
| 6 | +for your tests. For example, if you use XML configuration in production for legacy |
| 7 | +reasons, you may decide that you want to use `@Configuration` classes to configure |
8 | 8 | specific Spring-managed components for your tests, or vice versa. |
9 | 9 |
|
10 | 10 | Furthermore, some third-party frameworks (such as Spring Boot) provide first-class |
11 | 11 | support for loading an `ApplicationContext` from different types of resources |
12 | | -simultaneously (for example, XML configuration files, Groovy scripts, and |
13 | | -`@Configuration` classes). The Spring Framework, historically, has not supported this for |
14 | | -standard deployments. Consequently, most of the `SmartContextLoader` implementations that |
15 | | -the Spring Framework delivers in the `spring-test` module support only one resource type |
16 | | -for each test context. However, this does not mean that you cannot use both. One |
17 | | -exception to the general rule is that the `GenericGroovyXmlContextLoader` and |
| 12 | +simultaneously (for example, `@Configuration` classes, XML configuration files, and |
| 13 | +Groovy scripts). The Spring Framework, historically, has not supported this for standard |
| 14 | +deployments. Consequently, most of the `SmartContextLoader` implementations that the |
| 15 | +Spring Framework delivers in the `spring-test` module support only one resource type for |
| 16 | +each test context. However, this does not mean that you cannot use a mixture of resource |
| 17 | +types. One exception to the general rule is that the `GenericGroovyXmlContextLoader` and |
18 | 18 | `GenericGroovyXmlWebContextLoader` support both XML configuration files and Groovy |
19 | 19 | scripts simultaneously. Furthermore, third-party frameworks may choose to support the |
20 | | -declaration of both `locations` and `classes` through `@ContextConfiguration`, and, with |
| 20 | +declaration of both `classes` and `locations` through `@ContextConfiguration`, and, with |
21 | 21 | the standard testing support in the TestContext framework, you have the following options. |
22 | 22 |
|
23 | | -If you want to use resource locations (for example, XML or Groovy) and `@Configuration` |
24 | | -classes to configure your tests, you must pick one as the entry point, and that one must |
25 | | -include or import the other. For example, in XML or Groovy scripts, you can include |
26 | | -`@Configuration` classes by using component scanning or defining them as normal Spring |
27 | | -beans, whereas, in a `@Configuration` class, you can use `@ImportResource` to import XML |
28 | | -configuration files or Groovy scripts. Note that this behavior is semantically equivalent |
| 23 | +If you want to use `@Configuration` classes and resource locations (for example, XML or |
| 24 | +Groovy) to configure your tests, you must pick one as the entry point, and that one must |
| 25 | +import or include the other. For example, in a `@Configuration` class, you can use |
| 26 | +`@ImportResource` to import XML configuration files or Groovy scripts; whereas, in XML or |
| 27 | +Groovy scripts, you can include `@Configuration` classes by using component scanning or |
| 28 | +defining them as normal Spring beans. Note that this behavior is semantically equivalent |
29 | 29 | to how you configure your application in production: In production configuration, you |
30 | | -define either a set of XML or Groovy resource locations or a set of `@Configuration` |
31 | | -classes from which your production `ApplicationContext` is loaded, but you still have the |
32 | | -freedom to include or import the other type of configuration. |
33 | | - |
| 30 | +define either a set of `@Configuration` classes or a set of XML or Groovy resource |
| 31 | +locations from which your production `ApplicationContext` is loaded, but you still have |
| 32 | +the freedom to import or include the other type of configuration. |
0 commit comments