120
120
<title >Spring MVC</title >
121
121
122
122
<para >The <literal >org.springframework.test.web</literal > package
123
- contains <classname >AbstractModelAndViewTests </classname >, which
124
- serves as a convenient base class for JUnit 3.8 based unit tests
125
- dealing with Spring MVC < classname >ModelAndView</ classname > objects.
126
- When developing against Java 1.4 and higher (e.g., in combination with
127
- JUnit 4+, TestNG, etc.), you have the option of using the
128
- <classname >ModelAndViewAssert</ classname > class (in the same package)
129
- to test your < classname >ModelAndView</ classname > related
130
- functionality.</ para >
131
-
132
- < para >Tip: depending on your testing environment, either extend
133
- <classname >AbstractModelAndViewTests</ classname > or use
134
- <classname >ModelAndViewAssert</ classname > directly and then use
135
- <literal >MockHttpServletRequest </literal >,
136
- < literal >MockHttpSession</ literal >, etc. from the < link
137
- linkend = " mock-objects-servlet " >< literal >org.springframework.mock.web</ literal ></ link >
138
- package to test your Spring MVC < literal >Controller</ literal >s.</ para >
123
+ contains <classname >ModelAndViewAssert </classname >, which can be
124
+ used in combination with any testing framework (e.g., JUnit 4+,
125
+ TestNG, etc.) for unit tests dealing with Spring MVC
126
+ < classname >ModelAndView</ classname > objects.</ para >
127
+
128
+ <tip >
129
+ < title >Unit testing Spring MVC Controllers</ title >
130
+ < para >
131
+ To test your Spring MVC < literal >Controller</ literal >s,
132
+ use < classname >ModelAndViewAssert</ classname > combined with
133
+ <literal >MockHttpServletRequest</ literal >,
134
+ <literal >MockHttpSession</ literal >, etc. from the < link
135
+ linkend = " mock-objects-servlet " > <literal >org.springframework.mock.web </literal ></ link >
136
+ package.
137
+ </ para >
138
+ </ tip >
139
139
</section >
140
140
</section >
141
141
</section >
175
175
or remote tests relying on deployment to an application server.</para >
176
176
177
177
<para >
178
- Since Spring 2.5 unit and integration testing support is provided
178
+ Since Spring 2.5, unit and integration testing support is provided
179
179
in the form of the annotation-driven <link
180
180
linkend =" testcontext-framework" >Spring TestContext Framework</link >.
181
181
The TestContext Framework is agnostic of the actual testing framework
265
265
rebuild the application context before executing the next test.</para >
266
266
267
267
<para >
268
- Context management and caching with the
268
+ See: context management and caching with the
269
269
<link linkend =" testcontext-ctx-management" >TestContext Framework</link >.
270
270
</para >
271
271
</section >
312
312
</itemizedlist >
313
313
314
314
<para >
315
- Dependency Injection of test fixtures with the
315
+ See: dependency injection of test fixtures with the
316
316
<link linkend =" testcontext-fixture-di" >TestContext Framework</link >.
317
317
</para >
318
318
</section >
323
323
<para >One common issue in tests that access a real database is their
324
324
affect on the state of the persistence store. Even when you're using a
325
325
development database, changes to the state may affect future tests.
326
- Also, many operations - such as inserting to or modifying persistent
326
+ Also, many operations - such as inserting or modifying persistent
327
327
data - cannot be performed (or verified) outside a transaction.</para >
328
328
329
- <para >The TestContext framework meets this need . By default, the
330
- framework will create and roll back a transaction for each
329
+ <para >The TestContext framework addresses this issue . By default,
330
+ the framework will create and roll back a transaction for each
331
331
test. You simply write code that can assume the existence of a
332
332
transaction. If you call transactionally proxied objects in your
333
333
tests, they will behave correctly, according to their transactional
343
343
useful when you want a particular test to populate or modify the
344
344
database - the TestContext framework can be
345
345
instructed to cause the transaction to commit instead of roll back
346
- via the <interfacename >@TransactionConfiguration</interfacename >
347
- and <interfacename >@Rollback</interfacename > annotations.</para >
346
+ via the
347
+ <link linkend =" integration-testing-annotations" ><interfacename >@TransactionConfiguration</interfacename ></link >
348
+ and
349
+ <link linkend =" integration-testing-annotations" ><interfacename >@Rollback</interfacename ></link >
350
+ annotations.</para >
348
351
349
352
<para >
350
- Transaction management with the
353
+ See: transaction management with the
351
354
<link linkend =" testcontext-tx" >TestContext Framework</link >.
352
355
</para >
353
356
</section >
382
385
</listitem >
383
386
</itemizedlist >
384
387
385
- <para >You may find it desirable to provide a custom, application-wide superclass for
386
- integration tests that provides further useful instance variables and
388
+ <para >
389
+ In addition, you may find it desirable to provide your own custom,
390
+ application-wide superclass for integration tests that provides
391
+ further useful instance variables and
387
392
methods specific to your project.</para >
388
393
389
394
<para >
390
- Support classes for the
395
+ See: support classes for the
391
396
<link linkend =" testcontext-support-classes" >TestContext Framework</link >.
392
397
</para >
393
398
</section >
@@ -458,6 +463,7 @@ public class CustomConfiguredApplicationContextTests {
458
463
passed or not).</para >
459
464
460
465
<programlisting language =" java" >@DirtiesContext
466
+ @Test
461
467
public void testProcessWhichDirtiesAppCtx() {
462
468
<lineannotation >// some logic that results in the Spring container being dirtied</lineannotation >
463
469
}</programlisting >
@@ -520,6 +526,7 @@ public class CustomConfiguredTransactionalTests {
520
526
the default rollback flag configured at the class level.</para >
521
527
522
528
<programlisting language =" java" >@Rollback(false)
529
+ @Test
523
530
public void testProcessWithoutRollback() {
524
531
<lineannotation >// ...</lineannotation >
525
532
}</programlisting >
@@ -566,6 +573,7 @@ public void afterTransaction() {
566
573
context.</para >
567
574
568
575
<programlisting language =" java" >@NotTransactional
576
+ @Test
569
577
public void testProcessWithoutTransaction() {
570
578
<lineannotation >// ...</lineannotation >
571
579
}</programlisting >
@@ -596,6 +604,7 @@ public void testProcessWithoutTransaction() {
596
604
entire class or individual methods.</para >
597
605
598
606
<programlisting language =" java" >@IfProfileValue(name="java.vendor", value="Sun Microsystems Inc.")
607
+ @Test
599
608
public void testProcessWhichRunsOnlyOnSunJvm() {
600
609
<lineannotation >// some logic that should run only on Java VMs from Sun Microsystems</lineannotation >
601
610
}</programlisting >
@@ -607,6 +616,7 @@ public void testProcessWhichRunsOnlyOnSunJvm() {
607
616
Consider the following example:</para >
608
617
609
618
<programlisting language =" java" >@IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"})
619
+ @Test
610
620
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
611
621
<lineannotation >// some logic that should run only for unit and integration test groups</lineannotation >
612
622
}</programlisting >
@@ -706,6 +716,7 @@ public void testProcessWithOneSecondTimeout() {
706
716
fixture.</para >
707
717
708
718
<programlisting language =" java" >@Repeat(10)
719
+ @Test
709
720
public void testProcessRepeatedly() {
710
721
<lineannotation >// ...</lineannotation >
711
722
}</programlisting >
@@ -894,16 +905,16 @@ public void testProcessRepeatedly() {
894
905
<programlisting language =" java" >@RunWith(SpringJUnit4ClassRunner.class)
895
906
@ContextConfiguration
896
907
public class MyTest {
897
- <emphasis role =" bold" >@Autowired</emphasis >
898
- private ApplicationContext applicationContext;
908
+ <emphasis role =" bold" >@Autowired</emphasis >
909
+ private ApplicationContext applicationContext;
899
910
900
911
<lineannotation >// class body...</lineannotation >
901
912
}</programlisting >
902
913
</tip >
903
914
904
- <para >In contrast to the now deprecated JUnit 3.8 legacy support, test
905
- classes which use the TestContext framework do not need to override any
906
- <literal >protected</literal > instance methods to configure their
915
+ <para >In contrast to the now deprecated JUnit 3.8 legacy class hierarchy,
916
+ test classes which use the TestContext framework do not need to override
917
+ any <literal >protected</literal > instance methods to configure their
907
918
application context. Rather, configuration is achieved merely by
908
919
declaring the <interfacename >@ContextConfiguration</interfacename >
909
920
annotation at the class level. If your test class does not explicitly
@@ -1079,7 +1090,7 @@ public class ExtendedTest extends BaseTest {
1079
1090
1080
1091
<programlisting language =" java" >@RunWith(SpringJUnit4ClassRunner.class)
1081
1092
<lineannotation >// specifies the Spring configuration to load for this test fixture</lineannotation >
1082
- <emphasis role =" bold" >@ContextConfiguration(locations={ "daos.xml"} )</emphasis >
1093
+ <emphasis role =" bold" >@ContextConfiguration("daos.xml")</emphasis >
1083
1094
public final class HibernateTitleDaoTests {
1084
1095
1085
1096
<lineannotation >// this instance will be dependency injected <emphasis
@@ -1100,7 +1111,7 @@ public final class HibernateTitleDaoTests {
1100
1111
1101
1112
<programlisting language =" java" >@RunWith(SpringJUnit4ClassRunner.class)
1102
1113
<lineannotation >// specifies the Spring configuration to load for this test fixture</lineannotation >
1103
- <emphasis role =" bold" >@ContextConfiguration(locations={ "daos.xml"} )</emphasis >
1114
+ <emphasis role =" bold" >@ContextConfiguration("daos.xml")</emphasis >
1104
1115
public final class HibernateTitleDaoTests {
1105
1116
1106
1117
<lineannotation >// this instance will be dependency injected <emphasis
@@ -1125,7 +1136,7 @@ public final class HibernateTitleDaoTests {
1125
1136
1126
1137
<programlisting language =" java" >@RunWith(SpringJUnit4ClassRunner.class)
1127
1138
<lineannotation >// specifies the Spring configuration to load for this test fixture</lineannotation >
1128
- <emphasis role =" bold" >@ContextConfiguration(locations={ "daos.xml"} )</emphasis >
1139
+ <emphasis role =" bold" >@ContextConfiguration("daos.xml")</emphasis >
1129
1140
public final class HibernateTitleDaoTests {
1130
1141
1131
1142
<lineannotation >// this instance will be dependency injected <emphasis
@@ -1146,7 +1157,7 @@ public final class HibernateTitleDaoTests {
1146
1157
1147
1158
<programlisting language =" java" >@RunWith(SpringJUnit4ClassRunner.class)
1148
1159
<lineannotation >// specifies the Spring configuration to load for this test fixture</lineannotation >
1149
- <emphasis role =" bold" >@ContextConfiguration(locations={ "daos.xml"} )</emphasis >
1160
+ <emphasis role =" bold" >@ContextConfiguration("daos.xml")</emphasis >
1150
1161
public final class HibernateTitleDaoTests {
1151
1162
1152
1163
<lineannotation >// this instance will be dependency injected <emphasis
0 commit comments