Constently use assertThatExceptionOf... assertions

Closes gh-37964
This commit is contained in:
Phillip Webb
2023-10-19 20:17:26 -07:00
parent fe69930ac7
commit abdad1cabe
20 changed files with 103 additions and 95 deletions

View File

@@ -35,9 +35,9 @@ import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
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.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Tests for {@link SpringApplicationShutdownHook}.
@@ -163,8 +163,7 @@ class SpringApplicationShutdownHookTests {
ConfigurableApplicationContext context = new GenericApplicationContext();
shutdownHook.registerApplicationContext(context);
context.refresh();
assertThatThrownBy(() -> shutdownHook.deregisterFailedApplicationContext(context))
.isInstanceOf(IllegalStateException.class);
assertThatIllegalStateException().isThrownBy(() -> shutdownHook.deregisterFailedApplicationContext(context));
assertThat(shutdownHook.isApplicationContextRegistered(context)).isTrue();
}
@@ -174,7 +173,7 @@ class SpringApplicationShutdownHookTests {
GenericApplicationContext context = new GenericApplicationContext();
shutdownHook.registerApplicationContext(context);
context.registerBean(FailingBean.class);
assertThatThrownBy(context::refresh).isInstanceOf(BeanCreationException.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(context::refresh);
assertThat(shutdownHook.isApplicationContextRegistered(context)).isTrue();
shutdownHook.deregisterFailedApplicationContext(context);
assertThat(shutdownHook.isApplicationContextRegistered(context)).isFalse();

View File

@@ -27,7 +27,8 @@ import org.springframework.boot.context.metrics.buffering.StartupTimeline.Timeli
import org.springframework.core.metrics.StartupStep;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link BufferingApplicationStartup}.
@@ -86,8 +87,8 @@ class BufferingApplicationStartupTests {
void startRecordingShouldFailIfEventsWereRecorded() {
BufferingApplicationStartup applicationStartup = new BufferingApplicationStartup(2);
applicationStartup.start("first").end();
assertThatThrownBy(applicationStartup::startRecording).isInstanceOf(IllegalStateException.class)
.hasMessage("Cannot restart recording once steps have been buffered.");
assertThatIllegalStateException().isThrownBy(applicationStartup::startRecording)
.withMessage("Cannot restart recording once steps have been buffered.");
}
@Test
@@ -95,8 +96,8 @@ class BufferingApplicationStartupTests {
BufferingApplicationStartup applicationStartup = new BufferingApplicationStartup(2);
StartupStep step = applicationStartup.start("first");
step.end();
assertThatThrownBy(() -> step.tag("name", "value")).isInstanceOf(IllegalStateException.class)
.hasMessage("StartupStep has already ended.");
assertThatIllegalStateException().isThrownBy(() -> step.tag("name", "value"))
.withMessage("StartupStep has already ended.");
}
@Test
@@ -104,7 +105,8 @@ class BufferingApplicationStartupTests {
BufferingApplicationStartup applicationStartup = new BufferingApplicationStartup(2);
StartupStep step = applicationStartup.start("first");
step.tag("name", "value");
assertThatThrownBy(() -> step.getTags().iterator().remove()).isInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> step.getTags().iterator().remove());
}
@Test // gh-25792

View File

@@ -34,7 +34,7 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.jdbc.datasource.init.ScriptStatementFailedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for {@link DataSourceScriptDatabaseInitializer}.
@@ -82,7 +82,7 @@ class DataSourceScriptDatabaseInitializerTests
populator.setContinueOnError(false);
}
};
assertThatThrownBy(initializer::initializeDatabase).isInstanceOf(ScriptStatementFailedException.class);
assertThatExceptionOfType(ScriptStatementFailedException.class).isThrownBy(initializer::initializeDatabase);
}
@Override

View File

@@ -33,7 +33,7 @@ import org.springframework.context.support.StaticMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Integration tests for {@link MessageSourceMessageInterpolator}.
@@ -86,8 +86,8 @@ class MessageSourceMessageInterpolatorIntegrationTests {
@Test
void recursion() {
assertThatThrownBy(() -> validate("recursion"))
.hasStackTraceContaining("Circular reference '{recursion -> middle -> recursion}'");
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> validate("recursion"))
.withStackTraceContaining("Circular reference '{recursion -> middle -> recursion}'");
}
@Test

View File

