Skip to content

Commit 62c991f

Browse files
committed
[SPR-5145] Updated reference manual regarding upgrade to JUnit 4.5; additional improvements in the testing chapter as well.
1 parent 1785891 commit 62c991f

File tree

1 file changed

+74
-56
lines changed

1 file changed

+74
-56
lines changed

spring-framework-reference/src/testing.xml

+74-56
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<para>One of the main benefits of Dependency Injection is that your code
2424
should really depend far less on the container than in traditional J2EE
25-
development. The POJOs that comprise your application should be testable
25+
development. The POJOs that make up your application should be testable
2626
in JUnit or TestNG tests, with objects simply instantiated using the
2727
<literal>new</literal> operator, <emphasis>without Spring or any other
2828
container</emphasis>. You can use <link linkend="mock-objects">mock
@@ -181,7 +181,7 @@
181181
linkend="testcontext-framework">Spring TestContext Framework</link>,
182182
which is agnostic of the actual testing framework in use, thus allowing
183183
instrumentation of tests in various environments including JUnit 3.8,
184-
JUnit 4.4, TestNG, etc. <emphasis>Note that the Spring TestContext
184+
JUnit 4.5, TestNG, etc. <emphasis>Note that the Spring TestContext
185185
Framework requires Java 5+.</emphasis></para>
186186
</section>
187187

