Skip to content

Commit 6e9bdac

Browse files
quaffsnicoll
authored andcommitted
Include WebMvcRegistrations beans in WebMvcTest
See gh-27823
1 parent 0fed29d commit 6e9bdac

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ include::{docs-java}/features/testing/springbootapplications/jsontests/MyJsonAss
368368
[[features.testing.spring-boot-applications.spring-mvc-tests]]
369369
==== Auto-configured Spring MVC Tests
370370
To test whether Spring MVC controllers are working as expected, use the `@WebMvcTest` annotation.
371-
`@WebMvcTest` auto-configures the Spring MVC infrastructure and limits scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`, `Converter`, `GenericConverter`, `Filter`, `HandlerInterceptor`, `WebMvcConfigurer`, and `HandlerMethodArgumentResolver`.
371+
`@WebMvcTest` auto-configures the Spring MVC infrastructure and limits scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`, `Converter`, `GenericConverter`, `Filter`, `HandlerInterceptor`, `WebMvcConfigurer`, `WebMvcRegistrations`, and `HandlerMethodArgumentResolver`.
372372
Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@WebMvcTest` annotation is used.
373373
`@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans.
374374

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

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.LinkedHashSet;
2222
import java.util.Set;
2323

24+
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
2425
import org.springframework.boot.context.TypeExcludeFilter;
2526
import org.springframework.boot.jackson.JsonComponent;
2627
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
@@ -43,6 +44,7 @@
4344
*
4445
* @author Phillip Webb
4546
* @author Madhura Bhave
47+
* @author Yanming Zhou
4648
* @since 2.2.1
4749
*/
4850
public final class WebMvcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<WebMvcTest> {
@@ -60,6 +62,7 @@ public final class WebMvcTypeExcludeFilter extends StandardAnnotationCustomizabl
6062
includes.add(ControllerAdvice.class);
6163
includes.add(JsonComponent.class);
6264
includes.add(WebMvcConfigurer.class);
65+
includes.add(WebMvcRegistrations.class);
6366
includes.add(javax.servlet.Filter.class);
6467
includes.add(FilterRegistrationBean.class);
6568
includes.add(DelegatingFilterProxyRegistrationBean.class);

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

+11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.Test;
2323
import org.thymeleaf.dialect.IDialect;
2424

25+
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
2526
import org.springframework.context.annotation.ComponentScan.Filter;
2627
import org.springframework.context.annotation.FilterType;
2728
import org.springframework.core.type.classreading.MetadataReader;
@@ -43,6 +44,7 @@
4344
* Tests for {@link WebMvcTypeExcludeFilter}.
4445
*
4546
* @author Phillip Webb
47+
* @author Yanming Zhou
4648
*/
4749
class WebMvcTypeExcludeFilterTests {
4850

@@ -55,6 +57,7 @@ void matchWhenHasNoControllers() throws Exception {
5557
assertThat(excludes(filter, Controller2.class)).isFalse();
5658
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
5759
assertThat(excludes(filter, ExampleWeb.class)).isFalse();
60+
assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse();
5861
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
5962
assertThat(excludes(filter, ExampleService.class)).isTrue();
6063
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
@@ -72,6 +75,7 @@ void matchWhenHasController() throws Exception {
7275
assertThat(excludes(filter, Controller2.class)).isTrue();
7376
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
7477
assertThat(excludes(filter, ExampleWeb.class)).isFalse();
78+
assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse();
7579
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
7680
assertThat(excludes(filter, ExampleService.class)).isTrue();
7781
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
@@ -89,6 +93,7 @@ void matchNotUsingDefaultFilters() throws Exception {
8993
assertThat(excludes(filter, Controller2.class)).isTrue();
9094
assertThat(excludes(filter, ExampleControllerAdvice.class)).isTrue();
9195
assertThat(excludes(filter, ExampleWeb.class)).isTrue();
96+
assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isTrue();
9297
assertThat(excludes(filter, ExampleMessageConverter.class)).isTrue();
9398
assertThat(excludes(filter, ExampleService.class)).isTrue();
9499
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
@@ -106,6 +111,7 @@ void matchWithIncludeFilter() throws Exception {
106111
assertThat(excludes(filter, Controller2.class)).isFalse();
107112
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
108113
assertThat(excludes(filter, ExampleWeb.class)).isFalse();
114+
assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse();
109115
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
110116
assertThat(excludes(filter, ExampleService.class)).isTrue();
111117
assertThat(excludes(filter, ExampleRepository.class)).isFalse();
@@ -121,6 +127,7 @@ void matchWithExcludeFilter() throws Exception {
121127
assertThat(excludes(filter, Controller2.class)).isFalse();
122128
assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse();
123129
assertThat(excludes(filter, ExampleWeb.class)).isFalse();
130+
assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse();
124131
assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse();
125132
assertThat(excludes(filter, ExampleService.class)).isTrue();
126133
assertThat(excludes(filter, ExampleRepository.class)).isTrue();
@@ -180,6 +187,10 @@ static class ExampleWeb implements WebMvcConfigurer {
180187

181188
}
182189

190+
static class ExampleWebMvcRegistrations implements WebMvcRegistrations {
191+
192+
}
193+
183194
static class ExampleMessageConverter extends MappingJackson2HttpMessageConverter {
184195

185196
}

0 commit comments

Comments
 (0)