@@ -77,7 +77,7 @@ import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Base for testing classes that extends {@link AbstractReactiveWebServerFactory}.
@@ -210,7 +210,8 @@ public abstract class AbstractReactiveWebServerFactoryTests {
}
protected void assertThatSslWithInvalidAliasCallFails(ThrowingCallable call) {
assertThatThrownBy(call).hasStackTraceContaining("Keystore does not contain specified alias 'test-alias-404'");
assertThatExceptionOfType(Exception.class).isThrownBy(call)
.withStackTraceContaining("Keystore does not contain specified alias 'test-alias-404'");
}
protected ReactorClientHttpConnector buildTrustAllSslConnector() {
@@ -399,8 +400,8 @@ public abstract class AbstractReactiveWebServerFactoryTests {
@Test
void whenSslIsEnabledAndNoKeyStoreIsConfiguredThenServerFailsToStart() {
assertThatThrownBy(() -> testBasicSslWithKeyStore(null, null))
.hasMessageContaining("Could not load key store 'null'");
assertThatExceptionOfType(Exception.class).isThrownBy(() -> testBasicSslWithKeyStore(null, null))
.withMessageContaining("Could not load key store 'null'");
}
@Test

View File

@@ -28,7 +28,6 @@ import org.junit.jupiter.params.provider.ValueSource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Tests for {@link PrivateKeyParser}.
@@ -75,12 +74,12 @@ class PrivateKeyParserTests {
})
// @formatter:on
void shouldNotParseUnsupportedTraditionalPkcs1(String file) {
assertThatThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs1/" + file))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Error loading private key file")
.hasCauseInstanceOf(IllegalStateException.class)
.getCause()
.hasMessageContaining("Unrecognized private key format");
assertThatIllegalStateException()
.isThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs1/" + file))
.withMessageContaining("Error loading private key file")
.withCauseInstanceOf(IllegalStateException.class)
.havingCause()
.withMessageContaining("Unrecognized private key format");
}
@ParameterizedTest
@@ -118,12 +117,12 @@ class PrivateKeyParserTests {
})
// @formatter:on
void shouldNotParseUnsupportedEcPkcs8(String file) {
assertThatThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs8/" + file))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Error loading private key file")
.hasCauseInstanceOf(IllegalStateException.class)
.getCause()
.hasMessageContaining("Unrecognized private key format");
assertThatIllegalStateException()
.isThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs8/" + file))
.withMessageContaining("Error loading private key file")
.withCauseInstanceOf(IllegalStateException.class)
.havingCause()
.withMessageContaining("Unrecognized private key format");
}
@EnabledForJreRange(min = JRE.JAVA_17, disabledReason = "EdDSA is only supported since Java 17")
@@ -191,12 +190,12 @@ class PrivateKeyParserTests {
})
// @formatter:on
void shouldNotParseUnsupportedEcSec1(String file) {
assertThatThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/sec1/" + file))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Error loading private key file")
.hasCauseInstanceOf(IllegalStateException.class)
.getCause()
.hasMessageContaining("Unrecognized private key format");
assertThatIllegalStateException()
.isThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/sec1/" + file))
.withMessageContaining("Error loading private key file")
.withCauseInstanceOf(IllegalStateException.class)
.havingCause()
.withMessageContaining("Unrecognized private key format");
}
@Test

View File

@@ -24,7 +24,7 @@ import java.security.KeyStoreException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link SslConfigurationValidator}.
@@ -65,17 +65,17 @@ class SslConfigurationValidatorTests {
@Test
void validateKeyAliasWhenAliasNotFoundShouldThrowException() {
assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(this.keyStore, INVALID_ALIAS))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Keystore does not contain specified alias '" + INVALID_ALIAS + "'");
assertThatIllegalStateException()
.isThrownBy(() -> SslConfigurationValidator.validateKeyAlias(this.keyStore, INVALID_ALIAS))
.withMessage("Keystore does not contain specified alias '" + INVALID_ALIAS + "'");
}
@Test
void validateKeyAliasWhenKeyStoreThrowsExceptionOnContains() throws KeyStoreException {
KeyStore uninitializedKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
assertThatThrownBy(() -> SslConfigurationValidator.validateKeyAlias(uninitializedKeyStore, "alias"))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Could not determine if keystore contains alias 'alias'");
assertThatIllegalStateException()
.isThrownBy(() -> SslConfigurationValidator.validateKeyAlias(uninitializedKeyStore, "alias"))
.withMessage("Could not determine if keystore contains alias 'alias'");
}
}

View File

@@ -148,7 +148,6 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@@ -482,7 +481,8 @@ public abstract class AbstractServletWebServerFactoryTests {
}
protected void assertThatSslWithInvalidAliasCallFails(ThrowingCallable call) {
assertThatThrownBy(call).hasStackTraceContaining("Keystore does not contain specified alias 'test-alias-404'");
assertThatExceptionOfType(Exception.class).isThrownBy(call)
.withStackTraceContaining("Keystore does not contain specified alias 'test-alias-404'");
}
@Test