From a5c56043b9a4504c4d6e23cb10b2433facc8c599 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Tue, 30 Nov 2021 18:35:09 +0100 Subject: [PATCH] Simplify AssertJ usage See gh-28862 --- .../CloudFoundryActuatorAutoConfigurationTests.java | 2 +- .../invoker/cache/CachingOperationInvokerTests.java | 4 ++-- .../client/MetricsWebClientCustomizerTests.java | 2 +- .../couchbase/CouchbaseAutoConfigurationTests.java | 3 ++- ...lasticsearchRestClientAutoConfigurationTests.java | 4 ++-- ...lasticsearchRestClientAutoConfigurationTests.java | 12 ++++++------ .../sendgrid/SendGridAutoConfigurationTests.java | 3 +-- .../session/SessionAutoConfigurationJdbcTests.java | 3 +-- .../session/SessionAutoConfigurationTests.java | 2 +- .../TransactionAutoConfigurationTests.java | 2 +- .../boot/builder/SpringApplicationBuilderTests.java | 3 +-- .../tomcat/TomcatReactiveWebServerFactoryTests.java | 3 +-- .../tomcat/TomcatServletWebServerFactoryTests.java | 3 +-- 13 files changed, 21 insertions(+), 25 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java index 2bc35b8b06..04f261b6cd 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java @@ -233,7 +233,7 @@ class CloudFoundryActuatorAutoConfigurationTests { ExposableWebEndpoint endpoint = endpoints.iterator().next(); assertThat(endpoint.getOperations()).hasSize(2); WebOperation webOperation = findOperationWithRequestPath(endpoint, "health"); - assertThat(webOperation).extracting("invoker").extracting("target") + assertThat(webOperation).extracting("invoker.target") .isInstanceOf(CloudFoundryHealthEndpointWebExtension.class); }); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoker/cache/CachingOperationInvokerTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoker/cache/CachingOperationInvokerTests.java index 6a8edada25..9141d123a1 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoker/cache/CachingOperationInvokerTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/invoker/cache/CachingOperationInvokerTests.java @@ -34,6 +34,7 @@ import org.springframework.boot.actuate.endpoint.OperationArgumentResolver; import org.springframework.boot.actuate.endpoint.SecurityContext; import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker; +import static org.assertj.core.api.Assertions.as; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.mockito.BDDMockito.given; @@ -115,8 +116,7 @@ class CachingOperationInvokerTests { Thread.sleep(10); } invokeWithUniquePrincipal(invoker); - assertThat(invoker).extracting("cachedResponses").asInstanceOf(InstanceOfAssertFactories.MAP) - .hasSizeLessThan(count); + assertThat(invoker).extracting("cachedResponses", as(InstanceOfAssertFactories.MAP)).hasSizeLessThan(count); } private void invokeWithUniquePrincipal(CachingOperationInvoker invoker) { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientCustomizerTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientCustomizerTests.java index b4dd2c1a33..894f0d5fa2 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientCustomizerTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/MetricsWebClientCustomizerTests.java @@ -58,7 +58,7 @@ class MetricsWebClientCustomizerTests { this.clientBuilder.filters((filters) -> assertThat(filters).hasSize(1)); this.customizer.customize(this.clientBuilder); this.clientBuilder.filters( - (filters) -> assertThat(filters).hasSize(1).first().isInstanceOf(MetricsWebClientFilterFunction.class)); + (filters) -> assertThat(filters).singleElement().isInstanceOf(MetricsWebClientFilterFunction.class)); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java index bec1f367bb..5a420e604c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java @@ -39,6 +39,7 @@ 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.as; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -80,7 +81,7 @@ class CouchbaseAutoConfigurationTests { expectedModuleIds.add(new JsonValueModule().getTypeId()); JsonSerializer serializer = env.jsonSerializer(); assertThat(serializer).extracting("wrapped").isInstanceOf(JacksonJsonSerializer.class) - .extracting("mapper").asInstanceOf(InstanceOfAssertFactories.type(ObjectMapper.class)) + .extracting("mapper", as(InstanceOfAssertFactories.type(ObjectMapper.class))) .extracting(ObjectMapper::getRegisteredModuleIds).isEqualTo(expectedModuleIds); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java index b0f0a7f31a..b63789d2d9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/elasticsearch/ReactiveElasticsearchRestClientAutoConfigurationTests.java @@ -276,8 +276,8 @@ class ReactiveElasticsearchRestClientAutoConfigurationTests { void whenMaxInMemorySizeIsConfiguredThenUnderlyingWebClientHasCustomMaxInMemorySize(String prefix) { this.contextRunner.withPropertyValues(prefix + "max-in-memory-size=1MB").run((context) -> { WebClient client = configureWebClient(context.getBean(ClientConfiguration.class).getClientConfigurers()); - assertThat(client).extracting("exchangeFunction").extracting("strategies").extracting("codecConfigurer") - .extracting("defaultCodecs").asInstanceOf(InstanceOfAssertFactories.type(DefaultCodecConfig.class)) + assertThat(client).extracting("exchangeFunction.strategies.codecConfigurer.defaultCodecs") + .asInstanceOf(InstanceOfAssertFactories.type(DefaultCodecConfig.class)) .extracting(DefaultCodecConfig::maxInMemorySize).isEqualTo(1024 * 1024); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java index 216f6f0773..deb6969d75 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java @@ -161,8 +161,8 @@ class ElasticsearchRestClientAutoConfigurationTests { RestClient client = context.getBean(RestHighLevelClient.class).getLowLevelClient(); assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) .containsExactly("http://localhost:9200"); - assertThat(client).extracting("client") - .extracting("credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)) + assertThat(client) + .extracting("client.credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)) .satisfies((credentialsProvider) -> { Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200)); assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user"); @@ -177,8 +177,8 @@ class ElasticsearchRestClientAutoConfigurationTests { RestClient client = context.getBean(RestHighLevelClient.class).getLowLevelClient(); assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) .containsExactly("http://localhost:9200"); - assertThat(client).extracting("client") - .extracting("credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)) + assertThat(client) + .extracting("client.credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)) .satisfies((credentialsProvider) -> { Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200)); assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user"); @@ -194,8 +194,8 @@ class ElasticsearchRestClientAutoConfigurationTests { RestClient client = context.getBean(RestHighLevelClient.class).getLowLevelClient(); assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString)) .containsExactly("http://localhost:9200", "http://localhost:9201"); - assertThat(client).extracting("client") - .extracting("credentialsProvider", + assertThat(client) + .extracting("client.credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)) .satisfies((credentialsProvider) -> { Credentials uriCredentials = credentialsProvider diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java index 5cd87fe1a8..9eb82afee3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/sendgrid/SendGridAutoConfigurationTests.java @@ -74,8 +74,7 @@ class SendGridAutoConfigurationTests { loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY", "spring.sendgrid.proxy.host:localhost", "spring.sendgrid.proxy.port:5678"); SendGrid sendGrid = this.context.getBean(SendGrid.class); - assertThat(sendGrid).extracting("client").extracting("httpClient").extracting("routePlanner") - .isInstanceOf(DefaultProxyRoutePlanner.class); + assertThat(sendGrid).extracting("client.httpClient.routePlanner").isInstanceOf(DefaultProxyRoutePlanner.class); } private void loadContext(String... environment) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java index 63667a1c70..9eecf4cd22 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationJdbcTests.java @@ -190,8 +190,7 @@ class SessionAutoConfigurationJdbcTests extends AbstractSessionAutoConfiguration JdbcIndexedSessionRepository repository = validateSessionRepository(context, JdbcIndexedSessionRepository.class); DataSource sessionDataSource = context.getBean("sessionDataSource", DataSource.class); - assertThat(repository).extracting("jdbcOperations").extracting("dataSource") - .isEqualTo(sessionDataSource); + assertThat(repository).extracting("jdbcOperations.dataSource").isEqualTo(sessionDataSource); assertThat(context.getBean(JdbcSessionDataSourceScriptDatabaseInitializer.class)) .hasFieldOrPropertyWithValue("dataSource", sessionDataSource); assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy( diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java index 25233f6144..ff8391ad3b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -160,7 +160,7 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class) .withPropertyValues("server.port=0").run((context) -> { SessionRepositoryFilter filter = context.getBean(SessionRepositoryFilter.class); - assertThat(filter).extracting("httpSessionIdResolver").extracting("cookieSerializer") + assertThat(filter).extracting("httpSessionIdResolver.cookieSerializer") .isSameAs(context.getBean(DefaultCookieSerializer.class)); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java index a4bb93a157..d796f44878 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/TransactionAutoConfigurationTests.java @@ -131,7 +131,7 @@ class TransactionAutoConfigurationTests { this.contextRunner.withUserConfiguration(SeveralPlatformTransactionManagersConfiguration.class) .run((context) -> { TransactionManagerCustomizers customizers = context.getBean(TransactionManagerCustomizers.class); - assertThat(customizers).extracting("customizers").asList().hasSize(1).first() + assertThat(customizers).extracting("customizers").asList().singleElement() .isInstanceOf(TransactionProperties.class); }); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index bc4566a56c..3e3f889902 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -304,8 +304,7 @@ class SpringApplicationBuilderTests { } }; SpringApplication application = applicationBuilder.build(); - assertThat(application).isInstanceOf(CustomSpringApplication.class) - .asInstanceOf(InstanceOfAssertFactories.type(CustomSpringApplication.class)) + assertThat(application).asInstanceOf(InstanceOfAssertFactories.type(CustomSpringApplication.class)) .satisfies((customApp) -> assertThat(customApp.resourceLoader).isEqualTo(resourceLoader)); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java index 5b6ff84e7a..ea122a439b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactoryTests.java @@ -102,8 +102,7 @@ class TomcatReactiveWebServerFactoryTests extends AbstractReactiveWebServerFacto void defaultTomcatListeners() { TomcatReactiveWebServerFactory factory = getFactory(); if (AprLifecycleListener.isAprAvailable()) { - assertThat(factory.getContextLifecycleListeners()).hasSize(1).first() - .isInstanceOf(AprLifecycleListener.class); + assertThat(factory.getContextLifecycleListeners()).singleElement().isInstanceOf(AprLifecycleListener.class); } else { assertThat(factory.getContextLifecycleListeners()).isEmpty(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java index 0a9b456e2b..eee73a9bbf 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactoryTests.java @@ -135,8 +135,7 @@ class TomcatServletWebServerFactoryTests extends AbstractServletWebServerFactory void defaultTomcatListeners() { TomcatServletWebServerFactory factory = getFactory(); if (AprLifecycleListener.isAprAvailable()) { - assertThat(factory.getContextLifecycleListeners()).hasSize(1).first() - .isInstanceOf(AprLifecycleListener.class); + assertThat(factory.getContextLifecycleListeners()).singleElement().isInstanceOf(AprLifecycleListener.class); } else { assertThat(factory.getContextLifecycleListeners()).isEmpty();