Skip to content

Commit 455cb76

Browse files
committed
Further emphasize @⁠Configuration classes over XML and Groovy in testing chapter
See gh-36393 (cherry picked from commit 9eea227)
1 parent c77993a commit 455cb76

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ Dependency injection by using `@Autowired` is provided by the
9494
Test classes that use the TestContext framework do not need to extend any particular
9595
class or implement a specific interface to configure their application context. Instead,
9696
configuration is achieved by declaring the `@ContextConfiguration` annotation at the
97-
class level. If your test class does not explicitly declare application context resource
98-
locations or component classes, the configured `ContextLoader` determines how to load a
99-
context from a default location or default configuration classes. In addition to context
100-
resource locations and component classes, an application context can also be configured
101-
through xref:testing/testcontext-framework/ctx-management/context-customizers.adoc[context customizers]
97+
class level. If your test class does not explicitly declare component classes or resource
98+
locations, the configured `ContextLoader` determines how to load a context from _default_
99+
configuration classes or a _default_ location. In addition to component classes and
100+
context resource locations, an application context can also be configured through
101+
xref:testing/testcontext-framework/ctx-management/context-customizers.adoc[context customizers]
102102
or xref:testing/testcontext-framework/ctx-management/initializers.adoc[context initializers].
103103

104104
The following sections explain how to use `@ContextConfiguration` and related annotations
@@ -110,7 +110,7 @@ or context initializers. Alternatively, you can implement and configure your own
110110
* xref:testing/testcontext-framework/ctx-management/javaconfig.adoc[Context Configuration with Component Classes]
111111
* xref:testing/testcontext-framework/ctx-management/xml.adoc[Context Configuration with XML resources]
112112
* xref:testing/testcontext-framework/ctx-management/groovy.adoc[Context Configuration with Groovy Scripts]
113-
* xref:testing/testcontext-framework/ctx-management/mixed-config.adoc[Mixing XML, Groovy Scripts, and Component Classes]
113+
* xref:testing/testcontext-framework/ctx-management/mixed-config.adoc[Mixing Component Classes, XML, and Groovy Scripts]
114114
* xref:testing/testcontext-framework/ctx-management/context-customizers.adoc[Context Configuration with Context Customizers]
115115
* xref:testing/testcontext-framework/ctx-management/initializers.adoc[Context Configuration with Context Initializers]
116116
* xref:testing/testcontext-framework/ctx-management/inheritance.adoc[Context Configuration Inheritance]
Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
[[testcontext-ctx-management-mixed-config]]
2-
= Mixing XML, Groovy Scripts, and Component Classes
2+
= Mixing Component Classes, XML, and Groovy Scripts
33

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
88
specific Spring-managed components for your tests, or vice versa.
99

1010
Furthermore, some third-party frameworks (such as Spring Boot) provide first-class
1111
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
1818
`GenericGroovyXmlWebContextLoader` support both XML configuration files and Groovy
1919
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
2121
the standard testing support in the TestContext framework, you have the following options.
2222

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
2929
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

Comments
 (0)