From df34d26f4f49202a04c88b9aa584e391a0f96499 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Wed, 8 Jul 2020 20:46:31 +0900 Subject: [PATCH] Replace ReflectionUtils with AssertJ's extracting() See gh-22261 --- .../SecurityAutoConfigurationTests.java | 26 +++++++------------ .../SessionAutoConfigurationTests.java | 24 +++++++---------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java index a59e8a88ae..faa2b37ee9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java @@ -16,10 +16,9 @@ package org.springframework.boot.autoconfigure.security.servlet; -import java.util.EnumSet; - import javax.servlet.DispatcherType; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -44,7 +43,6 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension; import org.springframework.security.web.FilterChainProxy; -import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -143,11 +141,9 @@ class SecurityAutoConfigurationTests { .run((context) -> { DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class); - @SuppressWarnings("unchecked") - EnumSet dispatcherTypes = (EnumSet) ReflectionTestUtils - .getField(bean, "dispatcherTypes"); - assertThat(dispatcherTypes).containsOnly(DispatcherType.ASYNC, DispatcherType.ERROR, - DispatcherType.REQUEST); + assertThat(bean) + .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class)) + .containsOnly(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST); }); } @@ -157,10 +153,9 @@ class SecurityAutoConfigurationTests { .withConfiguration(AutoConfigurations.of(SecurityFilterAutoConfiguration.class)).run((context) -> { DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class); - @SuppressWarnings("unchecked") - EnumSet dispatcherTypes = (EnumSet) ReflectionTestUtils - .getField(bean, "dispatcherTypes"); - assertThat(dispatcherTypes).containsOnly(DispatcherType.INCLUDE, DispatcherType.ERROR); + assertThat(bean) + .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class)) + .containsOnly(DispatcherType.INCLUDE, DispatcherType.ERROR); }); } @@ -170,10 +165,9 @@ class SecurityAutoConfigurationTests { .withConfiguration(AutoConfigurations.of(SecurityFilterAutoConfiguration.class)).run((context) -> { DelegatingFilterProxyRegistrationBean bean = context.getBean("securityFilterChainRegistration", DelegatingFilterProxyRegistrationBean.class); - @SuppressWarnings("unchecked") - EnumSet dispatcherTypes = (EnumSet) ReflectionTestUtils - .getField(bean, "dispatcherTypes"); - assertThat(dispatcherTypes).isEmpty(); + assertThat(bean) + .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class)) + .isEmpty(); }); } 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 efdaa2caa2..f187e328e5 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 @@ -17,10 +17,10 @@ package org.springframework.boot.autoconfigure.session; import java.util.Collections; -import java.util.EnumSet; import javax.servlet.DispatcherType; +import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.mockito.InOrder; @@ -41,7 +41,6 @@ import org.springframework.session.web.http.DefaultCookieSerializer; import org.springframework.session.web.http.HeaderHttpSessionIdResolver; import org.springframework.session.web.http.HttpSessionIdResolver; import org.springframework.session.web.http.SessionRepositoryFilter; -import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -112,13 +111,13 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest .run((context) -> assertThat(context.getBean(SessionProperties.class).getTimeout()).hasSeconds(3)); } - @SuppressWarnings("unchecked") @Test void filterIsRegisteredWithAsyncErrorAndRequestDispatcherTypes() { this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class).run((context) -> { FilterRegistrationBean registration = context.getBean(FilterRegistrationBean.class); assertThat(registration.getFilter()).isSameAs(context.getBean(SessionRepositoryFilter.class)); - assertThat((EnumSet) ReflectionTestUtils.getField(registration, "dispatcherTypes")) + assertThat(registration) + .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class)) .containsOnly(DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.REQUEST); }); } @@ -132,25 +131,25 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest }); } - @SuppressWarnings("unchecked") @Test void filterDispatcherTypesCanBeCustomized() { this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class) .withPropertyValues("spring.session.servlet.filter-dispatcher-types=error, request").run((context) -> { FilterRegistrationBean registration = context.getBean(FilterRegistrationBean.class); - assertThat((EnumSet) ReflectionTestUtils.getField(registration, "dispatcherTypes")) + assertThat(registration) + .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class)) .containsOnly(DispatcherType.ERROR, DispatcherType.REQUEST); }); } - @SuppressWarnings("unchecked") @Test void emptyFilterDispatcherTypesDoNotThrowException() { this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class) .withPropertyValues("spring.session.servlet.filter-dispatcher-types=").run((context) -> { FilterRegistrationBean registration = context.getBean(FilterRegistrationBean.class); - Object dispatcherTypes = ReflectionTestUtils.getField(registration, "dispatcherTypes"); - assertThat((EnumSet) dispatcherTypes).isEmpty(); + assertThat(registration) + .extracting("dispatcherTypes", InstanceOfAssertFactories.iterable(DispatcherType.class)) + .isEmpty(); }); } @@ -177,11 +176,8 @@ class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurationTest this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class) .withPropertyValues("server.port=0").run((context) -> { SessionRepositoryFilter filter = context.getBean(SessionRepositoryFilter.class); - CookieHttpSessionIdResolver sessionIdResolver = (CookieHttpSessionIdResolver) ReflectionTestUtils - .getField(filter, "httpSessionIdResolver"); - DefaultCookieSerializer cookieSerializer = (DefaultCookieSerializer) ReflectionTestUtils - .getField(sessionIdResolver, "cookieSerializer"); - assertThat(cookieSerializer).isSameAs(context.getBean(DefaultCookieSerializer.class)); + assertThat(filter).extracting("httpSessionIdResolver").extracting("cookieSerializer") + .isSameAs(context.getBean(DefaultCookieSerializer.class)); }); }