Browse Source

Throw exception if TxMgr cannot be retrieved for @Transactional test

Prior to this commit, a @Transactional integration test would silently
be executed without a transaction if the transaction manager could not
be retrieved from the application context -- for example, it no such
bean was defined or if multiple beans were present but none satisfied
the qualifier.

This commit addresses this issue by throwing an IllegalStateException
if the PlatformTransactionManager cannot be retrieved for a
@Transactional test.

Issue: SPR-13895
(cherry picked from commit 6d2b9a0136)
pull/966/head
Sam Brannen 10 years ago
parent
commit
484dd96606
  1. 6
      spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java
  2. 32
      spring-test/src/test/java/org/springframework/test/context/transaction/TransactionalTestExecutionListenerTests.java

6
spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java

@ -186,6 +186,12 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis @@ -186,6 +186,12 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
}
tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
if (tm == null) {
throw new IllegalStateException(String.format(
"Failed to retrieve PlatformTransactionManager for @Transactional test for test context %s.",
testContext));
}
}
if (tm != null) {

32
spring-test/src/test/java/org/springframework/test/context/transaction/TransactionalTestExecutionListenerTests.java

@ -155,6 +155,38 @@ public class TransactionalTestExecutionListenerTests { @@ -155,6 +155,38 @@ public class TransactionalTestExecutionListenerTests {
TransactionContextHolder.removeCurrentTransactionContext();
}
/**
* SPR-13895
*/
@Test
public void transactionalTestWithoutTransactionManager() throws Exception {
TransactionalTestExecutionListener listener = new TransactionalTestExecutionListener() {
protected PlatformTransactionManager getTransactionManager(TestContext testContext, String qualifier) {
return null;
}
};
Class<? extends Invocable> clazz = TransactionalDeclaredOnClassLocallyTestCase.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
Invocable instance = clazz.newInstance();
given(testContext.getTestInstance()).willReturn(instance);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest"));
assertFalse(instance.invoked);
TransactionContextHolder.removeCurrentTransactionContext();
try {
listener.beforeTestMethod(testContext);
fail("Should have thrown an IllegalStateException");
}
catch (IllegalStateException e) {
assertTrue(e.getMessage().startsWith(
"Failed to retrieve PlatformTransactionManager for @Transactional test for test context"));
}
}
@Test
public void beforeTestMethodWithTransactionalDeclaredOnClassLocally() throws Exception {
assertBeforeTestMethodWithTransactionalTestMethod(TransactionalDeclaredOnClassLocallyTestCase.class);

Loading…
Cancel
Save