Java tutorial
/* * Copyright (C) 2013 Pascal Mazars * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package app.core; import org.junit.Assert; import org.junit.Test; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.IllegalTransactionStateException; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallback; /** * */ @TestExecutionListeners(TransactionalTestExecutionListener.class) public class TransactionNegativeTest extends TransactionTest { @Test public void testWeHaveAnExistingTransaction() { transactionTestHelper.throwExceptionIfNoExistingTransaction(); } @Test public void testRollbackOnException() { session = Db.getSession(); final TransactionStatus transactionStatus = getCurrentTransactionStatus(); // Assert.assertFalse(getCurrentTransaction().isActive()); new TransactionCallback<Object>() { @Override public Object doInTransaction(TransactionStatus transactionStatus) { try { // Assert.assertTrue(getCurrentTransaction().isActive()); session.save(new User("caca", "prout")); // Assert.assertTrue(getCurrentTransaction().isActive()); throw new RuntimeException("thrown on purpose. we want to check we get a rollback"); } finally { // Assert.assertTrue(getCurrentTransaction().isActive()); // Assert.assertTrue(getCurrentTransaction().wasRolledBack()); return null; } } }.doInTransaction(transactionStatus); //getCurrentTransaction().commit(); Assert.assertFalse(getCurrentTransaction().isActive()); // Assert.assertFalse(getCurrentTransaction().wasCommitted()); // Assert.assertTrue(getCurrentTransaction().wasRolledBack()); } }