Browse Source

Update documentation of 'annotated class' support in the TCF

Updated the testing chapter of the reference manual to refer to 
'annotated classes' instead of 'configuration classes' where 
appropriate.

Also revised the content and examples in the @ContextConfiguration 
annotation section, mentioned Gradle, and reformatted the entire text.

Issue: SPR-9401
pull/82/head
Sam Brannen 14 years ago
parent
commit
504cdf498e
  1. 421
      src/reference/docbook/testing.xml

421
src/reference/docbook/testing.xml

@ -1,8 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<chapter xmlns="http://docbook.org/ns/docbook" version="5.0" <chapter version="5.0" xml:id="testing" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xi="http://www.w3.org/2001/XInclude"
xml:id="testing"> xmlns:ns5="http://www.w3.org/1999/xhtml"
xmlns:ns4="http://www.w3.org/2000/svg"
xmlns:ns3="http://www.w3.org/1998/Math/MathML"
xmlns:ns="http://docbook.org/ns/docbook">
<title>Testing</title> <title>Testing</title>
<section id="testing-introduction"> <section id="testing-introduction">
@ -128,8 +131,9 @@
<classname>ModelAndViewAssert</classname> combined with <classname>ModelAndViewAssert</classname> combined with
<literal>MockHttpServletRequest</literal>, <literal>MockHttpServletRequest</literal>,
<literal>MockHttpSession</literal>, and so on from the <link <literal>MockHttpSession</literal>, and so on from the <link
linkend="mock-objects-servlet"><literal>org.springframework.mock.web</literal></link> linkend="mock-objects-servlet">
package.</para> <literal>org.springframework.mock.web</literal>
</link> package.</para>
</tip> </tip>
</section> </section>
</section> </section>
@ -254,11 +258,11 @@
every test fixture leads to slower overall test runs that could reduce every test fixture leads to slower overall test runs that could reduce
productivity.</para> productivity.</para>
<para>Test classes can provide either an array containing the resource <para>Test classes can provide either an array of <emphasis>resource
locations of XML configuration metadata — typically in the classpath — locations</emphasis> for XML configuration metadata — typically in the
or an array containing <interfacename>@Configuration</interfacename> classpath — or an array of <emphasis>annotated classes</emphasis> that
classes that is used to configure the application. These locations or is used to configure the application. These locations or classes are
classes are the same as or similar to those specified in the same as or similar to those specified in
<literal>web.xml</literal> or other deployment configuration <literal>web.xml</literal> or other deployment configuration
files.</para> files.</para>
@ -267,8 +271,8 @@
test. Thus the setup cost is incurred only once (per test suite), and test. Thus the setup cost is incurred only once (per test suite), and
subsequent test execution is much faster. In this context, the term subsequent test execution is much faster. In this context, the term
<emphasis>test suite</emphasis> means all tests run in the same JVM — <emphasis>test suite</emphasis> means all tests run in the same JVM —
for example, all tests run from an Ant or Maven build for a given for example, all tests run from an Ant, Maven, or Gradle build for a
project or module. In the unlikely case that a test corrupts the given project or module. In the unlikely case that a test corrupts the
application context and requires reloading — for example, by modifying application context and requires reloading — for example, by modifying
a bean definition or the state of an application object — the a bean definition or the state of an application object — the
TestContext framework can be configured to reload the configuration TestContext framework can be configured to reload the configuration
@ -350,10 +354,11 @@
useful when you want a particular test to populate or modify the useful when you want a particular test to populate or modify the
database — the TestContext framework can be instructed to cause the database — the TestContext framework can be instructed to cause the
transaction to commit instead of roll back via the <link transaction to commit instead of roll back via the <link
linkend="integration-testing-annotations"><interfacename>@TransactionConfiguration</interfacename></link> linkend="integration-testing-annotations">
and <link <interfacename>@TransactionConfiguration</interfacename>
linkend="integration-testing-annotations"><interfacename>@Rollback</interfacename></link> </link> and <link linkend="integration-testing-annotations">
annotations.</para> <interfacename>@Rollback</interfacename>
</link> annotations.</para>
<para>See transaction management with the <link <para>See transaction management with the <link
linkend="testcontext-tx">TestContext framework</link>.</para> linkend="testcontext-tx">TestContext framework</link>.</para>
@ -405,10 +410,11 @@
contains <classname>SimpleJdbcTestUtils</classname>, which is a contains <classname>SimpleJdbcTestUtils</classname>, which is a
collection of JDBC related utility functions intended to simplify collection of JDBC related utility functions intended to simplify
standard database testing scenarios. <emphasis>Note that <link standard database testing scenarios. <emphasis>Note that <link
linkend="testcontext-support-classes-junit4"><classname>AbstractTransactionalJUnit4SpringContextTests</classname></link> linkend="testcontext-support-classes-junit4">
and <link <classname>AbstractTransactionalJUnit4SpringContextTests</classname>
linkend="testcontext-support-classes-testng"><classname>AbstractTransactionalTestNGSpringContextTests</classname></link> </link> and <link linkend="testcontext-support-classes-testng">
provide convenience methods which delegate to <classname>AbstractTransactionalTestNGSpringContextTests</classname>
</link> provide convenience methods which delegate to
<classname>SimpleJdbcTestUtils</classname> internally.</emphasis></para> <classname>SimpleJdbcTestUtils</classname> internally.</emphasis></para>
</section> </section>
@ -427,8 +433,11 @@
<itemizedlist> <itemizedlist>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@ContextConfiguration</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@ContextConfiguration</interfacename>
</emphasis>
</para>
<para>Defines class-level metadata that is used to determine how <para>Defines class-level metadata that is used to determine how
to load and configure an to load and configure an
@ -436,28 +445,46 @@
classes. Specifically, classes. Specifically,
<interfacename>@ContextConfiguration</interfacename> declares <interfacename>@ContextConfiguration</interfacename> declares
<emphasis>either</emphasis> the application context resource <emphasis>either</emphasis> the application context resource
<literal>locations</literal> <emphasis>or</emphasis> the <varname>locations</varname> <emphasis>or</emphasis> the annotated
<interfacename>@Configuration</interfacename> <varname>classes</varname> that will be used to load the
<varname>classes</varname> (but not both) to load as well as the context.</para>
<interfacename>ContextLoader</interfacename> strategy to use for
loading the context. Note, however, that you typically do not need <para>Resource locations are typically XML configuration files
to explicitly configure the loader since the default loader located in the classpath; whereas, annotated classes are typically
supports either resource <varname>locations</varname> or <interfacename>@Configuration</interfacename> classes. However,
configuration <varname>classes</varname>.</para> resource locations could also refer to files in the file system,
and annotated classes could be component classes, etc. See <link
linkend="testcontext-ctx-management-xml">Context configuration
with XML resources</link> and the Javadoc for
<interfacename>@ContextConfiguration</interfacename> for further
details.</para>
<programlisting language="java"><emphasis role="bold">@ContextConfiguration</emphasis>(<emphasis <programlisting language="java"><emphasis role="bold">@ContextConfiguration</emphasis>("/test-config.xml")
role="bold">locations</emphasis>="example/test-context.xml", <emphasis
role="bold">loader</emphasis>=CustomContextLoader.class)
public class XmlApplicationContextTests { public class XmlApplicationContextTests {
<lineannotation>// class body...</lineannotation> <lineannotation>// class body...</lineannotation>
}</programlisting> }</programlisting>
<programlisting language="java"><emphasis role="bold">@ContextConfiguration</emphasis>(<emphasis <programlisting language="java"><emphasis role="bold">@ContextConfiguration</emphasis>(<emphasis
role="bold">classes</emphasis>=MyConfig.class) role="bold">classes</emphasis>=TestConfig.class)
public class ConfigClassApplicationContextTests { public class ConfigClassApplicationContextTests {
<lineannotation>// class body...</lineannotation> <lineannotation>// class body...</lineannotation>
}</programlisting> }</programlisting>
<para><interfacename>@ContextConfiguration</interfacename> may
optionally be used to declare the
<interfacename>ContextLoader</interfacename> strategy as well.
Note, however, that you typically do not need to explicitly
configure the loader since the default loader supports either
resource <varname>locations</varname> or annotated
<varname>classes</varname>.</para>
<programlisting language="java"><emphasis role="bold">@ContextConfiguration</emphasis>(<emphasis
role="bold">locations</emphasis>="/test-context.xml", <emphasis
role="bold">loader</emphasis>=CustomContextLoader.class)
public class CustomLoaderXmlApplicationContextTests {
<lineannotation>// class body...</lineannotation>
}</programlisting>
<note> <note>
<para><interfacename>@ContextConfiguration</interfacename> <para><interfacename>@ContextConfiguration</interfacename>
provides support for <emphasis>inheriting</emphasis> resource provides support for <emphasis>inheriting</emphasis> resource
@ -471,8 +498,11 @@ public class ConfigClassApplicationContextTests {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@ActiveProfiles</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@ActiveProfiles</interfacename>
</emphasis>
</para>
<para>A class-level annotation that is used to declare which <para>A class-level annotation that is used to declare which
<emphasis>bean definition profiles</emphasis> should be active <emphasis>bean definition profiles</emphasis> should be active
@ -494,8 +524,7 @@ public class DeveloperIntegrationTests {
<note> <note>
<para><interfacename>@ActiveProfiles</interfacename> provides <para><interfacename>@ActiveProfiles</interfacename> provides
support for <emphasis>inheriting</emphasis> active bean support for <emphasis>inheriting</emphasis> active bean
definition profiles declared by superclasses classes by definition profiles declared by superclasses by default.</para>
default.</para>
</note> </note>
<para>See <link <para>See <link
@ -506,8 +535,11 @@ public class DeveloperIntegrationTests {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@DirtiesContext</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@DirtiesContext</interfacename>
</emphasis>
</para>
<para>Indicates that the underlying Spring <para>Indicates that the underlying Spring
<interfacename>ApplicationContext</interfacename> has been <interfacename>ApplicationContext</interfacename> has been
@ -575,8 +607,11 @@ public void testProcessWhichDirtiesAppCtx() {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@TestExecutionListeners</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@TestExecutionListeners</interfacename>
</emphasis>
</para>
<para>Defines class-level metadata for configuring which <para>Defines class-level metadata for configuring which
<interfacename>TestExecutionListener</interfacename>s should be <interfacename>TestExecutionListener</interfacename>s should be
@ -597,8 +632,11 @@ public class CustomTestExecutionListenerTests {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@TransactionConfiguration</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@TransactionConfiguration</interfacename>
</emphasis>
</para>
<para>Defines class-level metadata for configuring transactional <para>Defines class-level metadata for configuring transactional
tests. Specifically, the bean name of the tests. Specifically, the bean name of the
@ -634,8 +672,11 @@ public class CustomConfiguredTransactionalTests {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@Rollback</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@Rollback</interfacename>
</emphasis>
</para>
<para>Indicates whether the transaction for the annotated test <para>Indicates whether the transaction for the annotated test
method should be <emphasis>rolled back</emphasis> after the test method should be <emphasis>rolled back</emphasis> after the test
@ -652,8 +693,11 @@ public void testProcessWithoutRollback() {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@BeforeTransaction</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@BeforeTransaction</interfacename>
</emphasis>
</para>
<para>Indicates that the annotated <literal>public void</literal> <para>Indicates that the annotated <literal>public void</literal>
method should be executed <emphasis>before</emphasis> a method should be executed <emphasis>before</emphasis> a
@ -668,8 +712,11 @@ public void testProcessWithoutRollback() {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@AfterTransaction</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@AfterTransaction</interfacename>
</emphasis>
</para>
<para>Indicates that the annotated <literal>public void</literal> <para>Indicates that the annotated <literal>public void</literal>
method should be executed <emphasis>after</emphasis> a transaction method should be executed <emphasis>after</emphasis> a transaction
@ -684,8 +731,11 @@ public void testProcessWithoutRollback() {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@NotTransactional</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@NotTransactional</interfacename>
</emphasis>
</para>
<para>The presence of this annotation indicates that the annotated <para>The presence of this annotation indicates that the annotated
test method must <emphasis>not</emphasis> execute in a test method must <emphasis>not</emphasis> execute in a
@ -728,54 +778,70 @@ public void testProcessWithoutTransaction() {
<itemizedlist> <itemizedlist>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@Autowired</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@Autowired</interfacename>
</emphasis>
</para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@Qualifier</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@Qualifier</interfacename>
</emphasis>
</para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para><emphasis role="bold">
role="bold"><interfacename>@Resource</interfacename></emphasis> <interfacename>@Resource</interfacename>
(javax.annotation) <emphasis>if JSR-250 is </emphasis> (javax.annotation) <emphasis>if JSR-250 is
present</emphasis></para> present</emphasis></para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para><emphasis role="bold">
role="bold"><interfacename>@Inject</interfacename></emphasis> <interfacename>@Inject</interfacename>
(javax.inject) <emphasis>if JSR-330 is present</emphasis></para> </emphasis> (javax.inject) <emphasis>if JSR-330 is
present</emphasis></para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para><emphasis role="bold">
role="bold"><interfacename>@Named</interfacename></emphasis> <interfacename>@Named</interfacename>
(javax.inject) <emphasis>if JSR-330 is present</emphasis></para> </emphasis> (javax.inject) <emphasis>if JSR-330 is
present</emphasis></para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para><emphasis role="bold">
role="bold"><interfacename>@PersistenceContext</interfacename></emphasis> <interfacename>@PersistenceContext</interfacename>
(javax.persistence) <emphasis>if JPA is present</emphasis></para> </emphasis> (javax.persistence) <emphasis>if JPA is
present</emphasis></para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para><emphasis role="bold">
role="bold"><interfacename>@PersistenceUnit</interfacename></emphasis> <interfacename>@PersistenceUnit</interfacename>
(javax.persistence) <emphasis>if JPA is present</emphasis></para> </emphasis> (javax.persistence) <emphasis>if JPA is
present</emphasis></para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@Required</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@Required</interfacename>
</emphasis>
</para>
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@Transactional</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@Transactional</interfacename>
</emphasis>
</para>
</listitem> </listitem>
</itemizedlist> </itemizedlist>
</section> </section>
@ -791,8 +857,11 @@ public void testProcessWithoutTransaction() {
<itemizedlist> <itemizedlist>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@IfProfileValue</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@IfProfileValue</interfacename>
</emphasis>
</para>
<para>Indicates that the annotated test is enabled for a specific <para>Indicates that the annotated test is enabled for a specific
testing environment. If the configured testing environment. If the configured
@ -827,8 +896,11 @@ public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@ProfileValueSourceConfiguration</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@ProfileValueSourceConfiguration</interfacename>
</emphasis>
</para>
<para>Class-level annotation that specifies what type of <para>Class-level annotation that specifies what type of
<literal>ProfileValueSource</literal> to use when retrieving <literal>ProfileValueSource</literal> to use when retrieving
@ -846,8 +918,11 @@ public class CustomProfileValueSourceTests {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@Timed</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@Timed</interfacename>
</emphasis>
</para>
<para>Indicates that the annotated test method must finish <para>Indicates that the annotated test method must finish
execution in a specified time period (in milliseconds). If the execution in a specified time period (in milliseconds). If the
@ -881,8 +956,11 @@ public void testProcessWithOneSecondTimeout() {
</listitem> </listitem>
<listitem> <listitem>
<para><emphasis <para>
role="bold"><interfacename>@Repeat</interfacename></emphasis></para> <emphasis role="bold">
<interfacename>@Repeat</interfacename>
</emphasis>
</para>
<para>Indicates that the annotated test method must be executed <para>Indicates that the annotated test method must be executed
repeatedly. The number of times that the test method is to be repeatedly. The number of times that the test method is to be
@ -909,17 +987,16 @@ public void testProcessRepeatedly() {
Framework</emphasis> (located in the Framework</emphasis> (located in the
<literal>org.springframework.test.context</literal> package) provides <literal>org.springframework.test.context</literal> package) provides
generic, annotation-driven unit and integration testing support that is generic, annotation-driven unit and integration testing support that is
agnostic of the testing framework in use, whether JUnit or TestNG. The agnostic of the testing framework in use. The TestContext framework also
TestContext framework also places a great deal of importance on places a great deal of importance on <emphasis>convention over
<emphasis>convention over configuration</emphasis> with reasonable configuration</emphasis> with reasonable defaults that can be overridden
defaults that can be overridden through annotation-based through annotation-based configuration.</para>
configuration.</para>
<para>In addition to generic testing infrastructure, the TestContext <para>In addition to generic testing infrastructure, the TestContext
framework provides explicit support for JUnit and TestNG in the form of framework provides explicit support for JUnit and TestNG in the form of
<literal>abstract</literal> support classes. For JUnit, Spring also <literal>abstract</literal> support classes. For JUnit, Spring also
provides a custom JUnit <interfacename>Runner</interfacename> that provides a custom JUnit <interfacename>Runner</interfacename> that
allows one to write so called <emphasis>POJO test classes</emphasis>. allows one to write so-called <emphasis>POJO test classes</emphasis>.
POJO test classes are not required to extend a particular class POJO test classes are not required to extend a particular class
hierarchy.</para> hierarchy.</para>
@ -1037,8 +1114,8 @@ public void testProcessRepeatedly() {
<para>As of Spring 3.1, implement <para>As of Spring 3.1, implement
<interfacename>SmartContextLoader</interfacename> instead of this <interfacename>SmartContextLoader</interfacename> instead of this
interface in order to provide support for configuration classes interface in order to provide support for annotated classes and
and active bean definition profiles.</para> active bean definition profiles.</para>
</listitem> </listitem>
<listitem> <listitem>
@ -1050,8 +1127,8 @@ public void testProcessRepeatedly() {
supersedes the <interfacename>ContextLoader</interfacename> SPI supersedes the <interfacename>ContextLoader</interfacename> SPI
that was introduced in Spring 2.5. Specifically, a that was introduced in Spring 2.5. Specifically, a
<interfacename>SmartContextLoader</interfacename> can choose to <interfacename>SmartContextLoader</interfacename> can choose to
process either resource <varname>locations</varname> or process either resource <varname>locations</varname> or annotated
configuration <varname>classes</varname>. Furthermore, a <varname>classes</varname>. Furthermore, a
<interfacename>SmartContextLoader</interfacename> can set active <interfacename>SmartContextLoader</interfacename> can set active
bean definition profiles in the context that it loads.</para> bean definition profiles in the context that it loads.</para>
@ -1071,13 +1148,14 @@ public void testProcessRepeatedly() {
<listitem> <listitem>
<para><classname>AnnotationConfigContextLoader</classname>: <para><classname>AnnotationConfigContextLoader</classname>:
loads an application context from loads an application context from <emphasis>annotated
<interfacename>@Configuration</interfacename> classes.</para> classes</emphasis>.</para>
</listitem> </listitem>
<listitem> <listitem>
<para><classname>GenericXmlContextLoader</classname>: loads an <para><classname>GenericXmlContextLoader</classname>: loads an
application context from XML resource locations.</para> application context from XML <emphasis>resource
locations</emphasis>.</para>
</listitem> </listitem>
<listitem> <listitem>
@ -1144,16 +1222,16 @@ public class MyTest {
achieved simply by declaring the achieved simply by declaring the
<interfacename>@ContextConfiguration</interfacename> annotation at the <interfacename>@ContextConfiguration</interfacename> annotation at the
class level. If your test class does not explicitly declare class level. If your test class does not explicitly declare
application context resource <literal>locations</literal> or application context resource <literal>locations</literal> or annotated
configuration <varname>classes</varname>, the configured <varname>classes</varname>, the configured
<interfacename>ContextLoader</interfacename> determines how to load a <interfacename>ContextLoader</interfacename> determines how to load a
context from a default location or default configuration context from a default location or default configuration
classes.</para> classes.</para>
<para>The following sections explain how to configure an <para>The following sections explain how to configure an
<interfacename>ApplicationContext</interfacename> via XML <interfacename>ApplicationContext</interfacename> via XML
configuration files or <interfacename>@Configuration</interfacename> configuration files or annotated classes (typically
classes using Spring's <interfacename>@Configuration</interfacename> classes) using Spring's
<interfacename>@ContextConfiguration</interfacename> <interfacename>@ContextConfiguration</interfacename>
annotation.</para> annotation.</para>
@ -1224,15 +1302,15 @@ public class MyTest {
</section> </section>
<section id="testcontext-ctx-management-javaconfig"> <section id="testcontext-ctx-management-javaconfig">
<title>Context configuration with @Configuration classes</title> <title>Context configuration with annotated classes</title>
<para>To load an <interfacename>ApplicationContext</interfacename> <para>To load an <interfacename>ApplicationContext</interfacename>
for your tests using <interfacename>@Configuration</interfacename> for your tests using <emphasis>annotated classes</emphasis> (see
classes (see <xref linkend="beans-java" />), annotate your test <xref linkend="beans-java" />), annotate your test class with
class with <interfacename>@ContextConfiguration</interfacename> and <interfacename>@ContextConfiguration</interfacename> and configure
configure the <literal>classes</literal> attribute with an array the <literal>classes</literal> attribute with an array that contains
that contains references to configuration classes. Alternatively, references to annotated classes. Alternatively, you can implement
you can implement and configure your own custom and configure your own custom
<interfacename>ContextLoader</interfacename> or <interfacename>ContextLoader</interfacename> or
<interfacename>SmartContextLoader</interfacename> for advanced use <interfacename>SmartContextLoader</interfacename> for advanced use
cases.</para> cases.</para>
@ -1249,11 +1327,11 @@ public class MyTest {
TestContext framework will attempt to detect the presence of default TestContext framework will attempt to detect the presence of default
configuration classes. Specifically, configuration classes. Specifically,
<classname>AnnotationConfigContextLoader</classname> will detect all <classname>AnnotationConfigContextLoader</classname> will detect all
static inner classes of the annotated test class that meet the static inner classes of the test class that meet the requirements
requirements for configuration class implementations as specified in for configuration class implementations as specified in the Javadoc
the Javadoc for <interfacename>@Configuration</interfacename>. In for <interfacename>@Configuration</interfacename>. In the following
the following example, the <classname>OrderServiceTest</classname> example, the <classname>OrderServiceTest</classname> class declares
class declares a static inner configuration class named a static inner configuration class named
<classname>Config</classname> that will be automatically used to <classname>Config</classname> that will be automatically used to
load the <interfacename>ApplicationContext</interfacename> for the load the <interfacename>ApplicationContext</interfacename> for the
test class. Note that the name of the configuration class is test class. Note that the name of the configuration class is
@ -1291,10 +1369,11 @@ public class OrderServiceTest {
</section> </section>
<section id="testcontext-ctx-management-mixed-config"> <section id="testcontext-ctx-management-mixed-config">
<title>Mixing XML resources and @Configuration classes</title> <title>Mixing XML resources and annotated classes</title>
<para>It may sometimes be desirable to mix XML resources and <para>It may sometimes be desirable to mix XML resources and
<interfacename>@Configuration</interfacename> classes to configure annotated classes (i.e., typically
<interfacename>@Configuration</interfacename> classes) to configure
an <interfacename>ApplicationContext</interfacename> for your tests. an <interfacename>ApplicationContext</interfacename> for your tests.
For example, if you use XML configuration in production, you may For example, if you use XML configuration in production, you may
decide that you want to use decide that you want to use
@ -1330,22 +1409,21 @@ public class OrderServiceTest {
<para><interfacename>@ContextConfiguration</interfacename> supports <para><interfacename>@ContextConfiguration</interfacename> supports
a boolean <literal>inheritLocations</literal> attribute that denotes a boolean <literal>inheritLocations</literal> attribute that denotes
whether resource locations or configuration classes declared by whether resource locations or annotated classes declared by
superclasses should be <emphasis>inherited</emphasis>. The default superclasses should be <emphasis>inherited</emphasis>. The default
value is <literal>true</literal>. This means that an annotated class value is <literal>true</literal>. This means that a test class
inherits the resource locations or configuration classes declared by inherits the resource locations or annotated classes declared by any
any annotated superclasses. Specifically, the resource locations or superclasses. Specifically, the resource locations or annotated
configuration classes for an annotated test class are appended to classes for a test class are appended to the list of resource
the list of resource locations or configuration classes declared by locations or annotated classes declared by superclasses. Thus,
annotated superclasses. Thus, subclasses have the option of subclasses have the option of <emphasis>extending</emphasis> the
<emphasis>extending</emphasis> the list of resource locations or list of resource locations or annotated classes.</para>
configuration classes.</para>
<para>If <interfacename>@ContextConfiguration</interfacename>'s <para>If <interfacename>@ContextConfiguration</interfacename>'s
<literal>inheritLocations</literal> attribute is set to <literal>inheritLocations</literal> attribute is set to
<literal>false</literal>, the resource locations or configuration <literal>false</literal>, the resource locations or annotated
classes for the annotated class <emphasis>shadow</emphasis> and classes for the test class <emphasis>shadow</emphasis> and
effectively replace any resource locations or configuration classes effectively replace any resource locations or annotated classes
defined by superclasses.</para> defined by superclasses.</para>
<para>In the following example that uses XML resource locations, the <para>In the following example that uses XML resource locations, the
@ -1372,12 +1450,12 @@ public class ExtendedTest extends BaseTest {
<lineannotation>// class body...</lineannotation> <lineannotation>// class body...</lineannotation>
}</programlisting> }</programlisting>
<para>Similarly, in the following example that uses configuration <para>Similarly, in the following example that uses annotated
classes, the <interfacename>ApplicationContext</interfacename> for classes, the <interfacename>ApplicationContext</interfacename> for
<classname>ExtendedTest</classname> will be loaded from the <classname>ExtendedTest</classname> will be loaded from the
<classname>BaseConfig</classname> <emphasis <classname>BaseConfig</classname> <emphasis
role="bold">and</emphasis> <classname>ExtendedConfig</classname> role="bold">and</emphasis> <classname>ExtendedConfig</classname>
configuration classes, in that order. Beans defined in classes, in that order. Beans defined in
<classname>ExtendedConfig</classname> may therefore override (i.e., <classname>ExtendedConfig</classname> may therefore override (i.e.,
replace) those defined in <classname>BaseConfig</classname>.</para> replace) those defined in <classname>BaseConfig</classname>.</para>
@ -1403,7 +1481,7 @@ public class ExtendedTest extends BaseTest {
definition profiles</emphasis>), and integration tests can now be definition profiles</emphasis>), and integration tests can now be
configured to activate particular bean definition profiles for configured to activate particular bean definition profiles for
various testing scenarios. This is achieved by annotating a test various testing scenarios. This is achieved by annotating a test
class with the new <interfacename>@ActiveProfiles</interfacename> class with the <interfacename>@ActiveProfiles</interfacename>
annotation and supplying a list of profiles that should be activated annotation and supplying a list of profiles that should be activated
when loading the <interfacename>ApplicationContext</interfacename> when loading the <interfacename>ApplicationContext</interfacename>
for the test.</para> for the test.</para>
@ -1626,23 +1704,35 @@ public class TransferServiceTest {
<itemizedlist> <itemizedlist>
<listitem> <listitem>
<para><varname>locations</varname> <emphasis>(from <para>
@ContextConfiguration)</emphasis></para> <varname>locations</varname>
<emphasis>(from @ContextConfiguration)</emphasis>
</para>
</listitem> </listitem>
<listitem> <listitem>
<para><varname>classes</varname> <emphasis>(from <para>
@ContextConfiguration)</emphasis></para> <varname>classes</varname>
<emphasis>(from @ContextConfiguration)</emphasis>
</para>
</listitem> </listitem>
<listitem> <listitem>
<para><varname>contextLoader</varname> <emphasis>(from <para>
@ContextConfiguration)</emphasis></para> <varname>contextLoader</varname>
<emphasis>(from @ContextConfiguration)</emphasis>
</para>
</listitem> </listitem>
<listitem> <listitem>
<para><varname>activeProfiles</varname> <emphasis>(from <para>
@ActiveProfiles)</emphasis></para> <varname>activeProfiles</varname>
<emphasis>(from @ActiveProfiles)</emphasis>
</para>
</listitem> </listitem>
</itemizedlist> </itemizedlist>
@ -1677,9 +1767,9 @@ public class TransferServiceTest {
<para>To benefit from the caching mechanism, all tests must run <para>To benefit from the caching mechanism, all tests must run
within the same process or test suite. This can be achieved by within the same process or test suite. This can be achieved by
executing all tests as a group within an IDE. Similarly, when executing all tests as a group within an IDE. Similarly, when
executing tests with a build framework such as Ant or Maven it is executing tests with a build framework such as Ant, Maven, or
important to make sure that the build framework does not Gradle it is important to make sure that the build framework does
<emphasis>fork</emphasis> between tests. For example, if the not <emphasis>fork</emphasis> between tests. For example, if the
<ulink <ulink
url="http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#forkMode">forkMode</ulink> url="http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#forkMode">forkMode</ulink>
for the Maven Surefire plug-in is set to <literal>always</literal> for the Maven Surefire plug-in is set to <literal>always</literal>
@ -1729,13 +1819,13 @@ public class TransferServiceTest {
</tip> </tip>
<para>Because <interfacename>@Autowired</interfacename> is used to <para>Because <interfacename>@Autowired</interfacename> is used to
perform <link linkend="beans-factory-autowire"><emphasis>autowiring by perform <link linkend="beans-factory-autowire">
type</emphasis></link>, if you have multiple bean definitions of the <emphasis>autowiring by type</emphasis>
same type, you cannot rely on this approach for those particular </link>, if you have multiple bean definitions of the same type, you
beans. In that case, you can use cannot rely on this approach for those particular beans. In that case,
<interfacename>@Autowired</interfacename> in conjunction with you can use <interfacename>@Autowired</interfacename> in conjunction
<interfacename>@Qualifier</interfacename>. As of Spring 3.0 you may with <interfacename>@Qualifier</interfacename>. As of Spring 3.0 you
also choose to use <interfacename>@Inject</interfacename> in may also choose to use <interfacename>@Inject</interfacename> in
conjunction with <interfacename>@Named</interfacename>. Alternatively, conjunction with <interfacename>@Named</interfacename>. Alternatively,
if your test class has access to its if your test class has access to its
<classname>ApplicationContext</classname>, you can perform an explicit <classname>ApplicationContext</classname>, you can perform an explicit
@ -1904,12 +1994,14 @@ public class HibernateTitleRepositoryTests {
the <interfacename>@Rollback</interfacename> annotation to override the <interfacename>@Rollback</interfacename> annotation to override
the class-level default rollback setting.</para> the class-level default rollback setting.</para>
<para><emphasis><link <para>
linkend="testcontext-support-classes-junit4"><classname>AbstractTransactionalJUnit4SpringContextTests</classname></link> <emphasis><link linkend="testcontext-support-classes-junit4">
and <link <classname>AbstractTransactionalJUnit4SpringContextTests</classname>
linkend="testcontext-support-classes-testng"><classname>AbstractTransactionalTestNGSpringContextTests</classname></link> </link> and <link linkend="testcontext-support-classes-testng">
are preconfigured for transactional support at the class level. <classname>AbstractTransactionalTestNGSpringContextTests</classname>
</emphasis></para> </link> are preconfigured for transactional support at the class
level.</emphasis>
</para>
<para>Occasionally you need to execute certain code before or after a <para>Occasionally you need to execute certain code before or after a
transactional test method but outside the transactional context, for transactional test method but outside the transactional context, for
@ -2363,10 +2455,10 @@ public class HibernateClinicTests extends AbstractClinicTests { }
<itemizedlist> <itemizedlist>
<listitem> <listitem>
<para><ulink url="http://www.junit.org/">JUnit</ulink>: <para><ulink url="http://www.junit.org/">JUnit</ulink>: <quote>
<quote><emphasis>A programmer-oriented testing framework for <emphasis>A programmer-oriented testing framework for
Java</emphasis></quote>. Used by the Spring Framework in its test Java</emphasis>
suite.</para> </quote>. Used by the Spring Framework in its test suite.</para>
</listitem> </listitem>
<listitem> <listitem>
@ -2389,10 +2481,11 @@ public class HibernateClinicTests extends AbstractClinicTests { }
<listitem> <listitem>
<para><ulink url="http://www.easymock.org/">EasyMock</ulink>: Java <para><ulink url="http://www.easymock.org/">EasyMock</ulink>: Java
library <quote><emphasis>that provides Mock Objects for interfaces library <quote>
(and objects through the class extension) by generating them on the <emphasis>that provides Mock Objects for interfaces (and objects
fly using Java's proxy mechanism.</emphasis></quote> Used by the through the class extension) by generating them on the fly using
Spring Framework in its test suite.</para> Java's proxy mechanism.</emphasis>
</quote> Used by the Spring Framework in its test suite.</para>
</listitem> </listitem>
<listitem> <listitem>
@ -2416,8 +2509,8 @@ public class HibernateClinicTests extends AbstractClinicTests { }
</listitem> </listitem>
<listitem> <listitem>
<para><ulink url="http://grinder.sourceforge.net/">The Grinder</ulink>: <para><ulink url="http://grinder.sourceforge.net/">The
Java load testing framework.</para> Grinder</ulink>: Java load testing framework.</para>
</listitem> </listitem>
</itemizedlist> </itemizedlist>
</section> </section>

Loading…
Cancel
Save