Skip to content

Commit

Permalink
Support Propagation.NEVER for disabling test-managed transactions
Browse files Browse the repository at this point in the history
Prior to this commit only Propagation.NOT_SUPPORTED was supported for
disabling test-managed transactions via the `propagation` attribute of
`@Transactional`.

This commit allows users to specify Propagation.NOT_SUPPORTED or
Propagation.NEVER to disable test-managed transactions.

Closes gh-25909
  • Loading branch information
sbrannen committed Oct 13, 2020
1 parent 33fcba5 commit e5ae2cb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
* <em>are</em> annotated with {@code @Transactional} but have the
* {@link Transactional#propagation propagation} type set to
* {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED}
* or {@link org.springframework.transaction.annotation.Propagation#NEVER NEVER}
* will not be run within a transaction.
*
* <h3>Declarative Rollback and Commit Behavior</h3>
Expand Down Expand Up @@ -123,7 +124,8 @@
* <tr><th>Attribute</th><th>Supported for test-managed transactions</th></tr>
* <tr><td>{@link Transactional#value value} and {@link Transactional#transactionManager transactionManager}</td><td>yes</td></tr>
* <tr><td>{@link Transactional#propagation propagation}</td>
* <td>only {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED} is supported</td></tr>
* <td>only {@link org.springframework.transaction.annotation.Propagation#NOT_SUPPORTED NOT_SUPPORTED}
* and {@link org.springframework.transaction.annotation.Propagation#NEVER NEVER} are supported</td></tr>
* <tr><td>{@link Transactional#isolation isolation}</td><td>no</td></tr>
* <tr><td>{@link Transactional#timeout timeout}</td><td>no</td></tr>
* <tr><td>{@link Transactional#readOnly readOnly}</td><td>no</td></tr>
Expand Down Expand Up @@ -213,7 +215,8 @@ public void beforeTestMethod(final TestContext testContext) throws Exception {
"] found for test context " + testContext);
}

if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED ||
transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NEVER) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void isFlaggedForRollbackWithNonExistentTransactionContext() {
}

@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@Transactional(propagation = Propagation.NEVER)
void startTxWithNonExistentTransactionContext() {
assertThatIllegalStateException().isThrownBy(TestTransaction::start);
}
Expand All @@ -145,7 +145,7 @@ void startTxWithExistingTransaction() {
}

@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
@Transactional(propagation = Propagation.NEVER)
void endTxWithNonExistentTransactionContext() {
assertThatIllegalStateException().isThrownBy(TestTransaction::end);
}
Expand Down
4 changes: 2 additions & 2 deletions src/docs/asciidoc/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5056,7 +5056,7 @@ hierarchy runs within a transaction. Test methods that are not annotated with
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` attribute set to
`NOT_SUPPORTED` are not run within a transaction.
`NOT_SUPPORTED` or `NEVER` are not run within a transaction.

[[testcontext-tx-attribute-support]]
.`@Transactional` attribute support
Expand All @@ -5065,7 +5065,7 @@ are annotated with `@Transactional` but have the `propagation` attribute set to

|`value` and `transactionManager` |yes

|`propagation` |only `Propagation.NOT_SUPPORTED` is supported
|`propagation` |only `Propagation.NOT_SUPPORTED` and `Propagation.NEVER` are supported

|`isolation` |no

Expand Down

0 comments on commit e5ae2cb

Please sign in to comment.