Skip to content

Commit c0c9501

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

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

org.springframework.test/src/main/java/org/springframework/test/web/AbstractModelAndViewTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
/**
2929
* Convenient JUnit 3.8 base class for tests dealing with Spring Web MVC
3030
* {@link org.springframework.web.servlet.ModelAndView ModelAndView} objects.
31-
*
31+
*
3232
* <p>All <code>assert*()</code> methods throw {@link AssertionFailedError}s.
33-
*
33+
*
3434
* <p>Consider the use of {@link ModelAndViewAssert} with JUnit 4 and TestNG.
35-
*
35+
*
3636
* @author Alef Arendsen
3737
* @author Bram Smeets
3838
* @author Sam Brannen
@@ -41,6 +41,7 @@
4141
* @see ModelAndViewAssert
4242
* @deprecated as of Spring 3.0, in favor of using the listener-based test context framework
4343
* ({@link org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests})
44+
* or {@link ModelAndViewAssert} with JUnit 4 and TestNG.
4445
*/
4546
@Deprecated
4647
public abstract class AbstractModelAndViewTests extends TestCase {

spring-framework-reference/src/testing.xml

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,22 @@
120120
<title>Spring MVC</title>
121121

122122
<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>
139139
</section>
140140
</section>
141141
</section>
@@ -175,7 +175,7 @@
175175
or remote tests relying on deployment to an application server.</para>
176176

177177
<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
179179
in the form of the annotation-driven <link
180180
linkend="testcontext-framework">Spring TestContext Framework</link>.
181181
The TestContext Framework is agnostic of the actual testing framework
@@ -265,7 +265,7 @@
265265
rebuild the application context before executing the next test.</para>
266266

267267
<para>
268-
Context management and caching with the
268+
See: context management and caching with the
269269
<link linkend="testcontext-ctx-management">TestContext Framework</link>.
270270
</para>
271271
</section>
@@ -312,7 +312,7 @@
312312
</itemizedlist>
313313

314314
<para>
315-
Dependency Injection of test fixtures with the
315+
See: dependency injection of test fixtures with the
316316
<link linkend="testcontext-fixture-di">TestContext Framework</link>.
317317
</para>
318318
</section>
@@ -323,11 +323,11 @@
323323
<para>One common issue in tests that access a real database is their
324324
affect on the state of the persistence store. Even when you're using a
325325
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
327327
data - cannot be performed (or verified) outside a transaction.</para>
328328

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
331331
test. You simply write code that can assume the existence of a
332332
transaction. If you call transactionally proxied objects in your
333333
tests, they will behave correctly, according to their transactional
@@ -343,11 +343,14 @@
343343
useful when you want a particular test to populate or modify the
344344
database - the TestContext framework can be
345345
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>
348351

349352
<para>
350-
Transaction management with the
353+
See: transaction management with the
351354
<link linkend="testcontext-tx">TestContext Framework</link>.
352355
</para>
353356
</section>
@@ -382,12 +385,14 @@
382385
</listitem>
383386
</itemizedlist>
384387

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
387392
methods specific to your project.</para>
388393

389394
<para>
390-
Support classes for the
395+
See: support classes for the
391396
<link linkend="testcontext-support-classes">TestContext Framework</link>.
392397
</para>
393398
</section>
@@ -458,6 +463,7 @@ public class CustomConfiguredApplicationContextTests {
458463
passed or not).</para>
459464

460465
<programlisting language="java">@DirtiesContext
466+
@Test
461467
public void testProcessWhichDirtiesAppCtx() {
462468
<lineannotation>// some logic that results in the Spring container being dirtied</lineannotation>
463469
}</programlisting>
@@ -520,6 +526,7 @@ public class CustomConfiguredTransactionalTests {
520526
the default rollback flag configured at the class level.</para>
521527

522528
<programlisting language="java">@Rollback(false)
529+
@Test
523530
public void testProcessWithoutRollback() {
524531
<lineannotation>// ...</lineannotation>
525532
}</programlisting>
@@ -566,6 +573,7 @@ public void afterTransaction() {
566573
context.</para>
567574

568575
<programlisting language="java">@NotTransactional
576+
@Test
569577
public void testProcessWithoutTransaction() {
570578
<lineannotation>// ...</lineannotation>
571579
}</programlisting>
@@ -596,6 +604,7 @@ public void testProcessWithoutTransaction() {
596604
entire class or individual methods.</para>
597605

598606
<programlisting language="java">@IfProfileValue(name="java.vendor", value="Sun Microsystems Inc.")
607+
@Test
599608
public void testProcessWhichRunsOnlyOnSunJvm() {
600609
<lineannotation>// some logic that should run only on Java VMs from Sun Microsystems</lineannotation>
601610
}</programlisting>
@@ -607,6 +616,7 @@ public void testProcessWhichRunsOnlyOnSunJvm() {
607616
Consider the following example:</para>
608617

609618
<programlisting language="java">@IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"})
619+
@Test
610620
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
611621
<lineannotation>// some logic that should run only for unit and integration test groups</lineannotation>
612622
}</programlisting>
@@ -706,6 +716,7 @@ public void testProcessWithOneSecondTimeout() {
706716
fixture.</para>
707717

708718
<programlisting language="java">@Repeat(10)
719+
@Test
709720
public void testProcessRepeatedly() {
710721
<lineannotation>// ...</lineannotation>
711722
}</programlisting>
@@ -894,16 +905,16 @@ public void testProcessRepeatedly() {
894905
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
895906
@ContextConfiguration
896907
public class MyTest {
897-
<emphasis role="bold">@Autowired</emphasis>
898-
private ApplicationContext applicationContext;
908+
<emphasis role="bold">@Autowired</emphasis>
909+
private ApplicationContext applicationContext;
899910

900911
<lineannotation>// class body...</lineannotation>
901912
}</programlisting>
902913
</tip>
903914

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
907918
application context. Rather, configuration is achieved merely by
908919
declaring the <interfacename>@ContextConfiguration</interfacename>
909920
annotation at the class level. If your test class does not explicitly
@@ -1079,7 +1090,7 @@ public class ExtendedTest extends BaseTest {
10791090

10801091
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
10811092
<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>
10831094
public final class HibernateTitleDaoTests {
10841095

10851096
<lineannotation>// this instance will be dependency injected <emphasis
@@ -1100,7 +1111,7 @@ public final class HibernateTitleDaoTests {
11001111

11011112
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
11021113
<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>
11041115
public final class HibernateTitleDaoTests {
11051116

11061117
<lineannotation>// this instance will be dependency injected <emphasis
@@ -1125,7 +1136,7 @@ public final class HibernateTitleDaoTests {
11251136

11261137
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
11271138
<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>
11291140
public final class HibernateTitleDaoTests {
11301141

11311142
<lineannotation>// this instance will be dependency injected <emphasis
@@ -1146,7 +1157,7 @@ public final class HibernateTitleDaoTests {
11461157

11471158
<programlisting language="java">@RunWith(SpringJUnit4ClassRunner.class)
11481159
<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>
11501161
public final class HibernateTitleDaoTests {
11511162

11521163
<lineannotation>// this instance will be dependency injected <emphasis

0 commit comments

Comments
 (0)