Use new AssertJ exception assertions

This commit is contained in:
Sam Brannen
2022-05-31 14:08:28 +02:00
parent 9d324e59a0
commit 1beb7068f6
45 changed files with 554 additions and 803 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.stereotype.Repository;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Tests for PersistenceExceptionTranslationAdvisor's exception translation, as applied by
@@ -69,11 +70,11 @@ public class PersistenceExceptionTranslationAdvisorTests {
ri.throwsPersistenceException();
target.setBehavior(persistenceException1);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::noThrowsClause)
assertThatRuntimeException()
.isThrownBy(ri::noThrowsClause)
.isSameAs(persistenceException1);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::throwsPersistenceException)
assertThatRuntimeException()
.isThrownBy(ri::throwsPersistenceException)
.isSameAs(persistenceException1);
}
@@ -86,11 +87,11 @@ public class PersistenceExceptionTranslationAdvisorTests {
ri.throwsPersistenceException();
target.setBehavior(doNotTranslate);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::noThrowsClause)
assertThatRuntimeException()
.isThrownBy(ri::noThrowsClause)
.isSameAs(doNotTranslate);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::throwsPersistenceException)
assertThatRuntimeException()
.isThrownBy(ri::throwsPersistenceException)
.isSameAs(doNotTranslate);
}
@@ -123,12 +124,12 @@ public class PersistenceExceptionTranslationAdvisorTests {
RepositoryInterface ri = createProxy(target);
target.setBehavior(persistenceException1);
assertThatExceptionOfType(DataAccessException.class).isThrownBy(
ri::noThrowsClause)
assertThatExceptionOfType(DataAccessException.class)
.isThrownBy(ri::noThrowsClause)
.withCause(persistenceException1);
assertThatExceptionOfType(PersistenceException.class).isThrownBy(
ri::throwsPersistenceException)
assertThatExceptionOfType(PersistenceException.class)
.isThrownBy(ri::throwsPersistenceException)
.isSameAs(persistenceException1);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ 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.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Juergen Hoeller
@@ -57,20 +58,17 @@ public class TransactionSupportTests {
DefaultTransactionStatus status1 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
assertThat(status1.getTransaction() != null).as("Must have transaction").isTrue();
boolean condition2 = !status1.isNewTransaction();
assertThat(condition2).as("Must not be new transaction").isTrue();
assertThat(!status1.isNewTransaction()).as("Must not be new transaction").isTrue();
DefaultTransactionStatus status2 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
assertThat(status2.getTransaction() != null).as("Must have transaction").isTrue();
boolean condition1 = !status2.isNewTransaction();
assertThat(condition1).as("Must not be new transaction").isTrue();
assertThat(!status2.isNewTransaction()).as("Must not be new transaction").isTrue();
DefaultTransactionStatus status3 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
assertThat(status3.getTransaction() != null).as("Must have transaction").isTrue();
boolean condition = !status3.isNewTransaction();
assertThat(condition).as("Must not be new transaction").isTrue();
assertThat(!status3.isNewTransaction()).as("Must not be new transaction").isTrue();
}
@Test
@@ -181,8 +179,8 @@ public class TransactionSupportTests {
public void transactionTemplateWithException() {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
RuntimeException ex = new RuntimeException("Some application exception");
assertThatRuntimeException().isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
@@ -208,14 +206,9 @@ public class TransactionSupportTests {
}
};
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
}))
RuntimeException ex = new RuntimeException("Some application exception");
assertThatRuntimeException()
.isThrownBy(() -> template.executeWithoutResult(status -> { throw ex; }))
.isSameAs(tex);
assertThat(tm.begin).as("triggered begin").isTrue();
assertThat(tm.commit).as("no commit").isFalse();
@@ -227,13 +220,8 @@ public class TransactionSupportTests {
public void transactionTemplateWithError() {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
assertThatExceptionOfType(Error.class).isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw new Error("Some application error");
}
}));
assertThatExceptionOfType(Error.class)
.isThrownBy(() -> template.executeWithoutResult(status -> { throw new Error("Some application error"); }));
assertThat(tm.begin).as("triggered begin").isTrue();
assertThat(tm.commit).as("no commit").isFalse();
assertThat(tm.rollback).as("triggered rollback").isTrue();
@@ -247,23 +235,19 @@ public class TransactionSupportTests {
template.setTransactionManager(tm);
assertThat(template.getTransactionManager() == tm).as("correct transaction manager set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setPropagationBehaviorName("TIMEOUT_DEFAULT"));
assertThatIllegalArgumentException().isThrownBy(() -> template.setPropagationBehaviorName("TIMEOUT_DEFAULT"));
template.setPropagationBehaviorName("PROPAGATION_SUPPORTS");
assertThat(template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_SUPPORTS).as("Correct propagation behavior set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setPropagationBehavior(999));
assertThatIllegalArgumentException().isThrownBy(() -> template.setPropagationBehavior(999));
template.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY);
assertThat(template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY).as("Correct propagation behavior set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setIsolationLevelName("TIMEOUT_DEFAULT"));
assertThatIllegalArgumentException().isThrownBy(() -> template.setIsolationLevelName("TIMEOUT_DEFAULT"));
template.setIsolationLevelName("ISOLATION_SERIALIZABLE");
assertThat(template.getIsolationLevel() == TransactionDefinition.ISOLATION_SERIALIZABLE).as("Correct isolation level set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setIsolationLevel(999));
assertThatIllegalArgumentException().isThrownBy(() -> template.setIsolationLevel(999));
template.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
assertThat(template.getIsolationLevel() == TransactionDefinition.ISOLATION_REPEATABLE_READ).as("Correct isolation level set").isTrue();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ import org.springframework.transaction.testfixture.CallCountingTransactionManage
import org.springframework.transaction.testfixture.ReactiveCallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -161,8 +161,8 @@ public class AnnotationTransactionInterceptorTests {
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();
assertThatExceptionOfType(Exception.class).isThrownBy(
proxy::doSomethingElseWithCheckedException)
assertThatException()
.isThrownBy(proxy::doSomethingElseWithCheckedException)
.satisfies(ex -> assertGetTransactionAndCommitCount(1));
}
@@ -174,8 +174,8 @@ public class AnnotationTransactionInterceptorTests {
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();
assertThatExceptionOfType(Exception.class).isThrownBy(
proxy::doSomethingElseWithCheckedExceptionAndRollbackRule)
assertThatException()
.isThrownBy(proxy::doSomethingElseWithCheckedExceptionAndRollbackRule)
.satisfies(ex -> assertGetTransactionAndRollbackCount(1));
}

View File

@@ -45,7 +45,7 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.testfixture.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Tests demonstrating use of @EnableTransactionManagement @Configuration classes.
@@ -207,8 +207,8 @@ public class EnableTransactionManagementTests {
public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect() {
// 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))
assertThatException()
.isThrownBy(() -> new AnnotationConfigApplicationContext(EnableAspectjTxConfig.class, TxManagerConfig.class))
.withMessageContaining("AspectJJtaTransactionManagementConfiguration");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@ import org.springframework.transaction.support.TransactionSynchronization;
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.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Juergen Hoeller
@@ -58,7 +58,7 @@ public class TransactionalApplicationListenerAdapterTests {
TransactionPhase.BEFORE_COMMIT, p -> {throw ex;});
adapter.addCallback(callback);
assertThatExceptionOfType(RuntimeException.class)
assertThatRuntimeException()
.isThrownBy(() -> runInTransaction(() -> adapter.onApplicationEvent(event)))
.withMessage("event");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Stephane Nicoll
@@ -94,7 +94,7 @@ public class TransactionalApplicationListenerMethodAdapterTests {
TransactionalApplicationListenerMethodAdapter adapter = createTestInstance(m);
adapter.addCallback(callback);
assertThatExceptionOfType(RuntimeException.class)
assertThatRuntimeException()
.isThrownBy(() -> runInTransaction(() -> adapter.onApplicationEvent(event)))
.withMessage("event");