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

@@ -31,13 +31,14 @@ import com.datastax.oss.driver.internal.core.session.throttling.PassThroughReque
import com.datastax.oss.driver.internal.core.session.throttling.RateLimitingRequestThrottler;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 CassandraAutoConfiguration}
@@ -217,9 +218,10 @@ class CassandraAutoConfigurationTests {
@Test
void driverConfigLoaderWithRateLimitingRequiresExtraConfiguration() {
this.contextRunner.withPropertyValues("spring.data.cassandra.request.throttler.type=rate-limiting")
.run((context) -> assertThatThrownBy(() -> context.getBean(CqlSession.class))
.hasMessageContaining("Error instantiating class RateLimitingRequestThrottler")
.hasMessageContaining("No configuration setting found for key"));
.run((context) -> assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> context.getBean(CqlSession.class))
.withMessageContaining("Error instantiating class RateLimitingRequestThrottler")
.withMessageContaining("No configuration setting found for key"));
}
@Test

View File

@@ -34,13 +34,14 @@ import org.testcontainers.images.builder.Transferable;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
import org.springframework.util.StreamUtils;
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 CassandraAutoConfiguration} that only uses password authentication.
@@ -79,8 +80,9 @@ class CassandraAutoConfigurationWithPasswordAuthenticationIntegrationTests {
this.contextRunner
.withPropertyValues("spring.data.cassandra.username=not-a-user",
"spring.data.cassandra.password=invalid-password")
.run((context) -> assertThatThrownBy(() -> context.getBean(CqlSession.class))
.hasMessageContaining("Authentication error"));
.run((context) -> assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> context.getBean(CqlSession.class))
.withMessageContaining("Authentication error"));
}
static final class PasswordAuthenticatorCassandraContainer extends CassandraContainer {

View File

@@ -34,7 +34,7 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.Mockito.mock;
/**
@@ -69,11 +69,11 @@ class IntegrationPropertiesEnvironmentPostProcessorTests {
void registerIntegrationPropertiesPropertySourceWithUnknownResourceThrowsException() {
ConfigurableEnvironment environment = new StandardEnvironment();
ClassPathResource unknown = new ClassPathResource("does-not-exist.properties", getClass());
assertThatThrownBy(() -> new IntegrationPropertiesEnvironmentPostProcessor()
.registerIntegrationPropertiesPropertySource(environment, unknown))
.isInstanceOf(IllegalStateException.class)
.hasCauseInstanceOf(FileNotFoundException.class)
.hasMessageContaining(unknown.toString());
assertThatIllegalStateException()
.isThrownBy(() -> new IntegrationPropertiesEnvironmentPostProcessor()
.registerIntegrationPropertiesPropertySource(environment, unknown))
.withCauseInstanceOf(FileNotFoundException.class)
.withMessageContaining(unknown.toString());
}
@Test

View File

@@ -64,7 +64,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
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.mockito.Mockito.mock;
/**
@@ -328,10 +328,10 @@ class JacksonAutoConfigurationTests {
void disableDefaultLeniency() {
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:false").run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
assertThatThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class))
.isInstanceOf(InvalidFormatException.class)
.hasMessageContaining("expected format")
.hasMessageContaining("yyyyMMdd");
assertThatExceptionOfType(InvalidFormatException.class)
.isThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class))
.withMessageContaining("expected format")
.withMessageContaining("yyyyMMdd");
});
}

View File

@@ -36,6 +36,7 @@ import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -51,7 +52,7 @@ import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.FileSystemUtils;
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 EmbeddedMongoAutoConfiguration}.
@@ -82,8 +83,10 @@ class EmbeddedMongoAutoConfigurationTests {
TestPropertyValues.of("spring.data.mongodb.port=0").applyTo(this.context);
this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
EmbeddedMongoAutoConfiguration.class);
assertThatThrownBy(() -> this.context.refresh()).hasRootCauseExactlyInstanceOf(IllegalStateException.class)
.hasRootCauseMessage("Set the spring.mongodb.embedded.version property or define your own MongodConfig "
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> this.context.refresh())
.withRootCauseExactlyInstanceOf(IllegalStateException.class)
.havingRootCause()
.withMessage("Set the spring.mongodb.embedded.version property or define your own MongodConfig "
+ "bean to use embedded MongoDB");
}