From 89b2f9cf54987aaf89652e7ab9db0efe704b253a Mon Sep 17 00:00:00 2001 From: Josh Cummings <3627351+jzheaux@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:13:49 -0600 Subject: [PATCH] Improve Test Runnability in IDE In some configurations, Configuration classes with static elements may cause a test to hang. This commit changes JeeConfigurerTests test configuration classes to use mock beans instead of referencing them as static fields. --- .../web/configurers/JeeConfigurerTests.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/JeeConfigurerTests.java b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/JeeConfigurerTests.java index 01dea8db6d..f30cd581bd 100644 --- a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/JeeConfigurerTests.java +++ b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/JeeConfigurerTests.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; import org.springframework.security.config.ObjectPostProcessor; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -34,6 +35,7 @@ import org.springframework.security.core.userdetails.AuthenticationUserDetailsSe import org.springframework.security.core.userdetails.User; import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers; import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; import org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource; import org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter; import org.springframework.test.web.servlet.MockMvc; @@ -64,18 +66,16 @@ public class JeeConfigurerTests { @Test public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eePreAuthenticatedProcessingFilter() { - ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class); this.spring.register(ObjectPostProcessorConfig.class).autowire(); - verify(ObjectPostProcessorConfig.objectPostProcessor) - .postProcess(any(J2eePreAuthenticatedProcessingFilter.class)); + ObjectPostProcessor objectPostProcessor = this.spring.getContext().getBean(ObjectPostProcessor.class); + verify(objectPostProcessor).postProcess(any(J2eePreAuthenticatedProcessingFilter.class)); } @Test public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eeBasedPreAuthenticatedWebAuthenticationDetailsSource() { - ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class); this.spring.register(ObjectPostProcessorConfig.class).autowire(); - verify(ObjectPostProcessorConfig.objectPostProcessor) - .postProcess(any(J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.class)); + ObjectPostProcessor objectPostProcessor = this.spring.getContext().getBean(ObjectPostProcessor.class); + verify(objectPostProcessor).postProcess(any(J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.class)); } @Test @@ -135,12 +135,14 @@ public class JeeConfigurerTests { public void requestWhenCustomAuthenticatedUserDetailsServiceInLambdaThenCustomAuthenticatedUserDetailsServiceUsed() throws Exception { this.spring.register(JeeCustomAuthenticatedUserDetailsServiceConfig.class).autowire(); + AuthenticationUserDetailsService userDetailsService = this.spring + .getContext() + .getBean(AuthenticationUserDetailsService.class); Principal user = mock(Principal.class); User userDetails = new User("user", "N/A", true, true, true, true, AuthorityUtils.createAuthorityList("ROLE_USER")); given(user.getName()).willReturn("user"); - given(JeeCustomAuthenticatedUserDetailsServiceConfig.authenticationUserDetailsService.loadUserDetails(any())) - .willReturn(userDetails); + given(userDetailsService.loadUserDetails(any())).willReturn(userDetails); // @formatter:off MockHttpServletRequestBuilder authRequest = get("/") .principal(user) @@ -157,7 +159,7 @@ public class JeeConfigurerTests { @EnableWebSecurity static class ObjectPostProcessorConfig { - static ObjectPostProcessor objectPostProcessor; + ObjectPostProcessor objectPostProcessor = spy(ReflectingObjectPostProcessor.class); @Bean SecurityFilterChain filterChain(HttpSecurity http) throws Exception { @@ -169,8 +171,9 @@ public class JeeConfigurerTests { } @Bean - static ObjectPostProcessor objectPostProcessor() { - return objectPostProcessor; + @Primary + ObjectPostProcessor objectPostProcessor() { + return this.objectPostProcessor; } } @@ -245,7 +248,7 @@ public class JeeConfigurerTests { @EnableWebSecurity public static class JeeCustomAuthenticatedUserDetailsServiceConfig { - static AuthenticationUserDetailsService authenticationUserDetailsService = mock( + private AuthenticationUserDetailsService authenticationUserDetailsService = mock( AuthenticationUserDetailsService.class); @Bean @@ -256,12 +259,17 @@ public class JeeConfigurerTests { .anyRequest().hasRole("USER") ) .jee((jee) -> jee - .authenticatedUserDetailsService(authenticationUserDetailsService) + .authenticatedUserDetailsService(this.authenticationUserDetailsService) ); return http.build(); // @formatter:on } + @Bean + AuthenticationUserDetailsService authenticationUserDetailsService() { + return this.authenticationUserDetailsService; + } + } }