[SPR-8240] Restructuring annotation content.
This commit is contained in:
parent
ac4cd8c37b
commit
e7ce297cf3
|
@ -414,433 +414,454 @@
|
||||||
<section id="integration-testing-annotations">
|
<section id="integration-testing-annotations">
|
||||||
<title>Annotations</title>
|
<title>Annotations</title>
|
||||||
|
|
||||||
<para>The Spring Framework provides the following set of
|
<section id="integration-testing-annotations-spring">
|
||||||
<emphasis>Spring-specific</emphasis> annotations that you can use in
|
<title>Spring Testing Annotations</title>
|
||||||
your unit and integration tests in conjunction with the TestContext
|
|
||||||
framework. Refer to the respective Javadoc for further information,
|
|
||||||
including default attribute values, attribute aliases, and so on.</para>
|
|
||||||
|
|
||||||
<itemizedlist>
|
<para>The Spring Framework provides the following set of
|
||||||
<listitem>
|
<emphasis>Spring-specific</emphasis> annotations that you can use in
|
||||||
<para><emphasis
|
your unit and integration tests in conjunction with the TestContext
|
||||||
role="bold"><interfacename>@ContextConfiguration</interfacename></emphasis></para>
|
framework. Refer to the respective Javadoc for further information,
|
||||||
|
including default attribute values, attribute aliases, and so
|
||||||
|
on.</para>
|
||||||
|
|
||||||
<para>Defines class-level metadata that is used to determine how to
|
<itemizedlist>
|
||||||
load and configure an
|
<listitem>
|
||||||
<interfacename>ApplicationContext</interfacename> for test classes.
|
<para><emphasis
|
||||||
Specifically, <interfacename>@ContextConfiguration</interfacename>
|
role="bold"><interfacename>@ContextConfiguration</interfacename></emphasis></para>
|
||||||
declares <emphasis>either</emphasis> the application context
|
|
||||||
resource <literal>locations</literal> <emphasis>or</emphasis> the
|
|
||||||
<interfacename>@Configuration</interfacename>
|
|
||||||
<varname>classes</varname> to load as well as the
|
|
||||||
<interfacename>ContextLoader</interfacename> strategy to use for
|
|
||||||
loading the context. Note, however, that you typically do not need
|
|
||||||
to explicitly configure the loader since the default loader supports
|
|
||||||
either resource <varname>locations</varname> or configuration
|
|
||||||
<varname>classes</varname>.</para>
|
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@ContextConfiguration</emphasis>(<emphasis
|
<para>Defines class-level metadata that is used to determine how
|
||||||
role="bold">locations</emphasis>="example/test-context.xml", <emphasis
|
to load and configure an
|
||||||
role="bold">loader</emphasis>=CustomContextLoader.class)
|
<interfacename>ApplicationContext</interfacename> for test
|
||||||
|
classes. Specifically,
|
||||||
|
<interfacename>@ContextConfiguration</interfacename> declares
|
||||||
|
<emphasis>either</emphasis> the application context resource
|
||||||
|
<literal>locations</literal> <emphasis>or</emphasis> the
|
||||||
|
<interfacename>@Configuration</interfacename>
|
||||||
|
<varname>classes</varname> to load as well as the
|
||||||
|
<interfacename>ContextLoader</interfacename> strategy to use for
|
||||||
|
loading the context. Note, however, that you typically do not need
|
||||||
|
to explicitly configure the loader since the default loader
|
||||||
|
supports either resource <varname>locations</varname> or
|
||||||
|
configuration <varname>classes</varname>.</para>
|
||||||
|
|
||||||
|
<programlisting language="java"><emphasis role="bold">@ContextConfiguration</emphasis>(<emphasis
|
||||||
|
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>=MyConfig.class)
|
||||||
public class ConfigClassApplicationContextTests {
|
public class ConfigClassApplicationContextTests {
|
||||||
<lineannotation>// class body...</lineannotation>
|
<lineannotation>// class body...</lineannotation>
|
||||||
}</programlisting>
|
}</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
|
||||||
locations or configuration classes by default.</para>
|
locations or configuration classes by default.</para>
|
||||||
</note>
|
</note>
|
||||||
|
|
||||||
<para>See <link linkend="testcontext-ctx-management">Context
|
<para>See <link linkend="testcontext-ctx-management">Context
|
||||||
management and caching</link> and Javadoc for examples and further
|
management and caching</link> and Javadoc for examples and further
|
||||||
details.</para>
|
details.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@DirtiesContext</interfacename></emphasis></para>
|
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
|
||||||
<emphasis>dirtied</emphasis> (i.e., modified or corrupted in some
|
<emphasis>dirtied</emphasis> (i.e., modified or corrupted in some
|
||||||
manner) during the execution of a test and should be closed,
|
manner) during the execution of a test and should be closed,
|
||||||
regardless of whether the test passed.
|
regardless of whether the test passed.
|
||||||
<interfacename>@DirtiesContext</interfacename> is supported in the
|
<interfacename>@DirtiesContext</interfacename> is supported in the
|
||||||
following scenarios:</para>
|
following scenarios:</para>
|
||||||
|
|
||||||
<itemizedlist>
|
<itemizedlist>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>After the current test class, when declared on a class
|
<para>After the current test class, when declared on a class
|
||||||
with class mode set to <literal>AFTER_CLASS</literal>, which is
|
with class mode set to <literal>AFTER_CLASS</literal>, which
|
||||||
the default class mode.</para>
|
is the default class mode.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>After each test method in the current test class, when
|
<para>After each test method in the current test class, when
|
||||||
declared on a class with class mode set to
|
declared on a class with class mode set to
|
||||||
<literal>AFTER_EACH_TEST_METHOD.</literal></para>
|
<literal>AFTER_EACH_TEST_METHOD.</literal></para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>After the current test, when declared on a method.</para>
|
<para>After the current test, when declared on a
|
||||||
</listitem>
|
method.</para>
|
||||||
</itemizedlist>
|
</listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
|
||||||
<para>Use this annotation if a test has modified the context (for
|
<para>Use this annotation if a test has modified the context (for
|
||||||
example, by replacing a bean definition). Subsequent tests are
|
example, by replacing a bean definition). Subsequent tests are
|
||||||
supplied a new context.</para>
|
supplied a new context.</para>
|
||||||
|
|
||||||
<para>With JUnit 4.5+ or TestNG you can use
|
<para>With JUnit 4.5+ or TestNG you can use
|
||||||
<interfacename>@DirtiesContext</interfacename> as both a class-level
|
<interfacename>@DirtiesContext</interfacename> as both a
|
||||||
and method-level annotation within the same test class. In such
|
class-level and method-level annotation within the same test
|
||||||
scenarios, the <interfacename>ApplicationContext</interfacename> is
|
class. In such scenarios, the
|
||||||
marked as <emphasis>dirty</emphasis> after any such annotated method
|
<interfacename>ApplicationContext</interfacename> is marked as
|
||||||
as well as after the entire class. If the
|
<emphasis>dirty</emphasis> after any such annotated method as well
|
||||||
<classname>ClassMode</classname> is set to
|
as after the entire class. If the <classname>ClassMode</classname>
|
||||||
<literal>AFTER_EACH_TEST_METHOD</literal>, the context is marked
|
is set to <literal>AFTER_EACH_TEST_METHOD</literal>, the context
|
||||||
dirty after each test method in the class.</para>
|
is marked dirty after each test method in the class.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@DirtiesContext</emphasis>
|
<programlisting language="java"><emphasis role="bold">@DirtiesContext</emphasis>
|
||||||
public class ContextDirtyingTests {
|
public class ContextDirtyingTests {
|
||||||
<lineannotation>// some tests that result in the Spring container being dirtied</lineannotation>
|
<lineannotation>// some tests that result in the Spring container being dirtied</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@DirtiesContext</emphasis>(<emphasis
|
<programlisting language="java"><emphasis role="bold">@DirtiesContext</emphasis>(<emphasis
|
||||||
role="bold">classMode</emphasis> = ClassMode.AFTER_EACH_TEST_METHOD)
|
role="bold">classMode</emphasis> = ClassMode.AFTER_EACH_TEST_METHOD)
|
||||||
public class ContextDirtyingTests {
|
public class ContextDirtyingTests {
|
||||||
<lineannotation>// some tests that result in the Spring container being dirtied</lineannotation>
|
<lineannotation>// some tests that result in the Spring container being dirtied</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@DirtiesContext</emphasis>
|
<programlisting language="java"><emphasis role="bold">@DirtiesContext</emphasis>
|
||||||
@Test
|
@Test
|
||||||
public void testProcessWhichDirtiesAppCtx() {
|
public void testProcessWhichDirtiesAppCtx() {
|
||||||
<lineannotation>// some logic that results in the Spring container being dirtied</lineannotation>
|
<lineannotation>// some logic that results in the Spring container being dirtied</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<para>When an application context is marked
|
<para>When an application context is marked
|
||||||
<emphasis>dirty</emphasis>, it is removed from the testing
|
<emphasis>dirty</emphasis>, it is removed from the testing
|
||||||
framework's cache and closed; thus the underlying Spring container
|
framework's cache and closed; thus the underlying Spring container
|
||||||
is rebuilt for any subsequent test that requires a context with the
|
is rebuilt for any subsequent test that requires a context with
|
||||||
same set of resource locations.</para>
|
the same set of resource locations.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@TestExecutionListeners</interfacename></emphasis></para>
|
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
|
||||||
registered with the <classname>TestContextManager</classname>.
|
registered with the <classname>TestContextManager</classname>.
|
||||||
Typically, <interfacename>@TestExecutionListeners</interfacename> is
|
Typically, <interfacename>@TestExecutionListeners</interfacename>
|
||||||
used in conjunction with
|
is used in conjunction with
|
||||||
<interfacename>@ContextConfiguration</interfacename>.</para>
|
<interfacename>@ContextConfiguration</interfacename>.</para>
|
||||||
|
|
||||||
<programlisting language="java">@ContextConfiguration
|
<programlisting language="java">@ContextConfiguration
|
||||||
<emphasis role="bold">@TestExecutionListeners</emphasis>({CustomTestExecutionListener.class, AnotherTestExecutionListener.class})
|
<emphasis role="bold">@TestExecutionListeners</emphasis>({CustomTestExecutionListener.class, AnotherTestExecutionListener.class})
|
||||||
public class CustomTestExecutionListenerTests {
|
public class CustomTestExecutionListenerTests {
|
||||||
<lineannotation>// class body...</lineannotation>
|
<lineannotation>// class body...</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<para><interfacename>@TestExecutionListeners</interfacename>
|
<para><interfacename>@TestExecutionListeners</interfacename>
|
||||||
supports <emphasis>inherited</emphasis> listeners by default. See
|
supports <emphasis>inherited</emphasis> listeners by default. See
|
||||||
the Javadoc for an example and further details.</para>
|
the Javadoc for an example and further details.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@TransactionConfiguration</interfacename></emphasis></para>
|
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
|
||||||
<interfacename>PlatformTransactionManager</interfacename> that is to
|
<interfacename>PlatformTransactionManager</interfacename> that is
|
||||||
be used to drive transactions can be explicitly configured if the
|
to be used to drive transactions can be explicitly configured if
|
||||||
bean name of the desired
|
the bean name of the desired
|
||||||
<interfacename>PlatformTransactionManager</interfacename> is not
|
<interfacename>PlatformTransactionManager</interfacename> is not
|
||||||
"transactionManager". In addition, you can change the
|
"transactionManager". In addition, you can change the
|
||||||
<literal>defaultRollback</literal> flag to <literal>false</literal>.
|
<literal>defaultRollback</literal> flag to
|
||||||
Typically, <interfacename>@TransactionConfiguration</interfacename>
|
<literal>false</literal>. Typically,
|
||||||
is used in conjunction with
|
<interfacename>@TransactionConfiguration</interfacename> is used
|
||||||
<interfacename>@ContextConfiguration</interfacename>.</para>
|
in conjunction with
|
||||||
|
<interfacename>@ContextConfiguration</interfacename>.</para>
|
||||||
|
|
||||||
<programlisting language="java">@ContextConfiguration
|
<programlisting language="java">@ContextConfiguration
|
||||||
<emphasis role="bold">@TransactionConfiguration</emphasis>(<emphasis
|
<emphasis role="bold">@TransactionConfiguration</emphasis>(<emphasis
|
||||||
role="bold">transactionManager</emphasis>="txMgr", <emphasis
|
role="bold">transactionManager</emphasis>="txMgr", <emphasis
|
||||||
role="bold">defaultRollback</emphasis>=false)
|
role="bold">defaultRollback</emphasis>=false)
|
||||||
public class CustomConfiguredTransactionalTests {
|
public class CustomConfiguredTransactionalTests {
|
||||||
<lineannotation>// class body...</lineannotation>
|
<lineannotation>// class body...</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<note>
|
<note>
|
||||||
<para>If the default conventions are sufficient for your test
|
<para>If the default conventions are sufficient for your test
|
||||||
configuration, you can avoid using
|
configuration, you can avoid using
|
||||||
<interfacename>@TransactionConfiguration</interfacename>
|
<interfacename>@TransactionConfiguration</interfacename>
|
||||||
altogether. In other words, if your transaction manager bean is
|
altogether. In other words, if your transaction manager bean is
|
||||||
named "transactionManager" and if you want transactions to roll
|
named "transactionManager" and if you want transactions to roll
|
||||||
back automatically, there is no need to annotate your test class
|
back automatically, there is no need to annotate your test class
|
||||||
with
|
with
|
||||||
<interfacename>@TransactionConfiguration</interfacename>.</para>
|
<interfacename>@TransactionConfiguration</interfacename>.</para>
|
||||||
</note>
|
</note>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@Rollback</interfacename></emphasis></para>
|
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
|
||||||
method has completed. If <literal>true</literal>, the transaction is
|
method has completed. If <literal>true</literal>, the transaction
|
||||||
rolled back; otherwise, the transaction is committed. Use
|
is rolled back; otherwise, the transaction is committed. Use
|
||||||
<interfacename>@Rollback</interfacename> to override the default
|
<interfacename>@Rollback</interfacename> to override the default
|
||||||
rollback flag configured at the class level.</para>
|
rollback flag configured at the class level.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@Rollback</emphasis>(false)
|
<programlisting language="java"><emphasis role="bold">@Rollback</emphasis>(false)
|
||||||
@Test
|
@Test
|
||||||
public void testProcessWithoutRollback() {
|
public void testProcessWithoutRollback() {
|
||||||
<lineannotation>// ...</lineannotation>
|
<lineannotation>// ...</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@BeforeTransaction</interfacename></emphasis></para>
|
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 transaction
|
method should be executed <emphasis>before</emphasis> a
|
||||||
is started for test methods configured to run within a transaction
|
transaction is started for test methods configured to run within a
|
||||||
via the <interfacename>@Transactional</interfacename>
|
transaction via the <interfacename>@Transactional</interfacename>
|
||||||
annotation.</para>
|
annotation.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@BeforeTransaction
|
<programlisting language="java"><emphasis role="bold">@BeforeTransaction
|
||||||
</emphasis>public void beforeTransaction() {
|
</emphasis>public void beforeTransaction() {
|
||||||
<lineannotation>// logic to be executed before a transaction is started</lineannotation>
|
<lineannotation>// logic to be executed before a transaction is started</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@AfterTransaction</interfacename></emphasis></para>
|
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
|
||||||
has ended for test methods configured to run within a transaction
|
has ended for test methods configured to run within a transaction
|
||||||
via the <interfacename>@Transactional</interfacename>
|
via the <interfacename>@Transactional</interfacename>
|
||||||
annotation.</para>
|
annotation.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@AfterTransaction
|
<programlisting language="java"><emphasis role="bold">@AfterTransaction
|
||||||
</emphasis>public void afterTransaction() {
|
</emphasis>public void afterTransaction() {
|
||||||
<lineannotation>// logic to be executed after a transaction has ended</lineannotation>
|
<lineannotation>// logic to be executed after a transaction has ended</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@NotTransactional</interfacename></emphasis></para>
|
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 transactional
|
test method must <emphasis>not</emphasis> execute in a
|
||||||
context.</para>
|
transactional context.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@NotTransactional</emphasis>
|
<programlisting language="java"><emphasis role="bold">@NotTransactional</emphasis>
|
||||||
@Test
|
@Test
|
||||||
public void testProcessWithoutTransaction() {
|
public void testProcessWithoutTransaction() {
|
||||||
<lineannotation>// ...</lineannotation>
|
<lineannotation>// ...</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<warning>
|
<warning>
|
||||||
<title>@NotTransactional is deprecated</title>
|
<title>@NotTransactional is deprecated</title>
|
||||||
|
|
||||||
<para>As of Spring 3.0,
|
<para>As of Spring 3.0,
|
||||||
<interfacename>@NotTransactional</interfacename> is deprecated in
|
<interfacename>@NotTransactional</interfacename> is deprecated
|
||||||
favor of moving the <emphasis>non-transactional</emphasis> test
|
in favor of moving the <emphasis>non-transactional</emphasis>
|
||||||
method to a separate (non-transactional) test class or to a
|
test method to a separate (non-transactional) test class or to a
|
||||||
<interfacename>@BeforeTransaction</interfacename> or
|
<interfacename>@BeforeTransaction</interfacename> or
|
||||||
<interfacename>@AfterTransaction</interfacename> method. As an
|
<interfacename>@AfterTransaction</interfacename> method. As an
|
||||||
alternative to annotating an entire class with
|
alternative to annotating an entire class with
|
||||||
<interfacename>@Transactional</interfacename>, consider annotating
|
<interfacename>@Transactional</interfacename>, consider
|
||||||
individual methods with
|
annotating individual methods with
|
||||||
<interfacename>@Transactional</interfacename>; doing so allows a
|
<interfacename>@Transactional</interfacename>; doing so allows a
|
||||||
mix of transactional and non-transactional methods in the same
|
mix of transactional and non-transactional methods in the same
|
||||||
test class without the need for using
|
test class without the need for using
|
||||||
<interfacename>@NotTransactional</interfacename>.</para>
|
<interfacename>@NotTransactional</interfacename>.</para>
|
||||||
</warning>
|
</warning>
|
||||||
</listitem>
|
</listitem>
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
|
</section>
|
||||||
|
|
||||||
<para>The following annotations are <emphasis>only</emphasis> supported
|
<section id="integration-testing-annotations-standard">
|
||||||
when used in conjunction with the <link
|
<title>Standard Annotation Support</title>
|
||||||
linkend="testcontext-junit4-runner">SpringJUnit4ClassRunner</link> or
|
|
||||||
the <link linkend="testcontext-support-classes-junit4">JUnit</link>
|
|
||||||
support classes.</para>
|
|
||||||
|
|
||||||
<itemizedlist>
|
<para>The following non-test-specific annotations are supported with
|
||||||
<listitem>
|
standard semantics for all configurations of the Spring TestContext
|
||||||
<para><emphasis
|
Framework.</para>
|
||||||
role="bold"><interfacename>@IfProfileValue</interfacename></emphasis></para>
|
|
||||||
|
|
||||||
<para>Indicates that the annotated test is enabled for a specific
|
<itemizedlist>
|
||||||
testing environment. If the configured
|
<listitem>
|
||||||
<classname>ProfileValueSource</classname> returns a matching
|
<para><emphasis
|
||||||
<literal>value</literal> for the provided <literal>name</literal>,
|
role="bold"><interfacename>@Autowired</interfacename></emphasis></para>
|
||||||
the test is enabled. This annotation can be applied to an entire
|
</listitem>
|
||||||
class or to individual methods. Class-level usage overrides
|
|
||||||
method-level usage.</para>
|
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@IfProfileValue</emphasis>(<emphasis
|
<listitem>
|
||||||
role="bold">name</emphasis>="java.vendor", <emphasis role="bold">value</emphasis>="Sun Microsystems Inc.")
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@Qualifier</interfacename></emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@Resource</interfacename></emphasis>
|
||||||
|
(javax.annotation) <emphasis>if JSR-250 is
|
||||||
|
present</emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@Inject</interfacename></emphasis>
|
||||||
|
(javax.inject) <emphasis>if JSR-330 is present</emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@Named</interfacename></emphasis>
|
||||||
|
(javax.inject) <emphasis>if JSR-330 is present</emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@PersistenceContext</interfacename></emphasis>
|
||||||
|
(javax.persistence) <emphasis>if JPA is present</emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@PersistenceUnit</interfacename></emphasis>
|
||||||
|
(javax.persistence) <emphasis>if JPA is present</emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@Required</interfacename></emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@Transactional</interfacename></emphasis></para>
|
||||||
|
</listitem>
|
||||||
|
</itemizedlist>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="integration-testing-annotations-junit">
|
||||||
|
<title>Spring JUnit Testing Annotations</title>
|
||||||
|
|
||||||
|
<para>The following annotations are <emphasis>only</emphasis>
|
||||||
|
supported when used in conjunction with the <link
|
||||||
|
linkend="testcontext-junit4-runner">SpringJUnit4ClassRunner</link> or
|
||||||
|
the <link linkend="testcontext-support-classes-junit4">JUnit</link>
|
||||||
|
support classes.</para>
|
||||||
|
|
||||||
|
<itemizedlist>
|
||||||
|
<listitem>
|
||||||
|
<para><emphasis
|
||||||
|
role="bold"><interfacename>@IfProfileValue</interfacename></emphasis></para>
|
||||||
|
|
||||||
|
<para>Indicates that the annotated test is enabled for a specific
|
||||||
|
testing environment. If the configured
|
||||||
|
<classname>ProfileValueSource</classname> returns a matching
|
||||||
|
<literal>value</literal> for the provided <literal>name</literal>,
|
||||||
|
the test is enabled. This annotation can be applied to an entire
|
||||||
|
class or to individual methods. Class-level usage overrides
|
||||||
|
method-level usage.</para>
|
||||||
|
|
||||||
|
<programlisting language="java"><emphasis role="bold">@IfProfileValue</emphasis>(<emphasis
|
||||||
|
role="bold">name</emphasis>="java.vendor", <emphasis
|
||||||
|
role="bold">value</emphasis>="Sun Microsystems Inc.")
|
||||||
@Test
|
@Test
|
||||||
public void testProcessWhichRunsOnlyOnSunJvm() {
|
public void testProcessWhichRunsOnlyOnSunJvm() {
|
||||||
<lineannotation>// some logic that should run only on Java VMs from Sun Microsystems</lineannotation>
|
<lineannotation>// some logic that should run only on Java VMs from Sun Microsystems</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<para>Alternatively, you can configure
|
<para>Alternatively, you can configure
|
||||||
<interfacename>@IfProfileValue</interfacename> with a list of
|
<interfacename>@IfProfileValue</interfacename> with a list of
|
||||||
<literal>values</literal> (with <emphasis>OR</emphasis> semantics)
|
<literal>values</literal> (with <emphasis>OR</emphasis> semantics)
|
||||||
to achieve TestNG-like support for <emphasis>test groups</emphasis>
|
to achieve TestNG-like support for <emphasis>test
|
||||||
in a JUnit environment. Consider the following example:</para>
|
groups</emphasis> in a JUnit environment. Consider the following
|
||||||
|
example:</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@IfProfileValue</emphasis>(<emphasis
|
<programlisting language="java"><emphasis role="bold">@IfProfileValue</emphasis>(<emphasis
|
||||||
role="bold">name</emphasis>="test-groups", <emphasis role="bold">values</emphasis>={"unit-tests", "integration-tests"})
|
role="bold">name</emphasis>="test-groups", <emphasis
|
||||||
|
role="bold">values</emphasis>={"unit-tests", "integration-tests"})
|
||||||
@Test
|
@Test
|
||||||
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
|
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
|
||||||
<lineannotation>// some logic that should run only for unit and integration test groups</lineannotation>
|
<lineannotation>// some logic that should run only for unit and integration test groups</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@ProfileValueSourceConfiguration</interfacename></emphasis></para>
|
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
|
||||||
<emphasis>profile values</emphasis> configured through the
|
<emphasis>profile values</emphasis> configured through the
|
||||||
<interfacename>@IfProfileValue</interfacename> annotation. If
|
<interfacename>@IfProfileValue</interfacename> annotation. If
|
||||||
<interfacename>@ProfileValueSourceConfiguration</interfacename> is
|
<interfacename>@ProfileValueSourceConfiguration</interfacename> is
|
||||||
not declared for a test,
|
not declared for a test,
|
||||||
<classname>SystemProfileValueSource</classname> is used by
|
<classname>SystemProfileValueSource</classname> is used by
|
||||||
default.</para>
|
default.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@ProfileValueSourceConfiguration</emphasis>(CustomProfileValueSource.class)
|
<programlisting language="java"><emphasis role="bold">@ProfileValueSourceConfiguration</emphasis>(CustomProfileValueSource.class)
|
||||||
public class CustomProfileValueSourceTests {
|
public class CustomProfileValueSourceTests {
|
||||||
<lineannotation>// class body...</lineannotation>
|
<lineannotation>// class body...</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@Timed</interfacename></emphasis></para>
|
role="bold"><interfacename>@Timed</interfacename></emphasis></para>
|
||||||
|
|
||||||
<para>Indicates that the annotated test method must finish execution
|
<para>Indicates that the annotated test method must finish
|
||||||
in a specified time period (in milliseconds). If the text execution
|
execution in a specified time period (in milliseconds). If the
|
||||||
time exceeds the specified time period, the test fails.</para>
|
text execution time exceeds the specified time period, the test
|
||||||
|
fails.</para>
|
||||||
|
|
||||||
<para>The time period includes execution of the test method itself,
|
<para>The time period includes execution of the test method
|
||||||
any repetitions of the test (see
|
itself, any repetitions of the test (see
|
||||||
<interfacename>@Repeat</interfacename>), as well as any
|
<interfacename>@Repeat</interfacename>), as well as any
|
||||||
<emphasis>set up</emphasis> or <emphasis>tear down</emphasis> of the
|
<emphasis>set up</emphasis> or <emphasis>tear down</emphasis> of
|
||||||
test fixture.</para>
|
the test fixture.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@Timed</emphasis>(millis=1000)
|
<programlisting language="java"><emphasis role="bold">@Timed</emphasis>(millis=1000)
|
||||||
public void testProcessWithOneSecondTimeout() {
|
public void testProcessWithOneSecondTimeout() {
|
||||||
<lineannotation>// some logic that should not take longer than 1 second to execute</lineannotation>
|
<lineannotation>// some logic that should not take longer than 1 second to execute</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
|
|
||||||
<para>Spring's <interfacename>@Timed</interfacename> annotation has
|
<para>Spring's <interfacename>@Timed</interfacename> annotation
|
||||||
different semantics than JUnit's
|
has different semantics than JUnit's
|
||||||
<interfacename>@Test(timeout=...)</interfacename> support.
|
<interfacename>@Test(timeout=...)</interfacename> support.
|
||||||
Specifically, due to the manner in which JUnit handles test
|
Specifically, due to the manner in which JUnit handles test
|
||||||
execution timeouts (that is, by executing the test method in a
|
execution timeouts (that is, by executing the test method in a
|
||||||
separate <classname>Thread</classname>),
|
separate <classname>Thread</classname>),
|
||||||
<interfacename>@Test(timeout=...)</interfacename> applies to
|
<interfacename>@Test(timeout=...)</interfacename> applies to
|
||||||
<emphasis>each iteration</emphasis> in the case of repetitions and
|
<emphasis>each iteration</emphasis> in the case of repetitions and
|
||||||
preemptively fails the test if the test takes too long. Spring's
|
preemptively fails the test if the test takes too long. Spring's
|
||||||
<interfacename>@Timed</interfacename>, on the other hand, times the
|
<interfacename>@Timed</interfacename>, on the other hand, times
|
||||||
<emphasis>total</emphasis> test execution time (including all
|
the <emphasis>total</emphasis> test execution time (including all
|
||||||
repetitions) and does not preemptively fail the test but rather
|
repetitions) and does not preemptively fail the test but rather
|
||||||
waits for the test to complete before failing.</para>
|
waits for the test to complete before failing.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
|
||||||
<listitem>
|
<listitem>
|
||||||
<para><emphasis
|
<para><emphasis
|
||||||
role="bold"><interfacename>@Repeat</interfacename></emphasis></para>
|
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
|
||||||
executed is specified in the annotation.</para>
|
executed is specified in the annotation.</para>
|
||||||
|
|
||||||
<para>The scope of execution to be repeated includes execution of
|
<para>The scope of execution to be repeated includes execution of
|
||||||
the test method itself as well as any <emphasis>set up</emphasis> or
|
the test method itself as well as any <emphasis>set up</emphasis>
|
||||||
<emphasis>tear down</emphasis> of the test fixture.</para>
|
or <emphasis>tear down</emphasis> of the test fixture.</para>
|
||||||
|
|
||||||
<programlisting language="java"><emphasis role="bold">@Repeat</emphasis>(10)
|
<programlisting language="java"><emphasis role="bold">@Repeat</emphasis>(10)
|
||||||
@Test
|
@Test
|
||||||
public void testProcessRepeatedly() {
|
public void testProcessRepeatedly() {
|
||||||
<lineannotation>// ...</lineannotation>
|
<lineannotation>// ...</lineannotation>
|
||||||
}</programlisting>
|
}</programlisting>
|
||||||
</listitem>
|
</listitem>
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
|
</section>
|
||||||
<para>The following non-test-specific annotations are supported with
|
|
||||||
standard semantics for all configurations of the Spring TestContext
|
|
||||||
Framework.</para>
|
|
||||||
|
|
||||||
<itemizedlist>
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@Autowired</interfacename></emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@Qualifier</interfacename></emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@Resource</interfacename></emphasis>
|
|
||||||
(javax.annotation) <emphasis>if JSR-250 is present</emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@Inject</interfacename></emphasis>
|
|
||||||
(javax.inject) <emphasis>if JSR-330 is present</emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@Named</interfacename></emphasis>
|
|
||||||
(javax.inject) <emphasis>if JSR-330 is present</emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@PersistenceContext</interfacename></emphasis>
|
|
||||||
(javax.persistence) <emphasis>if JPA is present</emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@PersistenceUnit</interfacename></emphasis>
|
|
||||||
(javax.persistence) <emphasis>if JPA is present</emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@Required</interfacename></emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para><emphasis
|
|
||||||
role="bold"><interfacename>@Transactional</interfacename></emphasis></para>
|
|
||||||
</listitem>
|
|
||||||
</itemizedlist>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section id="testcontext-framework">
|
<section id="testcontext-framework">
|
||||||
|
|
Loading…
Reference in New Issue