Skip to content

Commit eef6fdb

Browse files
committed
Include WebSecurityConfigurer beans in @WebMvcTest
Update `WebMvcTypeExcludeFilter` to include `WebSecurityConfigurer` beans. Fixes gh-12275
1 parent 00218db commit eef6fdb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.core.convert.converter.GenericConverter;
3434
import org.springframework.http.converter.HttpMessageConverter;
3535
import org.springframework.stereotype.Controller;
36+
import org.springframework.util.ClassUtils;
3637
import org.springframework.util.ObjectUtils;
3738
import org.springframework.web.bind.annotation.ControllerAdvice;
3839
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
@@ -45,6 +46,9 @@
4546
*/
4647
class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
4748

49+
private static final String[] OPTIONAL_INCLUDES = {
50+
"org.springframework.security.config.annotation.web.WebSecurityConfigurer" };
51+
4852
private static final Set<Class<?>> DEFAULT_INCLUDES;
4953

5054
static {
@@ -60,6 +64,13 @@ class WebMvcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter {
6064
includes.add(ErrorAttributes.class);
6165
includes.add(Converter.class);
6266
includes.add(GenericConverter.class);
67+
for (String optionalInclude : OPTIONAL_INCLUDES) {
68+
try {
69+
includes.add(ClassUtils.forName(optionalInclude, null));
70+
}
71+
catch (Exception ex) {
72+
}
73+
}
6374
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
6475
}
6576

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import org.springframework.core.type.classreading.MetadataReaderFactory;
2727
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
2828
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
29+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2930
import org.springframework.stereotype.Controller;
3031
import org.springframework.stereotype.Repository;
3132
import org.springframework.stereotype.Service;
@@ -54,6 +55,7 @@ public void matchWhenHasNoControllers() throws Exception {
5455
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
5556
assertThat(excludes(filter, ExampleService.class)).isTrue();
5657
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
58+
assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isFalse();
5759
}
5860

5961
@Test
@@ -67,6 +69,7 @@ public void matchWhenHasController() throws Exception {
6769
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
6870
assertThat(excludes(filter, ExampleService.class)).isTrue();
6971
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
72+
assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isFalse();
7073
}
7174

7275
@Test
@@ -80,6 +83,7 @@ public void matchNotUsingDefaultFilters() throws Exception {
8083
assertThat(excludes(filter, ExampleMessageConverter.class)).isTrue();
8184
assertThat(excludes(filter, ExampleService.class)).isTrue();
8285
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
86+
assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isTrue();
8387
}
8488

8589
@Test
@@ -106,6 +110,7 @@ public void matchWithExcludeFilter() throws Exception {
106110
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
107111
assertThat(excludes(filter, ExampleService.class)).isTrue();
108112
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
113+
assertThat(excludes(filter, ExampleWebSecurityConfigurer.class)).isFalse();
109114
}
110115

111116
private boolean excludes(WebMvcTypeExcludeFilter filter, Class<?> type)
@@ -173,4 +178,8 @@ static class ExampleRepository {
173178

174179
}
175180

181+
static class ExampleWebSecurityConfigurer extends WebSecurityConfigurerAdapter {
182+
183+
}
184+
176185
}

0 commit comments

Comments
 (0)