Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Panel

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.

Note

It is important that you read and understand the class comments regarding the @Transactional annotation.  It is reproduced here for emphasis.

Code Block

 * 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.

Code Block
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();
    }
}

...