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
This commit is contained in:
Sam Brannen 2016-02-03 14:51:13 +01:00
parent d6648a06c9
commit 6d2b9a0136
2 changed files with 38 additions and 0 deletions

View File

@ -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) {

View File

@ -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);