|
|
|
@ -3520,17 +3520,25 @@ Annotating a test method with `@Transactional` causes the test to be run within |
|
|
|
|
transaction that is, by default, automatically rolled back after completion of the test. |
|
|
|
|
If a test class is annotated with `@Transactional`, each test method within that class |
|
|
|
|
hierarchy runs within a transaction. Test methods that are not annotated with |
|
|
|
|
`@Transactional` (at the class or method level) are not run within a transaction. |
|
|
|
|
Furthermore, tests that are annotated with `@Transactional` but have the `propagation` |
|
|
|
|
type set to `NOT_SUPPORTED` are not run within a transaction. |
|
|
|
|
`@Transactional` (at the class or method level) are not run within a transaction. Note |
|
|
|
|
that `@Transactional` is not supported on test lifecycle methods — for example, methods |
|
|
|
|
annotated with JUnit Jupiter's `@BeforeAll`, `@BeforeEach`, etc. Furthermore, tests that |
|
|
|
|
are annotated with `@Transactional` but have the `propagation` type set to |
|
|
|
|
`NOT_SUPPORTED` are not run within a transaction. |
|
|
|
|
|
|
|
|
|
[TIP] |
|
|
|
|
==== |
|
|
|
|
When using TestNG `@BeforeClass`,`@BeforeSuite` methods can not be used with the |
|
|
|
|
`@Transactional` annotation, as they are considered non transactional methods by the |
|
|
|
|
the spring text contetxt framework. However, you can inject a `PlatfromTransactionManager` |
|
|
|
|
or a `TransactionTemplate` and use it within `@BeforeClass` method to perform a |
|
|
|
|
transaction. |
|
|
|
|
Method-level lifecycle methods — for example, methods annotated with JUnit Jupiter's |
|
|
|
|
`@BeforeEach` or `@AfterEach` — are run within a test-managed transaction. On the other |
|
|
|
|
hand, suite-level and class-level lifecycle methods — for example, methods annotated with |
|
|
|
|
JUnit Jupiter's `@BeforeAll` or `@AfterAll` and methods annotated with TestNG's |
|
|
|
|
`@BeforeSuite`, `@AfterSuite`, `@BeforeClass`, or `@AfterClass` — are _not_ run within a |
|
|
|
|
test-managed transaction. |
|
|
|
|
|
|
|
|
|
If you need to execute code in a suite-level or class-level lifecycle method within a |
|
|
|
|
transaction, you may wish to inject a corresponding `PlatformTransactionManager` into |
|
|
|
|
your test class and then use that with a `TransactionTemplate` for programmatic |
|
|
|
|
transaction management. |
|
|
|
|
==== |
|
|
|
|
|
|
|
|
|
Note that <<testcontext-support-classes-junit4, |
|
|
|
|