diff --git a/spring-test/src/main/java/org/springframework/test/context/transaction/TestTransaction.java b/spring-test/src/main/java/org/springframework/test/context/transaction/TestTransaction.java index daf82410ca7..c511981c871 100644 --- a/spring-test/src/main/java/org/springframework/test/context/transaction/TestTransaction.java +++ b/spring-test/src/main/java/org/springframework/test/context/transaction/TestTransaction.java @@ -23,7 +23,8 @@ import org.springframework.transaction.TransactionStatus; * {@code TestTransaction} provides a collection of static utility methods for * programmatic interaction with test-managed transactions. * - *
Test-managed transactions are transactions that are managed by the Spring TestContext Framework. + *
Test-managed transactions are transactions that are managed by the + * Spring TestContext Framework. * *
Support for {@code TestTransaction} is automatically available whenever * the {@link TransactionalTestExecutionListener} is enabled. Note that the diff --git a/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java index 050523f67fd..13f915cc668 100644 --- a/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java @@ -28,7 +28,6 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils; -import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.test.annotation.Rollback; @@ -38,7 +37,6 @@ import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; -import org.springframework.transaction.annotation.TransactionManagementConfigurer; import org.springframework.transaction.interceptor.TransactionAttribute; import org.springframework.transaction.interceptor.TransactionAttributeSource; import org.springframework.util.Assert; @@ -49,14 +47,30 @@ import static org.springframework.core.annotation.AnnotationUtils.*; /** * {@code TestExecutionListener} that provides support for executing tests - * within transactions by honoring the + * within test-managed transactions by honoring Spring's * {@link org.springframework.transaction.annotation.Transactional @Transactional} - * annotation. Expects a {@link PlatformTransactionManager} bean to be defined in the - * Spring {@link ApplicationContext} for the test. + * annotation. * - *
Changes to the database during a test that is run with {@code @Transactional} - * will be run within a transaction that will, by default, be automatically - * rolled back after completion of the test. Test methods that are + *
Test-managed transactions are transactions that are managed + * by this listener. Such transactions should not be confused with + * Spring-managed transactions (i.e., those managed directly + * by Spring within the {@code ApplicationContext} loaded for tests) or + * application-managed transactions (i.e., those managed + * programmatically within application code that is invoked via tests). + * Spring-managed transactions and application-managed transactions will + * typically participate in test-managed transactions; however, caution + * should be taken if Spring-managed transactions or application-managed + * transactions are configured with any propagation type other than + * {@link org.springframework.transaction.annotation.Propagation#REQUIRED REQUIRED} + * or {@link org.springframework.transaction.annotation.Propagation#SUPPORTS SUPPORTS}. + * + *
Annotating a test method with {@code @Transactional} causes the test + * to be run within a transaction that will, by default, be automatically + * rolled back after completion of the test. If a test class is + * annotated with {@code @Transactional}, each test method within that class + * hierarchy will be run within a transaction. Test methods that are * not annotated with {@code @Transactional} (at the class or method * level) will not be run within a transaction. Furthermore, test methods * that are annotated with {@code @Transactional} but have the @@ -65,33 +79,49 @@ import static org.springframework.core.annotation.AnnotationUtils.*; * {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED} * will not be run within a transaction. * - *
Transactional commit and rollback behavior can be configured via the - * class-level {@link TransactionConfiguration @TransactionConfiguration} and - * method-level {@link Rollback @Rollback} annotations. + *
By default, test transactions will be automatically rolled back + * after completion of the test; however, transactional commit and rollback + * behavior can be configured declaratively via the class-level + * {@link TransactionConfiguration @TransactionConfiguration} and method-level + * {@link Rollback @Rollback} annotations. * - *
In case there are multiple instances of {@code PlatformTransactionManager} - * within the test's {@code ApplicationContext}, {@code @TransactionConfiguration} - * supports configuring the bean name of the {@code PlatformTransactionManager} - * that should be used to drive transactions. Alternatively, a qualifier - * may be declared via {@link Transactional#value} or - * {@link TransactionManagementConfigurer} can be implemented by an + *
As of Spring Framework 4.1, it is possible to interact with test-managed + * transactions programmatically via the static methods in {@link TestTransaction}. + * {@code TestTransaction} may be used within test methods, + * before methods, and after methods. + * + *
When executing transactional tests, it is sometimes useful to be able to + * execute certain set up or tear down code outside of a + * transaction. {@code TransactionalTestExecutionListener} provides such + * support for methods annotated with + * {@link BeforeTransaction @BeforeTransaction} or + * {@link AfterTransaction @AfterTransaction}. + * + *
{@code TransactionalTestExecutionListener} expects a + * {@link PlatformTransactionManager} bean to be defined in the Spring + * {@code ApplicationContext} for the test. In case there are multiple + * instances of {@code PlatformTransactionManager} within the test's + * {@code ApplicationContext}, {@code @TransactionConfiguration} supports + * configuring the bean name of the {@code PlatformTransactionManager} that + * should be used to drive transactions. Alternatively, a qualifier + * may be declared via + * {@link org.springframework.transaction.annotation.Transactional#value @Transactional("myQualifier")}, or + * {@link org.springframework.transaction.annotation.TransactionManagementConfigurer TransactionManagementConfigurer} + * can be implemented by an * {@link org.springframework.context.annotation.Configuration @Configuration} * class. See {@link TestContextTransactionUtils#retrieveTransactionManager()} * for details on the algorithm used to look up a transaction manager in * the test's {@code ApplicationContext}. * - *
When executing transactional tests, it is sometimes useful to be able to - * execute certain set up or tear down code outside of a - * transaction. {@code TransactionalTestExecutionListener} provides such - * support for methods annotated with - * {@link BeforeTransaction @BeforeTransaction} and - * {@link AfterTransaction @AfterTransaction}. - * * @author Sam Brannen * @author Juergen Hoeller * @since 2.5 * @see TransactionConfiguration - * @see TransactionManagementConfigurer + * @see org.springframework.transaction.annotation.TransactionManagementConfigurer * @see org.springframework.transaction.annotation.Transactional * @see org.springframework.test.annotation.Rollback * @see BeforeTransaction