@@ -443,7 +443,7 @@
443443
simplify standard database testing scenarios. <emphasis>Note that <link
444444
linkend="testcontext-support-classes-junit38"><classname>AbstractTransactionalJUnit38SpringContextTests</classname></link>,
445445
<link
446-
linkend="testcontext-support-classes-junit44"><classname>AbstractTransactionalJUnit4SpringContextTests</classname></link>,
446+
linkend="testcontext-support-classes-junit45"><classname>AbstractTransactionalJUnit4SpringContextTests</classname></link>,
447447
and <link
448448
linkend="testcontext-support-classes-testng"><classname>AbstractTransactionalTestNGSpringContextTests</classname></link>
449449
provide convenience methods which delegate to
@@ -636,7 +636,7 @@ public void testProcessWithoutTransaction() {
636636
linkend="testcontext-junit4-runner">SpringJUnit4ClassRunner</link>
637637
or the <link linkend="testcontext-support-classes-junit38">JUnit
638638
3.8</link> and <link
639-
linkend="testcontext-support-classes-junit44">JUnit 4.4</link> base
639+
linkend="testcontext-support-classes-junit45">JUnit 4.5</link> base
640640
test classes). Refer to the documentation in the
641641
<emphasis>TestContext Framework</emphasis> section for further
642642
details.</para>
@@ -647,7 +647,7 @@ public void testProcessWithoutTransaction() {
647647
<section id="junit38-legacy-support">
648648
<title>JUnit 3.8 legacy support</title>
649649

650-
<para>Spring's JUnit 3.8 legacy support is comprised of the classes
650+
<para>Spring's JUnit 3.8 legacy support consists of the classes
651651
found in the <literal>org.springframework.test</literal> package. This
652652
package provides valuable JUnit <classname>TestCase</classname>
653653
superclasses which can be extended for out-of-container integration
@@ -967,15 +967,15 @@ public void testProcessWithoutTransaction() {
967967
<literal>org.springframework.test.context</literal> package) provides
968968
generic, annotation-driven unit and integration testing support that is
969969
agnostic of the testing framework in use, for example JUnit 3.8, JUnit
970-
4.4, TestNG 5.5, etc. The TestContext framework also places a great deal
970+
4.5, TestNG 5.8, etc. The TestContext framework also places a great deal
971971
of importance on <emphasis>convention over configuration</emphasis> with
972972
reasonable defaults that can be overridden via annotation-based
973973
configuration.</para>
974974

975975
<para>In addition to generic testing infrastructure, the TestContext
976-
framework provides explicit support for JUnit 3.8, JUnit 4.4, and TestNG
977-
5.5 in the form of <literal>abstract</literal> support classes. For
978-
JUnit 4.4, the framework also provides a custom
976+
framework provides explicit support for JUnit 3.8, JUnit 4.5, and TestNG
977+
5.8 in the form of <literal>abstract</literal> support classes. For
978+
JUnit 4.5, the framework also provides a custom
979979
<interfacename>Runner</interfacename> which allows one to write test
980980
classes that are not required to extend a particular class
981981
hierarchy.</para>
@@ -1041,9 +1041,7 @@ public void testProcessWithoutTransaction() {
10411041

10421042
<para>Spring provides three
10431043
<interfacename>TestExecutionListener</interfacename>
1044-
implementations which are configured by default (via the
1045-
<interfacename>@TestExecutionListeners</interfacename>
1046-
annotation):
1044+
implementations which are configured by default:
10471045
<classname>DependencyInjectionTestExecutionListener</classname>,
10481046
<classname>DirtiesContextTestExecutionListener</classname>, and
10491047
<classname>TransactionalTestExecutionListener</classname>, which
@@ -1081,6 +1079,25 @@ public void testProcessWithoutTransaction() {
10811079
implement <interfacename>ApplicationContextAware</interfacename> and
10821080
therefore provide this functionality out-of-the-box.</para>
10831081

1082+
<tip>
1083+
<title>@Autowired ApplicationContext</title>
1084+
<para>
1085+
As an alternative to implementing the
1086+
<interfacename>ApplicationContextAware</interfacename> interface,
1087+
your test class can have its application context injected via the
1088+
<interfacename>@Autowired</interfacename> annotation on either a
1089+
field or setter method, for example:
1090+
</para>
1091+
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
1092+
@ContextConfiguration
1093+
public class MyTest {
1094+
<emphasis role="bold">@Autowired</emphasis>
1095+
private ApplicationContext applicationContext;
1096+
1097+
<lineannotation>// class body...</lineannotation>
1098+
}</programlisting>
1099+
</tip>
1100+
10841101
<para>In contrast to the JUnit 3.8 legacy support, test classes which
10851102
use the TestContext framework do not need to override any
10861103
<literal>protected</literal> instance methods to configure their
@@ -1196,10 +1213,10 @@ public class ExtendedTest extends BaseTest {
11961213
<interfacename>@ContextConfiguration</interfacename> by Setter
11971214
Injection, Field Injection, or both, depending on which annotations
11981215
you choose and whether you place them on setter methods or fields. For
1199-
consistency with annotation support in Spring 2.5, you may choose
1200-
either Spring's <interfacename>@Autowired</interfacename> annotation
1201-
or the <interfacename>@Resource</interfacename> annotation from JSR
1202-
250. The semantics for both are consistent throughout the Spring
1216+
consistency with the annotation support introduced in Spring 2.5, you
1217+
may choose either Spring's <interfacename>@Autowired</interfacename>
1218+
annotation or the <interfacename>@Resource</interfacename> annotation
1219+
from JSR 250. The semantics for both are consistent throughout the Spring
12031220
Framework. For example, if you prefer <link
12041221
linkend="beans-factory-autowire"><emphasis>autowiring by
12051222
type</emphasis></link>, annotate your setter methods or fields with
@@ -1221,11 +1238,13 @@ public class ExtendedTest extends BaseTest {
12211238
same type, you cannot rely on this approach for those particular
12221239
beans. In that case, you can use
12231240
<interfacename>@Resource</interfacename> for injection <emphasis>by
1224-
name</emphasis>. Alternatively, if your test class implements
1225-
<classname>ApplicationContextAware</classname>, you can directly
1226-
access the <classname>ApplicationContext</classname> supplied to your
1227-
test and perform an explicit lookup using (for example) a call to
1228-
<methodname>applicationContext.getBean("titleDao")</methodname>.</para>
1241+
name</emphasis>. Alternatively, if your test class has access to its
1242+
<classname>ApplicationContext</classname>, you can perform an explicit
1243+
lookup using (for example) a call to
1244+
<methodname>applicationContext.getBean("titleDao")</methodname>. A
1245+
third option is to use <interfacename>@Autowired</interfacename>
1246+
in conjunction with <interfacename>@Qualifier</interfacename>.
1247+
</para>
12291248

12301249
<para>If you don't want dependency injection applied to your test
12311250
instances, simply don't annotate any fields or setter methods with
@@ -1240,12 +1259,12 @@ public class ExtendedTest extends BaseTest {
12401259
<para>Consider the scenario where we have a class,
12411260
<classname>HibernateTitleDao</classname> (as outlined in the <link
12421261
linkend="testing-fixture-di">common goals</link> section). First,
1243-
let's look at a JUnit 4.4 based implementation of the test class
1262+
let's look at a JUnit 4.5 based implementation of the test class
12441263
itself which uses <interfacename>@Autowired</interfacename> for field
12451264
injection (we will look at the application context configuration after
12461265
all sample code listings). <emphasis>Note: The dependency injection
12471266
behavior in the following code listings is not in any way specific to
1248-
JUnit 4.4. The same DI techniques can be used in conjunction with any
1267+
JUnit 4.5. The same DI techniques can be used in conjunction with any
12491268
testing framework.</emphasis></para>
12501269

12511270
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
@@ -1441,7 +1460,7 @@ public final class HibernateTitleDaoTests {
14411460
<para><emphasis>Note that <link
14421461
linkend="testcontext-support-classes-junit38"><classname>AbstractTransactionalJUnit38SpringContextTests</classname></link>,
14431462
<link
1444-
linkend="testcontext-support-classes-junit44"><classname>AbstractTransactionalJUnit4SpringContextTests</classname></link>,
1463+
linkend="testcontext-support-classes-junit45"><classname>AbstractTransactionalJUnit4SpringContextTests</classname></link>,
14451464
and <link
14461465
linkend="testcontext-support-classes-testng"><classname>AbstractTransactionalTestNGSpringContextTests</classname></link>
14471466
are pre-configured for transactional support at the class level.
@@ -1595,11 +1614,11 @@ public class FictitiousTransactionalTest {
15951614
</itemizedlist>
15961615
</section>
15971616

1598-
<section id="testcontext-support-classes-junit44">
1599-
<title>JUnit 4.4 support classes</title>
1617+
<section id="testcontext-support-classes-junit45">
1618+
<title>JUnit 4.5 support classes</title>
16001619

16011620
<para>The <literal>org.springframework.test.context.junit4</literal>
1602-
package provides support classes for JUnit 4.4 based test
1621+
package provides support classes for JUnit 4.5 based test
16031622
cases.</para>
16041623

16051624
<itemizedlist>
@@ -1609,7 +1628,7 @@ public class FictitiousTransactionalTest {
16091628
<para>Abstract base test class which integrates the
16101629
<emphasis>Spring TestContext Framework</emphasis> with explicit
16111630
<classname>ApplicationContext</classname> testing support in a
1612-
JUnit 4.4 environment.</para>
1631+
JUnit 4.5 environment.</para>
16131632

16141633
<para>When you extend
16151634
<classname>AbstractJUnit4SpringContextTests</classname> you will
@@ -1679,13 +1698,13 @@ public class FictitiousTransactionalTest {
16791698
</section>
16801699

16811700
<section id="testcontext-junit4-runner">
1682-
<title>Custom JUnit 4.4 Runner</title>
1701+
<title>Custom JUnit 4.5 Runner</title>
16831702

16841703
<para>The <emphasis>Spring TestContext Framework</emphasis> offers
1685-
full integration with JUnit 4.4 via a custom runner. By annotating
1704+
full integration with JUnit 4.5 via a custom runner. By annotating
16861705
test classes with
16871706
<literal>@Runwith(SpringJUnit4ClassRunner.class)</literal>,
1688-
developers can implement standard JUnit 4.4 unit and integration
1707+
developers can implement standard JUnit 4.5 unit and integration
16891708
tests and simultaneously reap the benefits of the TestContext
16901709
framework such as support for loading application contexts,
16911710
dependency injection of test instances, transactional test method
@@ -1810,7 +1829,7 @@ public class SimpleTest {
18101829
linkend="testcontext-junit4-runner">SpringJUnit4ClassRunner</link> or
18111830
the <link linkend="testcontext-support-classes-junit38">JUnit
18121831
3.8</link> and <link
1813-
linkend="testcontext-support-classes-junit44">JUnit 4.4</link> support
1832+
linkend="testcontext-support-classes-junit45">JUnit 4.5</link> support
18141833
classes.</emphasis></para>
18151834

18161835
<itemizedlist>
@@ -2017,7 +2036,7 @@ public void afterTransaction() {
20172036

20182037
<para>The PetClinic sample application included with the full Spring
20192038
distribution illustrates several features of the <emphasis>Spring
2020-
TestContext Framework</emphasis> in a JUnit 4.4 environment. Most test
2039+
TestContext Framework</emphasis> in a JUnit 4.5 environment. Most test
20212040
functionality is included in the
20222041
<classname>AbstractClinicTests</classname>, for which a partial listing
20232042
is shown below:</para>
@@ -2149,57 +2168,56 @@ public class HibernateClinicTests extends AbstractClinicTests { }
21492168
<section id="testing-resources">
21502169
<title>Further Resources</title>
21512170

2152-
<para>This section contains links to further resources about testing in
2153-
general.</para>
2171+
<para>This section contains links to further resources about testing in general.</para>
21542172

21552173
<itemizedlist>
21562174
<listitem>
2157-
<para>The <ulink url="http://www.junit.org/">JUnit homepage</ulink>.
2158-
The Spring Framework's unit test suite is written using JUnit 3.8 as
2159-
the testing framework.</para>
2175+
<para><ulink url="http://www.junit.org/">JUnit</ulink>:
2176+
the Spring Framework's unit and integration test suite is written using
2177+
JUnit 3.8 and JUnit 4.5 as the testing framework.</para>
21602178
</listitem>
21612179

21622180
<listitem>
2163-
<para>The <ulink url="http://testng.org/">TestNG homepage</ulink>.
2164-
TestNG is a testing framework inspired by JUnit 3.8 with added support
2181+
<para><ulink url="http://testng.org/">TestNG</ulink>:
2182+
a testing framework inspired by JUnit 3.8 with added support
21652183
for Java 5 annotations, test groups, data-driven testing, distributed
21662184
testing, etc.</para>
21672185
</listitem>
21682186

21692187
<listitem>
2170-
<para>The <ulink url="http://www.mockobjects.com/">Mock Objects
2171-
homepage</ulink>. About Mock Objects, a technique for improving the
2172-
design of code within Test-Driven Development.</para>
2188+
<para><ulink url="http://www.mockobjects.com/">MockObjects.com</ulink>:
2189+
a website dedicated to mock objects, a technique for improving the design
2190+
of code within Test-Driven Development.</para>
21732191
</listitem>
21742192

21752193
<listitem>
2176-
<para><ulink url="http://en.wikipedia.org/wiki/Mock_Object"> "Mock
2177-
Objects" article at Wikipedia</ulink>.</para>
2194+
<para><ulink url="http://en.wikipedia.org/wiki/Mock_Object">"Mock Objects"</ulink>:
2195+
article at Wikipedia.</para>
21782196
</listitem>
21792197

21802198
<listitem>
2181-
<para>The <ulink url="http://www.easymock.org/">EasyMock
2182-
homepage</ulink>. The Spring Framework uses EasyMock extensively in
2183-
its test suite.</para>
2199+
<para><ulink url="http://www.easymock.org/">EasyMock</ulink>:
2200+
the Spring Framework uses EasyMock extensively in its test suite.</para>
21842201
</listitem>
21852202

21862203
<listitem>
2187-
<para>The <ulink url="http://www.jmock.org/">JMock homepage</ulink>.
2188-
JMock is a library that supports test-driven development of Java code
2204+
<para><ulink url="http://www.jmock.org/">JMock</ulink>:
2205+
a library that supports test-driven development of Java code
21892206
with mock objects.</para>
21902207
</listitem>
21912208

21922209
<listitem>
2193-
<para>The <ulink url="http://dbunit.sourceforge.net/">DbUnit
2194-
homepage</ulink>. DbUnit is a JUnit extension (also usable with Ant)
2195-
targeted for database-driven projects that, among other things, puts
2196-
your database into a known state between test runs.</para>
2210+
<para><ulink url="http://dbunit.sourceforge.net/">DbUnit</ulink>:
2211+
a JUnit extension (also usable with Ant and Maven) targeted for database-driven
2212+
projects that, among other things, puts your database into a known state
2213+
between test runs.</para>
21972214
</listitem>
21982215

21992216
<listitem>
2200-
<para>The <ulink url="http://grinder.sourceforge.net/">Grinder
2201-
homepage</ulink>. The Grinder is a Java load-testing framework.</para>
2217+
<para><ulink url="http://grinder.sourceforge.net/">Grinder</ulink>:
2218+
a Java load testing framework.</para>
22022219
</listitem>
22032220
</itemizedlist>
2221+
22042222
</section>
22052223
</chapter>

0 commit comments

Comments
 (0)