h3. New Method of Coding Integration Tests
Here's what we are doing to modernize our integration tests.
# *New Base Class for Integration Tests.*
\\
\\
We have created a new abstract class, *AbstractIntegrationTestBase* class, found in the csf-test module. This class uses JUnit and Spring annotations to gain access to functionality previously provided by extending the deprecated Spring class *{-}AbstractTransactionalDataSourceSpringContextTests{-}*. The relevant annotations are (at the class level):
\\
{code}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-csf-unit-tests-default.xml"})
@TestExecutionListeners({TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
{code}
*@RunWith* is a JUnit annotation that tells JUnit to use the Spring-supplied class to run the unit test rather than the default JUnit runner.
\\
*@ContextConfiguration* is a Spring annotation that tells the Spring test runner where to load the Spring context from.
\\
*@TestExecutionListeners* is a Spring annotation that adds functionality to the test case - in this case, transactional and dependency-injection functionality.
\\
\\
# *Changes in JUnit test case classes.*
\\
\\
#* Our JUnit integration tests should now extend the new *AbstractIntegrationTestBase* class. By doing so, the test class automatically gets the Spring context, and transactional and dependency-injection functionality from the base class. If required, the context configuration can be overriden simply by adding a similar @ContextConfiguration annotation to the test class.
#* You should provide the file *applicationContext-csf-unit-tests-default.xml*. This is a Spring configuration file, and should contain import statements that reference the Spring context files required by your test. An example can be seen in the *csf-iap* project (in the es-projects SVN repository).
#* To get the automatic rollback functionality, each JUnit test method should be annotated with @Transactional.
\\
#* Beans can be injected automatically by using the @Autowired annotation.
For a sample JUnit Integration test using the new method, take a look at *edu.mit.common.dao.academic.hibernate.TestHibernateAcademicTermDao* in the *csf-common-legacy* module.
|