22
22
23
23
<para >One of the main benefits of Dependency Injection is that your code
24
24
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
26
26
in JUnit or TestNG tests, with objects simply instantiated using the
27
27
<literal >new</literal > operator, <emphasis >without Spring or any other
28
28
container</emphasis >. You can use <link linkend =" mock-objects" >mock
181
181
linkend =" testcontext-framework" >Spring TestContext Framework</link >,
182
182
which is agnostic of the actual testing framework in use, thus allowing
183
183
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
185
185
Framework requires Java 5+.</emphasis ></para >
186
186
</section >
187
187
443
443
simplify standard database testing scenarios. <emphasis >Note that <link
444
444
linkend =" testcontext-support-classes-junit38" ><classname >AbstractTransactionalJUnit38SpringContextTests</classname ></link >,
445
445
<link
446
- linkend =" testcontext-support-classes-junit44 " ><classname >AbstractTransactionalJUnit4SpringContextTests</classname ></link >,
446
+ linkend =" testcontext-support-classes-junit45 " ><classname >AbstractTransactionalJUnit4SpringContextTests</classname ></link >,
447
447
and <link
448
448
linkend =" testcontext-support-classes-testng" ><classname >AbstractTransactionalTestNGSpringContextTests</classname ></link >
449
449
provide convenience methods which delegate to
@@ -636,7 +636,7 @@ public void testProcessWithoutTransaction() {
636
636
linkend =" testcontext-junit4-runner" >SpringJUnit4ClassRunner</link >
637
637
or the <link linkend =" testcontext-support-classes-junit38" >JUnit
638
638
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
640
640
test classes). Refer to the documentation in the
641
641
<emphasis >TestContext Framework</emphasis > section for further
642
642
details.</para >
@@ -647,7 +647,7 @@ public void testProcessWithoutTransaction() {
647
647
<section id =" junit38-legacy-support" >
648
648
<title >JUnit 3.8 legacy support</title >
649
649
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
651
651
found in the <literal >org.springframework.test</literal > package. This
652
652
package provides valuable JUnit <classname >TestCase</classname >
653
653
superclasses which can be extended for out-of-container integration
@@ -967,15 +967,15 @@ public void testProcessWithoutTransaction() {
967
967
<literal >org.springframework.test.context</literal > package) provides
968
968
generic, annotation-driven unit and integration testing support that is
969
969
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
971
971
of importance on <emphasis >convention over configuration</emphasis > with
972
972
reasonable defaults that can be overridden via annotation-based
973
973
configuration.</para >
974
974
975
975
<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
979
979
<interfacename >Runner</interfacename > which allows one to write test
980
980
classes that are not required to extend a particular class
981
981
hierarchy.</para >
@@ -1041,9 +1041,7 @@ public void testProcessWithoutTransaction() {
1041
1041
1042
1042
<para >Spring provides three
1043
1043
<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:
1047
1045
<classname >DependencyInjectionTestExecutionListener</classname >,
1048
1046
<classname >DirtiesContextTestExecutionListener</classname >, and
1049
1047
<classname >TransactionalTestExecutionListener</classname >, which
@@ -1081,6 +1079,25 @@ public void testProcessWithoutTransaction() {
1081
1079
implement <interfacename >ApplicationContextAware</interfacename > and
1082
1080
therefore provide this functionality out-of-the-box.</para >
1083
1081
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
+
1084
1101
<para >In contrast to the JUnit 3.8 legacy support, test classes which
1085
1102
use the TestContext framework do not need to override any
1086
1103
<literal >protected</literal > instance methods to configure their
@@ -1196,10 +1213,10 @@ public class ExtendedTest extends BaseTest {
1196
1213
<interfacename >@ContextConfiguration</interfacename > by Setter
1197
1214
Injection, Field Injection, or both, depending on which annotations
1198
1215
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
1203
1220
Framework. For example, if you prefer <link
1204
1221
linkend =" beans-factory-autowire" ><emphasis >autowiring by
1205
1222
type</emphasis ></link >, annotate your setter methods or fields with
@@ -1221,11 +1238,13 @@ public class ExtendedTest extends BaseTest {
1221
1238
same type, you cannot rely on this approach for those particular
1222
1239
beans. In that case, you can use
1223
1240
<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 >
1229
1248
1230
1249
<para >If you don't want dependency injection applied to your test
1231
1250
instances, simply don't annotate any fields or setter methods with
@@ -1240,12 +1259,12 @@ public class ExtendedTest extends BaseTest {
1240
1259
<para >Consider the scenario where we have a class,
1241
1260
<classname >HibernateTitleDao</classname > (as outlined in the <link
1242
1261
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
1244
1263
itself which uses <interfacename >@Autowired</interfacename > for field
1245
1264
injection (we will look at the application context configuration after
1246
1265
all sample code listings). <emphasis >Note: The dependency injection
1247
1266
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
1249
1268
testing framework.</emphasis ></para >
1250
1269
1251
1270
<programlisting language =" java" >@RunWith(SpringJUnit4ClassRunner.class)
@@ -1441,7 +1460,7 @@ public final class HibernateTitleDaoTests {
1441
1460
<para ><emphasis >Note that <link
1442
1461
linkend =" testcontext-support-classes-junit38" ><classname >AbstractTransactionalJUnit38SpringContextTests</classname ></link >,
1443
1462
<link
1444
- linkend =" testcontext-support-classes-junit44 " ><classname >AbstractTransactionalJUnit4SpringContextTests</classname ></link >,
1463
+ linkend =" testcontext-support-classes-junit45 " ><classname >AbstractTransactionalJUnit4SpringContextTests</classname ></link >,
1445
1464
and <link
1446
1465
linkend =" testcontext-support-classes-testng" ><classname >AbstractTransactionalTestNGSpringContextTests</classname ></link >
1447
1466
are pre-configured for transactional support at the class level.
@@ -1595,11 +1614,11 @@ public class FictitiousTransactionalTest {
1595
1614
</itemizedlist >
1596
1615
</section >
1597
1616
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 >
1600
1619
1601
1620
<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
1603
1622
cases.</para >
1604
1623
1605
1624
<itemizedlist >
@@ -1609,7 +1628,7 @@ public class FictitiousTransactionalTest {
1609
1628
<para >Abstract base test class which integrates the
1610
1629
<emphasis >Spring TestContext Framework</emphasis > with explicit
1611
1630
<classname >ApplicationContext</classname > testing support in a
1612
- JUnit 4.4 environment.</para >
1631
+ JUnit 4.5 environment.</para >
1613
1632
1614
1633
<para >When you extend
1615
1634
<classname >AbstractJUnit4SpringContextTests</classname > you will
@@ -1679,13 +1698,13 @@ public class FictitiousTransactionalTest {
1679
1698
</section >
1680
1699
1681
1700
<section id =" testcontext-junit4-runner" >
1682
- <title >Custom JUnit 4.4 Runner</title >
1701
+ <title >Custom JUnit 4.5 Runner</title >
1683
1702
1684
1703
<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
1686
1705
test classes with
1687
1706
<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
1689
1708
tests and simultaneously reap the benefits of the TestContext
1690
1709
framework such as support for loading application contexts,
1691
1710
dependency injection of test instances, transactional test method
@@ -1810,7 +1829,7 @@ public class SimpleTest {
1810
1829
linkend =" testcontext-junit4-runner" >SpringJUnit4ClassRunner</link > or
1811
1830
the <link linkend =" testcontext-support-classes-junit38" >JUnit
1812
1831
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
1814
1833
classes.</emphasis ></para >
1815
1834
1816
1835
<itemizedlist >
@@ -2017,7 +2036,7 @@ public void afterTransaction() {
2017
2036
2018
2037
<para >The PetClinic sample application included with the full Spring
2019
2038
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
2021
2040
functionality is included in the
2022
2041
<classname >AbstractClinicTests</classname >, for which a partial listing
2023
2042
is shown below:</para >
@@ -2149,57 +2168,56 @@ public class HibernateClinicTests extends AbstractClinicTests { }
2149
2168
<section id =" testing-resources" >
2150
2169
<title >Further Resources</title >
2151
2170
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 >
2154
2172
2155
2173
<itemizedlist >
2156
2174
<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 >
2160
2178
</listitem >
2161
2179
2162
2180
<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
2165
2183
for Java 5 annotations, test groups, data-driven testing, distributed
2166
2184
testing, etc.</para >
2167
2185
</listitem >
2168
2186
2169
2187
<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 >
2173
2191
</listitem >
2174
2192
2175
2193
<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 >
2178
2196
</listitem >
2179
2197
2180
2198
<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 >
2184
2201
</listitem >
2185
2202
2186
2203
<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
2189
2206
with mock objects.</para >
2190
2207
</listitem >
2191
2208
2192
2209
<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 >
2197
2214
</listitem >
2198
2215
2199
2216
<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 >
2202
2219
</listitem >
2203
2220
</itemizedlist >
2221
+
2204
2222
</section >
2205
2223
</chapter >
0 commit comments