UNDER CONSTRUCTION
Quick Links to:
New CSF AbstractIntegrationTestBase.java unit test base class
This new CSF abstract class is to be used with all unit tests. It replaces the following deprecated classes
- BaseTransactionalIntegrationTest
- AbstractTransactionalDataSourceSpringContextTests
that were used in csf-common-legacy.
About the AbstractIntegrationTestBase base class
Below is a complete listing of the AbstractIntergrationTestBase class. Of particular interest is the usage of the @Transactional and the @ContextConfiguration annotations.
It is important that you read and understand the class comments regarding the @Transactional annotation. It is reproduced here for emphasis.
* The class is marked as @Transactional, which means test methods in derived * classes will be run in a transaction and will be automatically rolled back on * completion. If you do not want transactional behavior in a test method, * annotate the method with @NotTransactional. If you do not want the automatic * rollback, annotate the method with @Rollback(false). BUT - normally our * "integration" tests should use a transaction and do rollback, so the defaults * should always be what you want.
The locations element of the @ContextConfiguration only specifies one xml file. This xml file is used to set the default application context for ALL unit tests which extends AbstractIntegrationTestBase.java class. At the bottom of this page is more information on how to configure and how to override the default application context.
import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; /** * @author sturner AbstractIntegrationTestBase * * Based on the old BaseTransactionalIntegrationTest class: refactored to move * away from deprecated Spring classes and to the newer Spring TestContext * Framework. * * NOTE - this is now an abstract class. This does not change the usage, as the * old version of the class was only ever used as a base class for JUnit * classes. * * The list of context config locations can be overriden by coding * @ContextConfiguration in a base class. * * The class is marked as @Transactional, which means test methods in derived * classes will be run in a transaction and will be automatically rolled back on * completion. If you do not want transactional behavior in a test method, * annotate the method with @NotTransactional. If you do not want the automatic * rollback, annotate the method with @Rollback(false). BUT - normally our * "integration" tests should use a transaction and do rollback, so the defaults * should always be what you want. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext-csf-unit-tests-default.xml"}) @TestExecutionListeners({TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class}) @Transactional public abstract class AbstractIntegrationTestBase { public AbstractIntegrationTestBase() { super(); } }
Usage of the AbstractIntegrationTestBase base class
- The following code fragment illustrates how to use AbstractIntegrationTestBase class
import edu.mit.csf.test.AbstractIntegrationTestBase; public class TestHibernateAcademicTermDao extends AbstractIntegrationTestBase { }
- Add the following dependencies to your project's pom.xml.
<dependency> <groupId>edu.mit.ist.es.csf</groupId> <artifactId>csf-test</artifactId> <version>[0.0.0,999.999.999)</version> <type>test-jar</type> <scope>test</scope> </dependency> <dependency> <groupId>edu.mit.ist.es.csf</groupId> <artifactId>csf-test</artifactId> <version>[0.0.0,999.999.999)</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency>
How to configuration the default test application context
The annotation
@ContextConfiguration(locations =
)
is used to set the default application context for all the junit tests which extend the AbstractIntegrationTestBase .java class.
If the project was created using the es-project-template (for either the jar or war) then applicationContext-csf-unit-tests-default.xml file can be found it the project's src/test/resources directory.
If the was not created using the es-project-template, then the applicationContext-cst-unit-tests-default.xml must be created in the projects sr/test/resources directory.