Migrate exception checking tests to use AssertJ

Migrate tests that use `@Test(expectedException=...)` or
`try...fail...catch` to use AssertJ's `assertThatException`
instead.
This commit is contained in:
Phillip Webb
2019-05-20 10:34:51 -07:00
parent fb26fc3f94
commit 02850f357f
561 changed files with 6592 additions and 10389 deletions

View File

@@ -31,8 +31,7 @@ import org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExcept
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.stereotype.Repository;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for PersistenceExceptionTranslationAdvisor's exception translation, as applied by
@@ -69,20 +68,12 @@ public class PersistenceExceptionTranslationAdvisorTests {
ri.throwsPersistenceException();
target.setBehavior(persistenceException1);
try {
ri.noThrowsClause();
fail();
}
catch (RuntimeException ex) {
assertSame(persistenceException1, ex);
}
try {
ri.throwsPersistenceException();
fail();
}
catch (RuntimeException ex) {
assertSame(persistenceException1, ex);
}
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::noThrowsClause)
.isSameAs(persistenceException1);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::throwsPersistenceException)
.isSameAs(persistenceException1);
}
@Test
@@ -94,20 +85,12 @@ public class PersistenceExceptionTranslationAdvisorTests {
ri.throwsPersistenceException();
target.setBehavior(doNotTranslate);
try {
ri.noThrowsClause();
fail();
}
catch (RuntimeException ex) {
assertSame(doNotTranslate, ex);
}
try {
ri.throwsPersistenceException();
fail();
}
catch (RuntimeException ex) {
assertSame(doNotTranslate, ex);
}
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::noThrowsClause)
.isSameAs(doNotTranslate);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::throwsPersistenceException)
.isSameAs(doNotTranslate);
}
@Test
@@ -139,25 +122,13 @@ public class PersistenceExceptionTranslationAdvisorTests {
RepositoryInterface ri = createProxy(target);
target.setBehavior(persistenceException1);
try {
ri.noThrowsClause();
fail();
}
catch (DataAccessException ex) {
// Expected
assertSame(persistenceException1, ex.getCause());
}
catch (PersistenceException ex) {
fail("Should have been translated");
}
assertThatExceptionOfType(DataAccessException.class).isThrownBy(
ri::noThrowsClause)
.withCause(persistenceException1);
try {
ri.throwsPersistenceException();
fail();
}
catch (PersistenceException ex) {
assertSame(persistenceException1, ex);
}
assertThatExceptionOfType(PersistenceException.class).isThrownBy(
ri::throwsPersistenceException)
.isSameAs(persistenceException1);
}

View File

@@ -23,7 +23,6 @@ import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
@@ -38,10 +37,10 @@ import org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.stereotype.Repository;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Rod Johnson
@@ -80,24 +79,15 @@ public class PersistenceExceptionTranslationPostProcessorTests {
assertTrue(AopUtils.isAopProxy(rwi2));
rwi2.additionalMethod(false);
checkWillTranslateExceptions(rwi2);
try {
rwi2.additionalMethod(true);
fail("Should have thrown DataAccessResourceFailureException");
}
catch (DataAccessResourceFailureException ex) {
assertEquals("my failure", ex.getMessage());
}
assertThatExceptionOfType(DataAccessResourceFailureException.class).isThrownBy(() ->
rwi2.additionalMethod(true))
.withMessage("my failure");
}
protected void checkWillTranslateExceptions(Object o) {
assertTrue(o instanceof Advised);
Advised a = (Advised) o;
for (Advisor advisor : a.getAdvisors()) {
if (advisor instanceof PersistenceExceptionTranslationAdvisor) {
return;
}
}
fail("No translation");
assertThat(o).isInstanceOf(Advised.class);
assertThat(((Advised) o).getAdvisors()).anyMatch(
PersistenceExceptionTranslationAdvisor.class::isInstance);
}

View File

