diff --git a/spring-framework-reference/src/testing.xml b/spring-framework-reference/src/testing.xml index 3b26d7b62da..46fae420b7e 100644 --- a/spring-framework-reference/src/testing.xml +++ b/spring-framework-reference/src/testing.xml @@ -22,7 +22,7 @@ One of the main benefits of Dependency Injection is that your code should really depend far less on the container than in traditional J2EE - development. The POJOs that comprise your application should be testable + development. The POJOs that make up your application should be testable in JUnit or TestNG tests, with objects simply instantiated using the new operator, without Spring or any other container. You can use mock @@ -181,7 +181,7 @@ linkend="testcontext-framework">Spring TestContext Framework, which is agnostic of the actual testing framework in use, thus allowing instrumentation of tests in various environments including JUnit 3.8, - JUnit 4.4, TestNG, etc. Note that the Spring TestContext + JUnit 4.5, TestNG, etc. Note that the Spring TestContext Framework requires Java 5+. @@ -443,7 +443,7 @@ simplify standard database testing scenarios. Note that AbstractTransactionalJUnit38SpringContextTests, AbstractTransactionalJUnit4SpringContextTests, + linkend="testcontext-support-classes-junit45">AbstractTransactionalJUnit4SpringContextTests, and AbstractTransactionalTestNGSpringContextTests provide convenience methods which delegate to @@ -636,7 +636,7 @@ public void testProcessWithoutTransaction() { linkend="testcontext-junit4-runner">SpringJUnit4ClassRunner or the JUnit 3.8 and JUnit 4.4 base + linkend="testcontext-support-classes-junit45">JUnit 4.5 base test classes). Refer to the documentation in the TestContext Framework section for further details. @@ -647,7 +647,7 @@ public void testProcessWithoutTransaction() {
JUnit 3.8 legacy support - Spring's JUnit 3.8 legacy support is comprised of the classes + Spring's JUnit 3.8 legacy support consists of the classes found in the org.springframework.test package. This package provides valuable JUnit TestCase superclasses which can be extended for out-of-container integration @@ -967,15 +967,15 @@ public void testProcessWithoutTransaction() { org.springframework.test.context package) provides generic, annotation-driven unit and integration testing support that is agnostic of the testing framework in use, for example JUnit 3.8, JUnit - 4.4, TestNG 5.5, etc. The TestContext framework also places a great deal + 4.5, TestNG 5.8, etc. The TestContext framework also places a great deal of importance on convention over configuration with reasonable defaults that can be overridden via annotation-based configuration. In addition to generic testing infrastructure, the TestContext - framework provides explicit support for JUnit 3.8, JUnit 4.4, and TestNG - 5.5 in the form of abstract support classes. For - JUnit 4.4, the framework also provides a custom + framework provides explicit support for JUnit 3.8, JUnit 4.5, and TestNG + 5.8 in the form of abstract support classes. For + JUnit 4.5, the framework also provides a custom Runner which allows one to write test classes that are not required to extend a particular class hierarchy. @@ -1041,9 +1041,7 @@ public void testProcessWithoutTransaction() { Spring provides three TestExecutionListener - implementations which are configured by default (via the - @TestExecutionListeners - annotation): + implementations which are configured by default: DependencyInjectionTestExecutionListener, DirtiesContextTestExecutionListener, and TransactionalTestExecutionListener, which @@ -1081,6 +1079,25 @@ public void testProcessWithoutTransaction() { implement ApplicationContextAware and therefore provide this functionality out-of-the-box. + + @Autowired ApplicationContext + + As an alternative to implementing the + ApplicationContextAware interface, + your test class can have its application context injected via the + @Autowired annotation on either a + field or setter method, for example: + + @RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class MyTest { + @Autowired + private ApplicationContext applicationContext; + + // class body... +} + + In contrast to the JUnit 3.8 legacy support, test classes which use the TestContext framework do not need to override any protected instance methods to configure their @@ -1196,10 +1213,10 @@ public class ExtendedTest extends BaseTest { @ContextConfiguration by Setter Injection, Field Injection, or both, depending on which annotations you choose and whether you place them on setter methods or fields. For - consistency with annotation support in Spring 2.5, you may choose - either Spring's @Autowired annotation - or the @Resource annotation from JSR - 250. The semantics for both are consistent throughout the Spring + consistency with the annotation support introduced in Spring 2.5, you + may choose either Spring's @Autowired + annotation or the @Resource annotation + from JSR 250. The semantics for both are consistent throughout the Spring Framework. For example, if you prefer autowiring by type, annotate your setter methods or fields with @@ -1221,11 +1238,13 @@ public class ExtendedTest extends BaseTest { same type, you cannot rely on this approach for those particular beans. In that case, you can use @Resource for injection by - name. Alternatively, if your test class implements - ApplicationContextAware, you can directly - access the ApplicationContext supplied to your - test and perform an explicit lookup using (for example) a call to - applicationContext.getBean("titleDao"). + name. Alternatively, if your test class has access to its + ApplicationContext, you can perform an explicit + lookup using (for example) a call to + applicationContext.getBean("titleDao"). A + third option is to use @Autowired + in conjunction with @Qualifier. + If you don't want dependency injection applied to your test instances, simply don't annotate any fields or setter methods with @@ -1240,12 +1259,12 @@ public class ExtendedTest extends BaseTest { Consider the scenario where we have a class, HibernateTitleDao (as outlined in the common goals section). First, - let's look at a JUnit 4.4 based implementation of the test class + let's look at a JUnit 4.5 based implementation of the test class itself which uses @Autowired for field injection (we will look at the application context configuration after all sample code listings). Note: The dependency injection behavior in the following code listings is not in any way specific to - JUnit 4.4. The same DI techniques can be used in conjunction with any + JUnit 4.5. The same DI techniques can be used in conjunction with any testing framework. @RunWith(SpringJUnit4ClassRunner.class) @@ -1441,7 +1460,7 @@ public final class HibernateTitleDaoTests { Note that AbstractTransactionalJUnit38SpringContextTests, AbstractTransactionalJUnit4SpringContextTests, + linkend="testcontext-support-classes-junit45">AbstractTransactionalJUnit4SpringContextTests, and AbstractTransactionalTestNGSpringContextTests are pre-configured for transactional support at the class level. @@ -1595,11 +1614,11 @@ public class FictitiousTransactionalTest {
-
- JUnit 4.4 support classes +
+ JUnit 4.5 support classes The org.springframework.test.context.junit4 - package provides support classes for JUnit 4.4 based test + package provides support classes for JUnit 4.5 based test cases. @@ -1609,7 +1628,7 @@ public class FictitiousTransactionalTest { Abstract base test class which integrates the Spring TestContext Framework with explicit ApplicationContext testing support in a - JUnit 4.4 environment. + JUnit 4.5 environment. When you extend AbstractJUnit4SpringContextTests you will @@ -1679,13 +1698,13 @@ public class FictitiousTransactionalTest {
- Custom JUnit 4.4 Runner + Custom JUnit 4.5 Runner The Spring TestContext Framework offers - full integration with JUnit 4.4 via a custom runner. By annotating + full integration with JUnit 4.5 via a custom runner. By annotating test classes with @Runwith(SpringJUnit4ClassRunner.class), - developers can implement standard JUnit 4.4 unit and integration + developers can implement standard JUnit 4.5 unit and integration tests and simultaneously reap the benefits of the TestContext framework such as support for loading application contexts, dependency injection of test instances, transactional test method @@ -1810,7 +1829,7 @@ public class SimpleTest { linkend="testcontext-junit4-runner">SpringJUnit4ClassRunner or the JUnit 3.8 and JUnit 4.4 support + linkend="testcontext-support-classes-junit45">JUnit 4.5 support classes. @@ -2017,7 +2036,7 @@ public void afterTransaction() { The PetClinic sample application included with the full Spring distribution illustrates several features of the Spring - TestContext Framework in a JUnit 4.4 environment. Most test + TestContext Framework in a JUnit 4.5 environment. Most test functionality is included in the AbstractClinicTests, for which a partial listing is shown below: @@ -2149,57 +2168,56 @@ public class HibernateClinicTests extends AbstractClinicTests { }
Further Resources - This section contains links to further resources about testing in - general. + This section contains links to further resources about testing in general. - The JUnit homepage. - The Spring Framework's unit test suite is written using JUnit 3.8 as - the testing framework. + JUnit: + the Spring Framework's unit and integration test suite is written using + JUnit 3.8 and JUnit 4.5 as the testing framework. - The TestNG homepage. - TestNG is a testing framework inspired by JUnit 3.8 with added support + TestNG: + a testing framework inspired by JUnit 3.8 with added support for Java 5 annotations, test groups, data-driven testing, distributed testing, etc. - The Mock Objects - homepage. About Mock Objects, a technique for improving the - design of code within Test-Driven Development. + MockObjects.com: + a website dedicated to mock objects, a technique for improving the design + of code within Test-Driven Development. - "Mock - Objects" article at Wikipedia. + "Mock Objects": + article at Wikipedia. - The EasyMock - homepage. The Spring Framework uses EasyMock extensively in - its test suite. + EasyMock: + the Spring Framework uses EasyMock extensively in its test suite. - The JMock homepage. - JMock is a library that supports test-driven development of Java code + JMock: + a library that supports test-driven development of Java code with mock objects. - The DbUnit - homepage. DbUnit is a JUnit extension (also usable with Ant) - targeted for database-driven projects that, among other things, puts - your database into a known state between test runs. + DbUnit: + a JUnit extension (also usable with Ant and Maven) targeted for database-driven + projects that, among other things, puts your database into a known state + between test runs. - The Grinder - homepage. The Grinder is a Java load-testing framework. + Grinder: + a Java load testing framework. +
\ No newline at end of file