Skip to content

Commit 89b2f9c

Browse files
committed
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.
1 parent 0e39685 commit 89b2f9c

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/JeeConfigurerTests.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.annotation.Primary;
2728
import org.springframework.security.config.ObjectPostProcessor;
2829
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2930
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -34,6 +35,7 @@
3435
import org.springframework.security.core.userdetails.User;
3536
import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers;
3637
import org.springframework.security.web.SecurityFilterChain;
38+
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
3739
import org.springframework.security.web.authentication.preauth.j2ee.J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource;
3840
import org.springframework.security.web.authentication.preauth.j2ee.J2eePreAuthenticatedProcessingFilter;
3941
import org.springframework.test.web.servlet.MockMvc;
@@ -64,18 +66,16 @@ public class JeeConfigurerTests {
6466

6567
@Test
6668
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eePreAuthenticatedProcessingFilter() {
67-
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
6869
this.spring.register(ObjectPostProcessorConfig.class).autowire();
69-
verify(ObjectPostProcessorConfig.objectPostProcessor)
70-
.postProcess(any(J2eePreAuthenticatedProcessingFilter.class));
70+
ObjectPostProcessor<Object> objectPostProcessor = this.spring.getContext().getBean(ObjectPostProcessor.class);
71+
verify(objectPostProcessor).postProcess(any(J2eePreAuthenticatedProcessingFilter.class));
7172
}
7273

7374
@Test
7475
public void configureWhenRegisteringObjectPostProcessorThenInvokedOnJ2eeBasedPreAuthenticatedWebAuthenticationDetailsSource() {
75-
ObjectPostProcessorConfig.objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
7676
this.spring.register(ObjectPostProcessorConfig.class).autowire();
77-
verify(ObjectPostProcessorConfig.objectPostProcessor)
78-
.postProcess(any(J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.class));
77+
ObjectPostProcessor<Object> objectPostProcessor = this.spring.getContext().getBean(ObjectPostProcessor.class);
78+
verify(objectPostProcessor).postProcess(any(J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.class));
7979
}
8080

8181
@Test
@@ -135,12 +135,14 @@ public void requestWhenJeeMappableAuthoritiesInLambdaThenAuthenticatedWithMappab
135135
public void requestWhenCustomAuthenticatedUserDetailsServiceInLambdaThenCustomAuthenticatedUserDetailsServiceUsed()
136136
throws Exception {
137137
this.spring.register(JeeCustomAuthenticatedUserDetailsServiceConfig.class).autowire();
138+
AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> userDetailsService = this.spring
139+
.getContext()
140+
.getBean(AuthenticationUserDetailsService.class);
138141
Principal user = mock(Principal.class);
139142
User userDetails = new User("user", "N/A", true, true, true, true,
140143
AuthorityUtils.createAuthorityList("ROLE_USER"));
141144
given(user.getName()).willReturn("user");
142-
given(JeeCustomAuthenticatedUserDetailsServiceConfig.authenticationUserDetailsService.loadUserDetails(any()))
143-
.willReturn(userDetails);
145+
given(userDetailsService.loadUserDetails(any())).willReturn(userDetails);
144146
// @formatter:off
145147
MockHttpServletRequestBuilder authRequest = get("/")
146148
.principal(user)
@@ -157,7 +159,7 @@ public void requestWhenCustomAuthenticatedUserDetailsServiceInLambdaThenCustomAu
157159
@EnableWebSecurity
158160
static class ObjectPostProcessorConfig {
159161

160-
static ObjectPostProcessor<Object> objectPostProcessor;
162+
ObjectPostProcessor<Object> objectPostProcessor = spy(ReflectingObjectPostProcessor.class);
161163

162164
@Bean
163165
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
@@ -169,8 +171,9 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
169171
}
170172

171173
@Bean
172-
static ObjectPostProcessor<Object> objectPostProcessor() {
173-
return objectPostProcessor;
174+
@Primary
175+
ObjectPostProcessor<Object> objectPostProcessor() {
176+
return this.objectPostProcessor;
174177
}
175178

176179
}
@@ -245,7 +248,7 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
245248
@EnableWebSecurity
246249
public static class JeeCustomAuthenticatedUserDetailsServiceConfig {
247250

248-
static AuthenticationUserDetailsService authenticationUserDetailsService = mock(
251+
private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> authenticationUserDetailsService = mock(
249252
AuthenticationUserDetailsService.class);
250253

251254
@Bean
@@ -256,12 +259,17 @@ SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
256259
.anyRequest().hasRole("USER")
257260
)
258261
.jee((jee) -> jee
259-
.authenticatedUserDetailsService(authenticationUserDetailsService)
262+
.authenticatedUserDetailsService(this.authenticationUserDetailsService)
260263
);
261264
return http.build();
262265
// @formatter:on
263266
}
264267

268+
@Bean
269+
AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> authenticationUserDetailsService() {
270+
return this.authenticationUserDetailsService;
271+
}
272+
265273
}
266274

267275
}

0 commit comments

Comments
 (0)