@@ -22,6 +22,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.function.Consumer;
import org.junit.Test;
@@ -30,10 +31,11 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.TypeMismatchDataAccessException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
/**
* @author Juergen Hoeller
@@ -47,45 +49,21 @@ public class DataAccessUtilsTests {
assertNull(DataAccessUtils.uniqueResult(col));
try {
DataAccessUtils.requiredUniqueResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(0, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.requiredUniqueResult(col))
.satisfies(sizeRequirements(1, 0));
try {
DataAccessUtils.objectResult(col, String.class);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(0, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.objectResult(col, String.class))
.satisfies(sizeRequirements(1, 0));
try {
DataAccessUtils.intResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(0, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.intResult(col))
.satisfies(sizeRequirements(1, 0));
try {
DataAccessUtils.longResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(0, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.longResult(col))
.satisfies(sizeRequirements(1, 0));
}
@Test
@@ -94,55 +72,25 @@ public class DataAccessUtilsTests {
col.add("test1");
col.add("test2");
try {
DataAccessUtils.uniqueResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(2, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.uniqueResult(col))
.satisfies(sizeRequirements(1, 2));
try {
DataAccessUtils.requiredUniqueResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(2, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.requiredUniqueResult(col))
.satisfies(sizeRequirements(1, 2));
try {
DataAccessUtils.objectResult(col, String.class);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(2, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.objectResult(col, String.class))
.satisfies(sizeRequirements(1, 2));
try {
DataAccessUtils.intResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(2, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.intResult(col))
.satisfies(sizeRequirements(1, 2));
try {
DataAccessUtils.longResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(2, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.longResult(col))
.satisfies(sizeRequirements(1, 2));
}
@Test
@@ -180,15 +128,9 @@ public class DataAccessUtilsTests {
col.add(new Integer(5));
col.add(new Integer(5));
try {
DataAccessUtils.uniqueResult(col);
fail("Should have thrown IncorrectResultSizeDataAccessException");
}
catch (IncorrectResultSizeDataAccessException ex) {
// expected
assertEquals(1, ex.getExpectedSize());
assertEquals(2, ex.getActualSize());
}
assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
DataAccessUtils.uniqueResult(col))
.satisfies(sizeRequirements(1, 2));
}
@Test
@@ -213,21 +155,11 @@ public class DataAccessUtilsTests {
assertEquals("test1", DataAccessUtils.requiredUniqueResult(col));
assertEquals("test1", DataAccessUtils.objectResult(col, String.class));
try {
DataAccessUtils.intResult(col);
fail("Should have thrown TypeMismatchDataAccessException");
}
catch (TypeMismatchDataAccessException ex) {
// expected
}
assertThatExceptionOfType(TypeMismatchDataAccessException.class).isThrownBy(() ->
DataAccessUtils.intResult(col));
try {
DataAccessUtils.longResult(col);
fail("Should have thrown TypeMismatchDataAccessException");
}
catch (TypeMismatchDataAccessException ex) {
// expected
}
assertThatExceptionOfType(TypeMismatchDataAccessException.class).isThrownBy(() ->
DataAccessUtils.longResult(col));
}
@Test
@@ -241,21 +173,11 @@ public class DataAccessUtilsTests {
assertEquals(date, DataAccessUtils.objectResult(col, Date.class));
assertEquals(date.toString(), DataAccessUtils.objectResult(col, String.class));
try {
DataAccessUtils.intResult(col);
fail("Should have thrown TypeMismatchDataAccessException");
}
catch (TypeMismatchDataAccessException ex) {
// expected
}
assertThatExceptionOfType(TypeMismatchDataAccessException.class).isThrownBy(() ->
DataAccessUtils.intResult(col));
try {
DataAccessUtils.longResult(col);
fail("Should have thrown TypeMismatchDataAccessException");
}
catch (TypeMismatchDataAccessException ex) {
// expected
}
assertThatExceptionOfType(TypeMismatchDataAccessException.class).isThrownBy(() ->
DataAccessUtils.longResult(col));
}
@Test
@@ -274,6 +196,13 @@ public class DataAccessUtilsTests {
assertSame(out, DataAccessUtils.translateIfNecessary(in, mpet));
}
private <E extends IncorrectResultSizeDataAccessException> Consumer<E> sizeRequirements(
int expectedSize, int actualSize) {
return ex -> {
assertThat(ex.getExpectedSize()).as("expected size").isEqualTo(expectedSize);
assertThat(ex.getActualSize()).as("actual size").isEqualTo(actualSize);
};
}
public static class MapPersistenceExceptionTranslator implements PersistenceExceptionTranslator {

View File

@@ -21,6 +21,7 @@ import javax.resource.spi.ManagedConnectionFactory;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -36,9 +37,10 @@ import static org.mockito.Mockito.verify;
*/
public class LocalConnectionFactoryBeanTests {
@Test(expected = IllegalArgumentException.class)
@Test
public void testManagedConnectionFactoryIsRequired() throws Exception {
new LocalConnectionFactoryBean().afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(
new LocalConnectionFactoryBean()::afterPropertiesSet);
}
@Test

View File

@@ -39,6 +39,9 @@ import org.springframework.transaction.support.TransactionSynchronizationAdapter
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -273,7 +276,7 @@ public class JtaTransactionManagerTests {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatIllegalStateException().isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
@@ -281,12 +284,7 @@ public class JtaTransactionManagerTests {
TransactionSynchronizationManager.registerSynchronization(synch);
throw new IllegalStateException("I want a rollback");
}
});
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
@@ -305,19 +303,14 @@ public class JtaTransactionManagerTests {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
}
});
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(ut).setRollbackOnly();
@@ -361,7 +354,7 @@ public class JtaTransactionManagerTests {
ptm.setGlobalRollbackOnParticipationFailure(false);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatIllegalStateException().isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
@@ -369,12 +362,7 @@ public class JtaTransactionManagerTests {
TransactionSynchronizationManager.registerSynchronization(synch);
throw new IllegalStateException("I want a rollback");
}
});
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(synch).beforeCompletion();
@@ -705,18 +693,13 @@ public class JtaTransactionManagerTests {
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
@@ -733,18 +716,13 @@ public class JtaTransactionManagerTests {
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallbackWithoutResult() {
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}
});
fail("Should have thrown CannotCreateTransactionException");
}
catch (CannotCreateTransactionException ex) {
// expected
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(tm).resume(tx);
}
@@ -782,17 +760,12 @@ public class JtaTransactionManagerTests {
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(TransactionSuspensionNotSupportedException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
});
fail("Should have thrown TransactionSuspensionNotSupportedException");
}
catch (TransactionSuspensionNotSupportedException ex) {
// expected
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
@@ -801,7 +774,7 @@ public class JtaTransactionManagerTests {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
try {
assertThatExceptionOfType(InvalidIsolationLevelException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
@@ -811,11 +784,7 @@ public class JtaTransactionManagerTests {
// something transactional
}
});
fail("Should have thrown InvalidIsolationLevelException");
}
catch (InvalidIsolationLevelException ex) {
// expected
}
});
}
@Test
@@ -823,7 +792,7 @@ public class JtaTransactionManagerTests {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willThrow(new SystemException("system exception"));
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -832,11 +801,7 @@ public class JtaTransactionManagerTests {
// something transactional
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
});
}
@Test
@@ -864,7 +829,7 @@ public class JtaTransactionManagerTests {
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new NotSupportedException("not supported")).given(ut).begin();
try {
assertThatExceptionOfType(NestedTransactionNotSupportedException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
@@ -874,11 +839,7 @@ public class JtaTransactionManagerTests {
// something transactional
}
});
fail("Should have thrown NestedTransactionNotSupportedException");
}
catch (NestedTransactionNotSupportedException ex) {
// expected
}
});
}
@Test
@@ -887,7 +848,7 @@ public class JtaTransactionManagerTests {
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new UnsupportedOperationException("not supported")).given(ut).begin();
try {
assertThatExceptionOfType(NestedTransactionNotSupportedException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
@@ -897,11 +858,7 @@ public class JtaTransactionManagerTests {
// something transactional
}
});
fail("Should have thrown NestedTransactionNotSupportedException");
}
catch (NestedTransactionNotSupportedException ex) {
// expected
}
});
}
@Test
@@ -910,7 +867,7 @@ public class JtaTransactionManagerTests {
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
willThrow(new SystemException("system exception")).given(ut).begin();
try {
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -919,11 +876,7 @@ public class JtaTransactionManagerTests {
// something transactional
}
});
fail("Should have thrown CannotCreateTransactionException");
}
catch (CannotCreateTransactionException ex) {
// expected
}
});
}
@Test
@@ -933,7 +886,7 @@ public class JtaTransactionManagerTests {
Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new RollbackException("unexpected rollback")).given(ut).commit();
try {
assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -948,11 +901,7 @@ public class JtaTransactionManagerTests {
});
}
});
fail("Should have thrown UnexpectedRollbackException");
}
catch (UnexpectedRollbackException ex) {
// expected
}
});
verify(ut).begin();
}
@@ -1007,12 +956,7 @@ public class JtaTransactionManagerTests {
if (!outerTransactionBoundaryReached) {
tm.rollback(ts);
}
if (failEarly) {
assertFalse(outerTransactionBoundaryReached);
}
else {
assertTrue(outerTransactionBoundaryReached);
}
assertThat(outerTransactionBoundaryReached).isNotEqualTo(failEarly);
}
verify(ut).begin();
@@ -1031,7 +975,7 @@ public class JtaTransactionManagerTests {
Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new HeuristicMixedException("heuristic exception")).given(ut).commit();
try {
assertThatExceptionOfType(HeuristicCompletionException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -1046,12 +990,7 @@ public class JtaTransactionManagerTests {
});
}
});
fail("Should have thrown HeuristicCompletionException");
}
catch (HeuristicCompletionException ex) {
// expected
assertTrue(ex.getOutcomeState() == HeuristicCompletionException.STATE_MIXED);
}
}).satisfies(ex -> assertThat(ex.getOutcomeState()).isEqualTo(HeuristicCompletionException.STATE_MIXED));
verify(ut).begin();
}
@@ -1063,7 +1002,7 @@ public class JtaTransactionManagerTests {
Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new HeuristicRollbackException("heuristic exception")).given(ut).commit();
try {
assertThatExceptionOfType(HeuristicCompletionException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -1078,12 +1017,7 @@ public class JtaTransactionManagerTests {
});
}
});
fail("Should have thrown HeuristicCompletionException");
}
catch (HeuristicCompletionException ex) {
// expected
assertTrue(ex.getOutcomeState() == HeuristicCompletionException.STATE_ROLLED_BACK);
}
}).satisfies(ex -> assertThat(ex.getOutcomeState()).isEqualTo(HeuristicCompletionException.STATE_ROLLED_BACK));
verify(ut).begin();
}
@@ -1095,7 +1029,7 @@ public class JtaTransactionManagerTests {
Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new SystemException("system exception")).given(ut).commit();
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -1110,11 +1044,7 @@ public class JtaTransactionManagerTests {
});
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
});
verify(ut).begin();
}
@@ -1125,7 +1055,7 @@ public class JtaTransactionManagerTests {
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
willThrow(new SystemException("system exception")).given(ut).rollback();
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -1140,11 +1070,7 @@ public class JtaTransactionManagerTests {
status.setRollbackOnly();
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
});
verify(ut).begin();
}
@@ -1155,7 +1081,7 @@ public class JtaTransactionManagerTests {
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new IllegalStateException("no existing transaction")).given(ut).setRollbackOnly();
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -1164,11 +1090,7 @@ public class JtaTransactionManagerTests {
status.setRollbackOnly();
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
});
}
@Test
@@ -1177,7 +1099,7 @@ public class JtaTransactionManagerTests {
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new SystemException("system exception")).given(ut).setRollbackOnly();
try {
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@@ -1192,11 +1114,7 @@ public class JtaTransactionManagerTests {
});
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
}
});
}
@Test
@@ -1212,15 +1130,9 @@ public class JtaTransactionManagerTests {
// first commit
ptm.commit(status);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
// second commit attempt
ptm.commit(status);
fail("Should have thrown IllegalTransactionStateException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
// second commit attempt
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
ptm.commit(status));
verify(ut).begin();
verify(ut).commit();
}
@@ -1237,14 +1149,9 @@ public class JtaTransactionManagerTests {
// first rollback
ptm.rollback(status);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
// second rollback attempt
ptm.rollback(status);
fail("Should have thrown IllegalTransactionStateException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
// second rollback attempt
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
ptm.rollback(status));
verify(ut).begin();
verify(ut).rollback();
@@ -1262,14 +1169,9 @@ public class JtaTransactionManagerTests {
// first: rollback
ptm.rollback(status);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
// second: commit attempt
ptm.commit(status);
fail("Should have thrown IllegalTransactionStateException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
// second: commit attempt
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
ptm.commit(status));
verify(ut).begin();
verify(ut).rollback();

View File

@@ -25,12 +25,13 @@ import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Juergen Hoeller
@@ -50,13 +51,8 @@ public class TransactionSupportTests {
assertTrue("Must have transaction", status2.hasTransaction());
assertTrue("Must be new transaction", status2.isNewTransaction());
try {
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
fail("Should not have thrown NoTransactionException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)));
}
@Test
@@ -72,15 +68,10 @@ public class TransactionSupportTests {
assertTrue("Must have transaction", status2.getTransaction() != null);
assertTrue("Must not be new transaction", !status2.isNewTransaction());
try {
DefaultTransactionStatus status3 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
assertTrue("Must have transaction", status3.getTransaction() != null);
assertTrue("Must not be new transaction", !status3.isNewTransaction());
}
catch (NoTransactionException ex) {
fail("Should not have thrown NoTransactionException");
}
DefaultTransactionStatus status3 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
assertTrue("Must have transaction", status3.getTransaction() != null);
assertTrue("Must not be new transaction", !status3.isNewTransaction());
}
@Test
@@ -91,8 +82,8 @@ public class TransactionSupportTests {
assertTrue("triggered begin", tm.begin);
assertTrue("triggered commit", tm.commit);
assertTrue("no rollback", !tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
assertFalse("no rollback", tm.rollback);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@Test
@@ -102,9 +93,9 @@ public class TransactionSupportTests {
tm.rollback(status);
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertFalse("no commit", tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@Test
@@ -115,9 +106,9 @@ public class TransactionSupportTests {
tm.commit(status);
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertFalse("no commit", tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@Test
@@ -126,10 +117,10 @@ public class TransactionSupportTests {
TransactionStatus status = tm.getTransaction(null);
tm.commit(status);
assertTrue("no begin", !tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("no rollback", !tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
assertFalse("no begin", tm.begin);
assertFalse("no commit", tm.commit);
assertFalse("no rollback", tm.rollback);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@Test
@@ -138,9 +129,9 @@ public class TransactionSupportTests {
TransactionStatus status = tm.getTransaction(null);
tm.rollback(status);
assertTrue("no begin", !tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("no rollback", !tm.rollback);
assertFalse("no begin", tm.begin);
assertFalse("no commit", tm.commit);
assertFalse("no rollback", tm.rollback);
assertTrue("triggered rollbackOnly", tm.rollbackOnly);
}
@@ -151,9 +142,9 @@ public class TransactionSupportTests {
status.setRollbackOnly();
tm.commit(status);
assertTrue("no begin", !tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("no rollback", !tm.rollback);
assertFalse("no begin", tm.begin);
assertFalse("no commit", tm.commit);
assertFalse("no rollback", tm.rollback);
assertTrue("triggered rollbackOnly", tm.rollbackOnly);
}
@@ -169,8 +160,8 @@ public class TransactionSupportTests {
assertTrue("triggered begin", tm.begin);
assertTrue("triggered commit", tm.commit);
assertTrue("no rollback", !tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
assertFalse("no rollback", tm.rollback);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@Test
@@ -192,23 +183,18 @@ public class TransactionSupportTests {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
try {
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
});
fail("Should have propagated RuntimeException");
}
catch (RuntimeException caught) {
// expected
assertTrue("Correct exception", caught == ex);
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
}
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
}))
.isSameAs(ex);
assertTrue("triggered begin", tm.begin);
assertFalse("no commit", tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@SuppressWarnings("serial")
@@ -224,45 +210,35 @@ public class TransactionSupportTests {
};
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
try {
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
});
fail("Should have propagated RuntimeException");
}
catch (RuntimeException caught) {
// expected
assertTrue("Correct exception", caught == tex);
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
}
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
}))
.isSameAs(tex);
assertTrue("triggered begin", tm.begin);
assertFalse("no commit", tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@Test
public void transactionTemplateWithError() {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
try {
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw new Error("Some application error");
}
});
fail("Should have propagated Error");
}
catch (Error err) {
// expected
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
}
assertThatExceptionOfType(Error.class).isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw new Error("Some application error");
}
}));
assertTrue("triggered begin", tm.begin);
assertFalse("no commit", tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertFalse("no rollbackOnly", tm.rollbackOnly);
}
@Test
@@ -272,43 +248,24 @@ public class TransactionSupportTests {
template.setTransactionManager(tm);
assertTrue("correct transaction manager set", template.getTransactionManager() == tm);
try {
template.setPropagationBehaviorName("TIMEOUT_DEFAULT");
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
assertThatIllegalArgumentException().isThrownBy(() ->
template.setPropagationBehaviorName("TIMEOUT_DEFAULT"));
template.setPropagationBehaviorName("PROPAGATION_SUPPORTS");
assertTrue("Correct propagation behavior set", template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_SUPPORTS);
try {
template.setPropagationBehavior(999);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
assertThatIllegalArgumentException().isThrownBy(() ->
template.setPropagationBehavior(999));
template.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY);
assertTrue("Correct propagation behavior set", template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY);
try {
template.setIsolationLevelName("TIMEOUT_DEFAULT");
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
assertThatIllegalArgumentException().isThrownBy(() ->
template.setIsolationLevelName("TIMEOUT_DEFAULT"));
template.setIsolationLevelName("ISOLATION_SERIALIZABLE");
assertTrue("Correct isolation level set", template.getIsolationLevel() == TransactionDefinition.ISOLATION_SERIALIZABLE);
try {
template.setIsolationLevel(999);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
assertThatIllegalArgumentException().isThrownBy(() ->
template.setIsolationLevel(999));
template.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
assertTrue("Correct isolation level set", template.getIsolationLevel() == TransactionDefinition.ISOLATION_REPEATABLE_READ);
}

View File

@@ -30,10 +30,10 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Rob Harrop
@@ -79,14 +79,10 @@ public class TxNamespaceHandlerTests {
assertEquals("Should not have started another transaction", 1, ptm.begun);
// try with exceptional
try {
testBean.exceptional(new IllegalArgumentException("foo"));
fail("Should NEVER get here");
}
catch (Throwable throwable) {
assertEquals("Should have another started transaction", 2, ptm.begun);
assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
}
assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
testBean.exceptional(new IllegalArgumentException("foo")));
assertEquals("Should have another started transaction", 2, ptm.begun);
assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
}
@Test

View File

@@ -24,10 +24,12 @@ import org.springframework.tests.transaction.CallCountingTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Rob Harrop
@@ -134,21 +136,13 @@ public class AnnotationTransactionInterceptorTests {
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();
try {
proxy.doSomethingErroneous();
fail("Should throw IllegalStateException");
}
catch (IllegalStateException ex) {
assertGetTransactionAndRollbackCount(1);
}
assertThatIllegalStateException().isThrownBy(
proxy::doSomethingErroneous)
.satisfies(ex -> assertGetTransactionAndRollbackCount(1));
try {
proxy.doSomethingElseErroneous();
fail("Should throw IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
assertGetTransactionAndRollbackCount(2);
}
assertThatIllegalArgumentException().isThrownBy(
proxy::doSomethingElseErroneous)
.satisfies(ex -> assertGetTransactionAndRollbackCount(2));
}
@Test
@@ -159,13 +153,9 @@ public class AnnotationTransactionInterceptorTests {
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();
try {
proxy.doSomethingElseWithCheckedException();
fail("Should throw Exception");
}
catch (Exception ex) {
assertGetTransactionAndCommitCount(1);
}
assertThatExceptionOfType(Exception.class).isThrownBy(
proxy::doSomethingElseWithCheckedException)
.satisfies(ex -> assertGetTransactionAndCommitCount(1));
}
@Test
@@ -176,13 +166,9 @@ public class AnnotationTransactionInterceptorTests {
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();
try {
proxy.doSomethingElseWithCheckedExceptionAndRollbackRule();
fail("Should throw Exception");
}
catch (Exception ex) {
assertGetTransactionAndRollbackCount(1);
}
assertThatExceptionOfType(Exception.class).isThrownBy(
proxy::doSomethingElseWithCheckedExceptionAndRollbackRule)
.satisfies(ex -> assertGetTransactionAndRollbackCount(1));
}
@Test

View File

@@ -35,9 +35,9 @@ import org.springframework.tests.transaction.CallCountingTransactionManager;
import org.springframework.transaction.config.TransactionManagementConfigUtils;
import org.springframework.transaction.event.TransactionalEventListenerFactory;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Rob Harrop
@@ -78,14 +78,12 @@ public class AnnotationTransactionNamespaceHandlerTests {
assertEquals("Should not have started another transaction", 1, ptm.begun);
// try with exceptional
try {
testBean.exceptional(new IllegalArgumentException("foo"));
fail("Should NEVER get here");
}
catch (Throwable throwable) {
assertEquals("Should have another started transaction", 2, ptm.begun);
assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
}
assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
testBean.exceptional(new IllegalArgumentException("foo")))
.satisfies(ex -> {
assertEquals("Should have another started transaction", 2, ptm.begun);
assertEquals("Should have 1 rolled back transaction", 1, ptm.rollbacks);
});
}
@Test

View File

@@ -38,12 +38,11 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.config.TransactionManagementConfigUtils;
import org.springframework.transaction.event.TransactionalEventListenerFactory;
import static org.hamcrest.CoreMatchers.containsString;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests demonstrating use of @EnableTransactionManagement @Configuration classes.
@@ -118,14 +117,11 @@ public class EnableTransactionManagementTests {
@Test
@SuppressWarnings("resource")
public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect() {
try {
new AnnotationConfigApplicationContext(EnableAspectjTxConfig.class, TxManagerConfig.class);
fail("should have thrown CNFE when trying to load AnnotationTransactionAspect. " +
"Do you actually have org.springframework.aspects on the classpath?");
}
catch (Exception ex) {
assertThat(ex.getMessage(), containsString("AspectJJtaTransactionManagementConfiguration"));
}
// should throw CNFE when trying to load AnnotationTransactionAspect.
// Do you actually have org.springframework.aspects on the classpath?
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
new AnnotationConfigApplicationContext(EnableAspectjTxConfig.class, TxManagerConfig.class))
.withMessageContaining("AspectJJtaTransactionManagementConfiguration");
}
@Test

View File

@@ -46,9 +46,8 @@ import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.transaction.event.TransactionPhase.AFTER_COMMIT;
import static org.springframework.transaction.event.TransactionPhase.AFTER_COMPLETION;
import static org.springframework.transaction.event.TransactionPhase.AFTER_ROLLBACK;
@@ -95,17 +94,14 @@ public class TransactionalEventListenerTests {
@Test
public void immediatelyImpactsCurrentTransaction() {
load(ImmediateTestListener.class, BeforeCommitTestListener.class);
try {
this.transactionTemplate.execute(status -> {
getContext().publishEvent("FAIL");
fail("Should have thrown an exception at this point");
return null;
});
}
catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("Test exception"));
assertTrue(e.getMessage().contains(EventCollector.IMMEDIATELY));
}
assertThatIllegalStateException().isThrownBy(() ->
this.transactionTemplate.execute(status -> {
getContext().publishEvent("FAIL");
throw new AssertionError("Should have thrown an exception at this point");
}))
.withMessageContaining("Test exception")
.withMessageContaining(EventCollector.IMMEDIATELY);
getEventCollector().assertEvents(EventCollector.IMMEDIATELY, "FAIL");
getEventCollector().assertTotalEventsCount(1);
}
@@ -203,24 +199,19 @@ public class TransactionalEventListenerTests {
@Test
public void beforeCommitWithException() { // Validates the custom synchronization is invoked
load(BeforeCommitTestListener.class);
try {
this.transactionTemplate.execute(status -> {
TransactionSynchronizationManager.registerSynchronization(new EventTransactionSynchronization(10) {
@Override
public void beforeCommit(boolean readOnly) {
throw new IllegalStateException("test");
}
});
getContext().publishEvent("test");
getEventCollector().assertNoEventReceived();
return null;
assertThatIllegalStateException().isThrownBy(() ->
this.transactionTemplate.execute(status -> {
TransactionSynchronizationManager.registerSynchronization(new EventTransactionSynchronization(10) {
@Override
public void beforeCommit(boolean readOnly) {
throw new IllegalStateException("test");
}
});
getContext().publishEvent("test");
getEventCollector().assertNoEventReceived();
return null;
});
fail("Should have thrown an exception");
}
catch (IllegalStateException e) {
// Test exception - ignore
}
}));
getEventCollector().assertNoEventReceived(); // Before commit not invoked
}

View File

@@ -352,7 +352,7 @@ public abstract class AbstractReactiveTransactionAspectTests {
private void checkReactiveTransaction(boolean expected) {
Mono.subscriberContext().handle((context, sink) -> {
if (context.hasKey(TransactionContext.class) != expected){
if (context.hasKey(TransactionContext.class) != expected) {
fail("Should have thrown NoTransactionException");
}
sink.complete();

View File

@@ -34,6 +34,7 @@ import org.springframework.transaction.TransactionSystemException;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.interceptor.TransactionAspectSupport.TransactionInfo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
@@ -157,13 +158,8 @@ public abstract class AbstractTransactionAspectTests {
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
checkTransactionStatus(false);
try {
itb.exceptional(new OptimisticLockingFailureException(""));
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() ->
itb.exceptional(new OptimisticLockingFailureException("")));
checkTransactionStatus(false);
assertSame(txatt, ptm.getDefinition());

View File

@@ -42,11 +42,11 @@ import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -191,15 +191,11 @@ public class BeanFactoryTransactionTests {
*/
@Test
public void testNoTransactionAttributeSource() {
try {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("noTransactionAttributeSource.xml", getClass()));
bf.getBean("noTransactionAttributeSource");
fail("Should require TransactionAttributeSource to be set");
}
catch (FatalBeanException ex) {
// Ok
}
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("noTransactionAttributeSource.xml", getClass()));
bf.getBean("noTransactionAttributeSource");
});
}
/**

View File

@@ -22,6 +22,7 @@ import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
@@ -72,19 +73,22 @@ public class RollbackRuleTests {
assertTrue(rr.getDepth(new RuntimeException()) > 0);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void ctorArgMustBeAThrowableClassWithNonThrowableType() {
new RollbackRuleAttribute(StringBuffer.class);
assertThatIllegalArgumentException().isThrownBy(() ->
new RollbackRuleAttribute(StringBuffer.class));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void ctorArgMustBeAThrowableClassWithNullThrowableType() {
new RollbackRuleAttribute((Class<?>) null);
assertThatIllegalArgumentException().isThrownBy(() ->
new RollbackRuleAttribute((Class<?>) null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void ctorArgExceptionStringNameVersionWithNull() {
new RollbackRuleAttribute((String) null);
assertThatIllegalArgumentException().isThrownBy(() ->
new RollbackRuleAttribute((String) null));
}
}

View File

@@ -23,6 +23,7 @@ import org.junit.Test;
import org.springframework.transaction.TransactionDefinition;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -66,11 +67,12 @@ public class TransactionAttributeEditorTests {
assertTrue(!ta.isReadOnly());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testInvalidPropagationCodeOnly() {
TransactionAttributeEditor pe = new TransactionAttributeEditor();
// should have failed with bogus propagation code
pe.setAsText("XXPROPAGATION_REQUIRED");
assertThatIllegalArgumentException().isThrownBy(() ->
pe.setAsText("XXPROPAGATION_REQUIRED"));
}
@Test
@@ -83,11 +85,12 @@ public class TransactionAttributeEditorTests {
assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void testValidPropagationAndIsolationCodesAndInvalidRollbackRule() {
TransactionAttributeEditor pe = new TransactionAttributeEditor();
// should fail with bogus rollback rule
pe.setAsText("PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,XXX");
assertThatIllegalArgumentException().isThrownBy(() ->
pe.setAsText("PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,XXX"));
}
@Test

View File

@@ -22,6 +22,7 @@ import org.junit.Test;
import org.springframework.transaction.TransactionDefinition;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -49,9 +50,10 @@ public class TransactionAttributeSourceEditorTests {
assertNull(tas.getTransactionAttribute(m, null));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void invalidFormat() throws Exception {
editor.setAsText("foo=bar");
assertThatIllegalArgumentException().isThrownBy(() ->
editor.setAsText("foo=bar"));
}
@Test

View File

@@ -36,11 +36,11 @@ import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -109,18 +109,13 @@ public class WebSphereUowTransactionManagerTests {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY);
try {
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
return "result";
}
});
fail("Should have thrown IllegalTransactionStateException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
}));
}
@Test
@@ -357,24 +352,21 @@ public class WebSphereUowTransactionManagerTests {
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
try {
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
return "result";
}
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() ->
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
return "result";
}
}))
.withCauseInstanceOf(UOWException.class)
.satisfies(ex -> {
assertThat(ex.getRootCause()).isSameAs(rex);
assertThat(ex.getMostSpecificCause()).isSameAs(rex);
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException ex) {
// expected
assertTrue(ex.getCause() instanceof UOWException);
assertSame(rex, ex.getRootCause());
assertSame(rex, ex.getMostSpecificCause());
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
@@ -393,7 +385,7 @@ public class WebSphereUowTransactionManagerTests {
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
try {
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() ->
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
@@ -402,12 +394,7 @@ public class WebSphereUowTransactionManagerTests {
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
throw new OptimisticLockingFailureException("");
}
});
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
}));
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
@@ -489,18 +476,13 @@ public class WebSphereUowTransactionManagerTests {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
try {
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
return "result";
}
});
fail("Should have thrown IllegalTransactionStateException");
}
catch (IllegalTransactionStateException ex) {
// expected
}
}));
}
@Test
@@ -511,18 +493,13 @@ public class WebSphereUowTransactionManagerTests {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
try {
assertThatExceptionOfType(NestedTransactionNotSupportedException.class).isThrownBy(() ->
ptm.execute(definition, new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus status) {
return "result";
}
});
fail("Should have thrown NestedTransactionNotSupportedException");
}
catch (NestedTransactionNotSupportedException ex) {
// expected
}
}));
}
@Test

View File

@@ -28,11 +28,11 @@ import org.springframework.tests.sample.beans.DerivedTestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.tests.transaction.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Juergen Hoeller
@@ -58,23 +58,13 @@ public class SimpleTransactionScopeTests {
context.refresh();
try {
context.getBean(TestBean.class);
fail("Should have thrown BeanCreationException");
}
catch (BeanCreationException ex) {
// expected - no synchronization active
assertTrue(ex.getCause() instanceof IllegalStateException);
}
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
context.getBean(TestBean.class))
.withCauseInstanceOf(IllegalStateException.class);
try {
context.getBean(DerivedTestBean.class);
fail("Should have thrown BeanCreationException");
}
catch (BeanCreationException ex) {
// expected - no synchronization active
assertTrue(ex.getCause() instanceof IllegalStateException);
}
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
context.getBean(DerivedTestBean.class))
.withCauseInstanceOf(IllegalStateException.class);
TestBean bean1 = null;
DerivedTestBean bean2 = null;
@@ -113,23 +103,13 @@ public class SimpleTransactionScopeTests {
assertTrue(bean2b.wasDestroyed());
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
try {
context.getBean(TestBean.class);
fail("Should have thrown IllegalStateException");
}
catch (BeanCreationException ex) {
// expected - no synchronization active
assertTrue(ex.getCause() instanceof IllegalStateException);
}
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
context.getBean(TestBean.class))
.withCauseInstanceOf(IllegalStateException.class);
try {
context.getBean(DerivedTestBean.class);
fail("Should have thrown IllegalStateException");
}
catch (BeanCreationException ex) {
// expected - no synchronization active
assertTrue(ex.getCause() instanceof IllegalStateException);
}
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
context.getBean(DerivedTestBean.class))
.withCauseInstanceOf(IllegalStateException.class);
}
@Test