@After public void cleanUpThreadLocalStateForSubsequentTestClassesInSuite() { TransactionContextHolder.removeCurrentTransactionContext(); }
private static TransactionContext requireCurrentTransactionContext() { TransactionContext txContext = TransactionContextHolder.getCurrentTransactionContext(); Assert.state(txContext != null, "TransactionContext is not active"); return txContext; }
Assert.notNull(testMethod, "Test method of supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); Assert.state(txContext == null, "Cannot start new transaction without ending existing transaction"); runBeforeTransactionMethods(testContext); txContext.startTransaction(); TransactionContextHolder.setCurrentTransactionContext(txContext);
/** * If a transaction is currently active for the supplied * {@linkplain TestContext test context}, this method will end the transaction * and run {@link AfterTransaction @AfterTransaction} methods. * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if * an error occurs while ending the transaction. */ @Override public void afterTestMethod(TestContext testContext) throws Exception { Method testMethod = testContext.getTestMethod(); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); // If there was (or perhaps still is) a transaction... if (txContext != null) { TransactionStatus transactionStatus = txContext.getTransactionStatus(); try { // If the transaction is still active... if (transactionStatus != null && !transactionStatus.isCompleted()) { txContext.endTransaction(); } } finally { runAfterTransactionMethods(testContext); } } }
Assert.notNull(testMethod, "Test method of supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); Assert.state(txContext == null, "Cannot start new transaction without ending existing transaction"); runBeforeTransactionMethods(testContext); txContext.startTransaction(); TransactionContextHolder.setCurrentTransactionContext(txContext);
/** * Determine whether a test-managed transaction is currently <em>active</em>. * @return {@code true} if a test-managed transaction is currently active * @see #start() * @see #end() */ public static boolean isActive() { TransactionContext transactionContext = TransactionContextHolder.getCurrentTransactionContext(); if (transactionContext != null) { TransactionStatus transactionStatus = transactionContext.getTransactionStatus(); return (transactionStatus != null && !transactionStatus.isCompleted()); } return false; }
private void assertBeforeTestMethodWithTransactionalTestMethod(Class<? extends Invocable> clazz, boolean invokedInTx) throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = BeanUtils.instantiateClass(clazz); given(testContext.getTestInstance()).willReturn(instance); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest")); assertFalse("callback should not have been invoked", instance.invoked()); TransactionContextHolder.removeCurrentTransactionContext(); listener.beforeTestMethod(testContext); assertEquals(invokedInTx, instance.invoked()); }
Assert.notNull(testMethod, "Test method of supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); Assert.state(txContext == null, "Cannot start new transaction without ending existing transaction"); runBeforeTransactionMethods(testContext); txContext.startTransaction(); TransactionContextHolder.setCurrentTransactionContext(txContext);
private static TransactionContext requireCurrentTransactionContext() { TransactionContext txContext = TransactionContextHolder.getCurrentTransactionContext(); Assert.state(txContext != null, "TransactionContext is not active"); return txContext; }
@Test // SPR-13895 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 = BeanUtils.instantiateClass(clazz); given(testContext.getTestInstance()).willReturn(instance); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest")); assertFalse("callback should not have been invoked", 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")); } }
private static TransactionContext requireCurrentTransactionContext() { TransactionContext txContext = TransactionContextHolder.getCurrentTransactionContext(); Assert.state(txContext != null, "TransactionContext is not active"); return txContext; }
private void assertBeforeTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = BeanUtils.instantiateClass(clazz); given(testContext.getTestInstance()).willReturn(instance); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest")); assertFalse("callback should not have been invoked", instance.invoked()); TransactionContextHolder.removeCurrentTransactionContext(); listener.beforeTestMethod(testContext); assertFalse("callback should not have been invoked", instance.invoked()); }
/** * Determine whether a test-managed transaction is currently <em>active</em>. * @return {@code true} if a test-managed transaction is currently active * @see #start() * @see #end() */ public static boolean isActive() { TransactionContext transactionContext = TransactionContextHolder.getCurrentTransactionContext(); if (transactionContext != null) { TransactionStatus transactionStatus = transactionContext.getTransactionStatus(); return (transactionStatus != null && !transactionStatus.isCompleted()); } return false; }
private void assertAfterTestMethodWithNonTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = BeanUtils.instantiateClass(clazz); given(testContext.getTestInstance()).willReturn(instance); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("nonTransactionalTest")); assertFalse("callback should not have been invoked", instance.invoked()); TransactionContextHolder.removeCurrentTransactionContext(); listener.beforeTestMethod(testContext); listener.afterTestMethod(testContext); assertFalse("callback should not have been invoked", instance.invoked()); }
/** * Determine whether a test-managed transaction is currently <em>active</em>. * @return {@code true} if a test-managed transaction is currently active * @see #start() * @see #end() */ public static boolean isActive() { TransactionContext transactionContext = TransactionContextHolder.getCurrentTransactionContext(); if (transactionContext != null) { TransactionStatus transactionStatus = transactionContext.getTransactionStatus(); return (transactionStatus != null && !transactionStatus.isCompleted()); } return false; }
private void assertAfterTestMethodWithTransactionalTestMethod(Class<? extends Invocable> clazz) throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz); Invocable instance = BeanUtils.instantiateClass(clazz); given(testContext.getTestInstance()).willReturn(instance); given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("transactionalTest")); given(tm.getTransaction(BDDMockito.any(TransactionDefinition.class))).willReturn(new SimpleTransactionStatus()); assertFalse("callback should not have been invoked", instance.invoked()); TransactionContextHolder.removeCurrentTransactionContext(); listener.beforeTestMethod(testContext); assertFalse("callback should not have been invoked", instance.invoked()); listener.afterTestMethod(testContext); assertTrue("callback should have been invoked", instance.invoked()); }
/** * If a transaction is currently active for the supplied * {@linkplain TestContext test context}, this method will end the transaction * and run {@link AfterTransaction @AfterTransaction} methods. * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if * an error occurs while ending the transaction. */ @Override public void afterTestMethod(TestContext testContext) throws Exception { Method testMethod = testContext.getTestMethod(); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); // If there was (or perhaps still is) a transaction... if (txContext != null) { TransactionStatus transactionStatus = txContext.getTransactionStatus(); try { // If the transaction is still active... if (transactionStatus != null && !transactionStatus.isCompleted()) { txContext.endTransaction(); } } finally { runAfterTransactionMethods(testContext); } } }
/** * If a transaction is currently active for the supplied * {@linkplain TestContext test context}, this method will end the transaction * and run {@link AfterTransaction @AfterTransaction} methods. * <p>{@code @AfterTransaction} methods are guaranteed to be invoked even if * an error occurs while ending the transaction. */ @Override public void afterTestMethod(TestContext testContext) throws Exception { Method testMethod = testContext.getTestMethod(); Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext(); // If there was (or perhaps still is) a transaction... if (txContext != null) { TransactionStatus transactionStatus = txContext.getTransactionStatus(); try { // If the transaction is still active... if (transactionStatus != null && !transactionStatus.isCompleted()) { txContext.endTransaction(); } } finally { runAfterTransactionMethods(testContext); } } }