Skip to content

Commit 60fb3ef

Browse files
committed
[SPR-8240] Added new "Mixing XML resources and @configuration classes" section to the testing chapter.
1 parent 1aa1278 commit 60fb3ef

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

spring-framework-reference/src/testing.xml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@
437437
<emphasis>either</emphasis> the application context resource
438438
<literal>locations</literal> <emphasis>or</emphasis> the
439439
<interfacename>@Configuration</interfacename>
440-
<varname>classes</varname> to load as well as the
440+
<varname>classes</varname> (but not both) to load as well as the
441441
<interfacename>ContextLoader</interfacename> strategy to use for
442442
loading the context. Note, however, that you typically do not need
443443
to explicitly configure the loader since the default loader
@@ -1253,6 +1253,40 @@ public class OrderServiceTest {
12531253
}</programlisting>
12541254
</section>
12551255

1256+
<section id="testcontext-ctx-management-mixed-config">
1257+
<title>Mixing XML resources and @Configuration classes</title>
1258+
1259+
<para>It may sometimes be desirable to mix XML resources and
1260+
<interfacename>@Configuration</interfacename> classes to configure
1261+
an <interfacename>ApplicationContext</interfacename> for your tests.
1262+
For example, if you use XML configuration in production, you may
1263+
decide that you want to use
1264+
<interfacename>@Configuration</interfacename> classes to configure
1265+
specific Spring-managed components for your tests, or vice versa. As
1266+
mentioned in <xref
1267+
linkend="integration-testing-annotations-spring" /> the TestContext
1268+
framework does not allow you to declare <emphasis>both</emphasis>
1269+
via <interfacename>@ContextConfiguration</interfacename>, but this
1270+
does not mean that you cannot use both. If you want to use XML
1271+
<emphasis role="bold">and</emphasis>
1272+
<interfacename>@Configuration</interfacename> classes to configure
1273+
your tests, you will have to pick one as the <emphasis>entry
1274+
point</emphasis>, and that one will have to include or import the
1275+
other. For example, in XML you can include
1276+
<interfacename>@Configuration</interfacename> classes in component
1277+
scanning or define them as normal Spring beans; whereas, in a
1278+
<interfacename>@Configuration</interfacename> class you can use
1279+
<interfacename>@ImportResource</interfacename> to import XML
1280+
configuration files. Note that this behavior is semantically
1281+
equivalent to how you configure your application in production: in
1282+
production configuration you will define either a set of XML
1283+
resource locations or a set of
1284+
<interfacename>@Configuration</interfacename> classes that your
1285+
production <interfacename>ApplicationContext</interfacename> will
1286+
load, but you still have the freedom to include or import the other
1287+
type of configuration.</para>
1288+
</section>
1289+
12561290
<section id="testcontext-ctx-management-inheritance">
12571291
<title>Context configuration inheritance</title>
12581292

0 commit comments

Comments
 (0)