Skip to content

Commit abdad1c

Browse files
committed
Constently use assertThatExceptionOf... assertions
Closes gh-37964
1 parent fe69930 commit abdad1c

File tree

20 files changed

+103
-95
lines changed

20 files changed

+103
-95
lines changed

buildSrc/src/test/java/org/springframework/boot/build/mavenplugin/PluginXmlParserTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.boot.build.mavenplugin.PluginXmlParser.Plugin;
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
28-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
28+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2929

3030
/**
3131
* Tests for {@link PluginXmlParser}.
@@ -50,9 +50,9 @@ void parseExistingDescriptorReturnPluginDescriptor() {
5050

5151
@Test
5252
void parseNonExistingFileThrowException() {
53-
assertThatThrownBy(() -> this.parser.parse(new File("src/test/resources/nonexistent.xml")))
54-
.isInstanceOf(RuntimeException.class)
55-
.hasCauseInstanceOf(FileNotFoundException.class);
53+
assertThatExceptionOfType(RuntimeException.class)
54+
.isThrownBy(() -> this.parser.parse(new File("src/test/resources/nonexistent.xml")))
55+
.withCauseInstanceOf(FileNotFoundException.class);
5656
}
5757

5858
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/orm/jpa/HibernateMetricsAutoConfigurationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
5151

5252
import static org.assertj.core.api.Assertions.assertThat;
53-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
53+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5454
import static org.mockito.BDDMockito.given;
5555
import static org.mockito.Mockito.mock;
5656

@@ -117,8 +117,8 @@ void entityManagerFactoryInstrumentationIsDisabledIfNotHibernateSessionFactory()
117117
.withUserConfiguration(NonHibernateEntityManagerFactoryConfiguration.class)
118118
.run((context) -> {
119119
// ensure EntityManagerFactory is not a Hibernate SessionFactory
120-
assertThatThrownBy(() -> context.getBean(EntityManagerFactory.class).unwrap(SessionFactory.class))
121-
.isInstanceOf(PersistenceException.class);
120+
assertThatExceptionOfType(PersistenceException.class)
121+
.isThrownBy(() -> context.getBean(EntityManagerFactory.class).unwrap(SessionFactory.class));
122122
MeterRegistry registry = context.getBean(MeterRegistry.class);
123123
assertThat(registry.find("hibernate.statements").meter()).isNull();
124124
});

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/graphql/GraphQlMetricsInstrumentationTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import org.springframework.boot.actuate.metrics.AutoTimer;
4444

4545
import static org.assertj.core.api.Assertions.assertThat;
46-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
46+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
4747
import static org.mockito.ArgumentMatchers.any;
4848
import static org.mockito.BDDMockito.given;
4949
import static org.mockito.Mockito.mock;
@@ -143,11 +143,9 @@ void shouldRecordDataFetchingMetricWhenError() throws Exception {
143143
DataFetcher<CompletionStage<String>> dataFetcher = mock(DataFetcher.class);
144144
given(dataFetcher.get(any())).willThrow(new IllegalStateException("test"));
145145
InstrumentationFieldFetchParameters fieldFetchParameters = mockFieldFetchParameters(false);
146-
147146
DataFetcher<?> instrumented = this.instrumentation.instrumentDataFetcher(dataFetcher, fieldFetchParameters);
148147
DataFetchingEnvironment environment = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().build();
149-
assertThatThrownBy(() -> instrumented.get(environment)).isInstanceOf(IllegalStateException.class);
150-
148+
assertThatIllegalArgumentException().isThrownBy(() -> instrumented.get(environment));
151149
Timer timer = this.registry.find("graphql.datafetcher").timer();
152150
assertThat(timer).isNotNull();
153151
assertThat(timer.count()).isEqualTo(1);

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientFilterFunctionTests.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import org.springframework.web.reactive.function.client.WebClient;
4444

4545
import static org.assertj.core.api.Assertions.assertThat;
46-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
46+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4747
import static org.mockito.BDDMockito.given;
4848
import static org.mockito.Mockito.mock;
4949

@@ -145,9 +145,10 @@ void filterWhenCancelThrownShouldRecordTimer() {
145145
.tags("method", "GET", "uri", "/projects/spring-boot", "status", "CLIENT_ERROR")
146146
.timer()
147147
.count()).isEqualTo(1);
148-
assertThatThrownBy(() -> this.registry.get("http.client.requests")
149-
.tags("method", "GET", "uri", "/projects/spring-boot", "status", "200")
150-
.timer()).isInstanceOf(MeterNotFoundException.class);
148+
assertThatExceptionOfType(MeterNotFoundException.class)
149+
.isThrownBy(() -> this.registry.get("http.client.requests")
150+
.tags("method", "GET", "uri", "/projects/spring-boot", "status", "200")
151+
.timer());
151152
}
152153

153154
@Test
@@ -162,9 +163,10 @@ void filterWhenCancelAfterResponseThrownShouldNotRecordTimer() {
162163
.tags("method", "GET", "uri", "/projects/spring-boot", "status", "200")
163164
.timer()
164165
.count()).isEqualTo(1);
165-
assertThatThrownBy(() -> this.registry.get("http.client.requests")
166-
.tags("method", "GET", "uri", "/projects/spring-boot", "status", "CLIENT_ERROR")
167-
.timer()).isInstanceOf(MeterNotFoundException.class);
166+
assertThatExceptionOfType(MeterNotFoundException.class)
167+
.isThrownBy(() -> this.registry.get("http.client.requests")
168+
.tags("method", "GET", "uri", "/projects/spring-boot", "status", "CLIENT_ERROR")
169+
.timer());
168170
}
169171

170172
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@
3131
import com.datastax.oss.driver.internal.core.session.throttling.RateLimitingRequestThrottler;
3232
import org.junit.jupiter.api.Test;
3333

34+
import org.springframework.beans.factory.BeanCreationException;
3435
import org.springframework.boot.autoconfigure.AutoConfigurations;
3536
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3637
import org.springframework.context.annotation.Bean;
3738
import org.springframework.context.annotation.Configuration;
3839

3940
import static org.assertj.core.api.Assertions.assertThat;
40-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
41+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4142

4243
/**
4344
* Tests for {@link CassandraAutoConfiguration}
@@ -217,9 +218,10 @@ void driverConfigLoaderUsePassThroughLimitingRequestThrottlerByDefault() {
217218
@Test
218219
void driverConfigLoaderWithRateLimitingRequiresExtraConfiguration() {
219220
this.contextRunner.withPropertyValues("spring.data.cassandra.request.throttler.type=rate-limiting")
220-
.run((context) -> assertThatThrownBy(() -> context.getBean(CqlSession.class))
221-
.hasMessageContaining("Error instantiating class RateLimitingRequestThrottler")
222-
.hasMessageContaining("No configuration setting found for key"));
221+
.run((context) -> assertThatExceptionOfType(BeanCreationException.class)
222+
.isThrownBy(() -> context.getBean(CqlSession.class))
223+
.withMessageContaining("Error instantiating class RateLimitingRequestThrottler")
224+
.withMessageContaining("No configuration setting found for key"));
223225
}
224226

225227
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationWithPasswordAuthenticationIntegrationTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
import org.testcontainers.junit.jupiter.Container;
3535
import org.testcontainers.junit.jupiter.Testcontainers;
3636

37+
import org.springframework.beans.factory.BeanCreationException;
3738
import org.springframework.boot.autoconfigure.AutoConfigurations;
3839
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3940
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
4041
import org.springframework.util.StreamUtils;
4142

4243
import static org.assertj.core.api.Assertions.assertThat;
43-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
44+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4445

4546
/**
4647
* Tests for {@link CassandraAutoConfiguration} that only uses password authentication.
@@ -79,8 +80,9 @@ void authenticationWithInvalidCredentials() {
7980
this.contextRunner
8081
.withPropertyValues("spring.data.cassandra.username=not-a-user",
8182
"spring.data.cassandra.password=invalid-password")
82-
.run((context) -> assertThatThrownBy(() -> context.getBean(CqlSession.class))
83-
.hasMessageContaining("Authentication error"));
83+
.run((context) -> assertThatExceptionOfType(BeanCreationException.class)
84+
.isThrownBy(() -> context.getBean(CqlSession.class))
85+
.withMessageContaining("Authentication error"));
8486
}
8587

8688
static final class PasswordAuthenticatorCassandraContainer extends CassandraContainer {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationPropertiesEnvironmentPostProcessorTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.springframework.core.io.Resource;
3535

3636
import static org.assertj.core.api.Assertions.assertThat;
37-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
37+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3838
import static org.mockito.Mockito.mock;
3939

4040
/**
@@ -69,11 +69,11 @@ void postProcessEnvironmentAddPropertySourceLast() {
6969
void registerIntegrationPropertiesPropertySourceWithUnknownResourceThrowsException() {
7070
ConfigurableEnvironment environment = new StandardEnvironment();
7171
ClassPathResource unknown = new ClassPathResource("does-not-exist.properties", getClass());
72-
assertThatThrownBy(() -> new IntegrationPropertiesEnvironmentPostProcessor()
73-
.registerIntegrationPropertiesPropertySource(environment, unknown))
74-
.isInstanceOf(IllegalStateException.class)
75-
.hasCauseInstanceOf(FileNotFoundException.class)
76-
.hasMessageContaining(unknown.toString());
72+
assertThatIllegalStateException()
73+
.isThrownBy(() -> new IntegrationPropertiesEnvironmentPostProcessor()
74+
.registerIntegrationPropertiesPropertySource(environment, unknown))
75+
.withCauseInstanceOf(FileNotFoundException.class)
76+
.withMessageContaining(unknown.toString());
7777
}
7878

7979
@Test

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
6565

6666
import static org.assertj.core.api.Assertions.assertThat;
67-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
67+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6868
import static org.mockito.Mockito.mock;
6969

7070
/**
@@ -328,10 +328,10 @@ void enableDefaultLeniency() {
328328
void disableDefaultLeniency() {
329329
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:false").run((context) -> {
330330
ObjectMapper mapper = context.getBean(ObjectMapper.class);
331-
assertThatThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class))
332-
.isInstanceOf(InvalidFormatException.class)
333-
.hasMessageContaining("expected format")
334-
.hasMessageContaining("yyyyMMdd");
331+
assertThatExceptionOfType(InvalidFormatException.class)
332+
.isThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class))
333+
.withMessageContaining("expected format")
334+
.withMessageContaining("yyyyMMdd");
335335
});
336336
}
337337

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.junit.jupiter.api.io.TempDir;
3737

3838
import org.springframework.beans.DirectFieldAccessor;
39+
import org.springframework.beans.factory.BeanCreationException;
3940
import org.springframework.beans.factory.annotation.Value;
4041
import org.springframework.beans.factory.config.BeanDefinition;
4142
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -51,7 +52,7 @@
5152
import org.springframework.util.FileSystemUtils;
5253

5354
import static org.assertj.core.api.Assertions.assertThat;
54-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
55+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5556

5657
/**
5758
* Tests for {@link EmbeddedMongoAutoConfiguration}.
@@ -82,8 +83,10 @@ void noVersion() {
8283
TestPropertyValues.of("spring.data.mongodb.port=0").applyTo(this.context);
8384
this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
8485
EmbeddedMongoAutoConfiguration.class);
85-
assertThatThrownBy(() -> this.context.refresh()).hasRootCauseExactlyInstanceOf(IllegalStateException.class)
86-
.hasRootCauseMessage("Set the spring.mongodb.embedded.version property or define your own MongodConfig "
86+
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> this.context.refresh())
87+
.withRootCauseExactlyInstanceOf(IllegalStateException.class)
88+
.havingRootCause()
89+
.withMessage("Set the spring.mongodb.embedded.version property or define your own MongodConfig "
8790
+ "bean to use embedded MongoDB");
8891
}
8992

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/ContextConsumerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.springframework.context.ApplicationContext;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2728
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
28-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2929
import static org.mockito.BDDMockito.given;
3030
import static org.mockito.BDDMockito.then;
3131
import static org.mockito.Mockito.inOrder;
@@ -59,8 +59,8 @@ void andThenNoInvokedIfThisFails() {
5959
given(predicate.test(24)).willReturn(false);
6060
ContextConsumer<ApplicationContext> firstConsumer = (context) -> assertThat(predicate.test(42)).isFalse();
6161
ContextConsumer<ApplicationContext> secondConsumer = (context) -> assertThat(predicate.test(24)).isFalse();
62-
assertThatThrownBy(() -> firstConsumer.andThen(secondConsumer).accept(mock(ApplicationContext.class)))
63-
.isInstanceOf(AssertionError.class);
62+
assertThatExceptionOfType(AssertionError.class)
63+
.isThrownBy(() -> firstConsumer.andThen(secondConsumer).accept(mock(ApplicationContext.class)));
6464
then(predicate).should().test(42);
6565
then(predicate).shouldHaveNoMoreInteractions();
6666
}

0 commit comments

Comments
 (0)