From 6e9bdacc10d4035633c654d8833340be82935c67 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Fri, 27 Aug 2021 14:18:29 +0800 Subject: [PATCH] Include WebMvcRegistrations beans in WebMvcTest See gh-27823 --- .../src/docs/asciidoc/features/testing.adoc | 2 +- .../web/servlet/WebMvcTypeExcludeFilter.java | 3 +++ .../web/servlet/WebMvcTypeExcludeFilterTests.java | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index fbe8387f5b..98e955c03a 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -368,7 +368,7 @@ include::{docs-java}/features/testing/springbootapplications/jsontests/MyJsonAss [[features.testing.spring-boot-applications.spring-mvc-tests]] ==== Auto-configured Spring MVC Tests To test whether Spring MVC controllers are working as expected, use the `@WebMvcTest` annotation. -`@WebMvcTest` auto-configures the Spring MVC infrastructure and limits scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`, `Converter`, `GenericConverter`, `Filter`, `HandlerInterceptor`, `WebMvcConfigurer`, and `HandlerMethodArgumentResolver`. +`@WebMvcTest` auto-configures the Spring MVC infrastructure and limits scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`, `Converter`, `GenericConverter`, `Filter`, `HandlerInterceptor`, `WebMvcConfigurer`, `WebMvcRegistrations`, and `HandlerMethodArgumentResolver`. Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@WebMvcTest` annotation is used. `@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java index f2be9723b7..345edc1dc2 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilter.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations; import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.boot.jackson.JsonComponent; import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter; @@ -43,6 +44,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; * * @author Phillip Webb * @author Madhura Bhave + * @author Yanming Zhou * @since 2.2.1 */ public final class WebMvcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter { @@ -60,6 +62,7 @@ public final class WebMvcTypeExcludeFilter extends StandardAnnotationCustomizabl includes.add(ControllerAdvice.class); includes.add(JsonComponent.class); includes.add(WebMvcConfigurer.class); + includes.add(WebMvcRegistrations.class); includes.add(javax.servlet.Filter.class); includes.add(FilterRegistrationBean.class); includes.add(DelegatingFilterProxyRegistrationBean.class); diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java index ce8477be42..198ecf7295 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTypeExcludeFilterTests.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule; import org.junit.jupiter.api.Test; import org.thymeleaf.dialect.IDialect; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.FilterType; import org.springframework.core.type.classreading.MetadataReader; @@ -43,6 +44,7 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link WebMvcTypeExcludeFilter}. * * @author Phillip Webb + * @author Yanming Zhou */ class WebMvcTypeExcludeFilterTests { @@ -55,6 +57,7 @@ class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, Controller2.class)).isFalse(); assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse(); assertThat(excludes(filter, ExampleWeb.class)).isFalse(); + assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse(); assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); @@ -72,6 +75,7 @@ class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, Controller2.class)).isTrue(); assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse(); assertThat(excludes(filter, ExampleWeb.class)).isFalse(); + assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse(); assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); @@ -89,6 +93,7 @@ class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, Controller2.class)).isTrue(); assertThat(excludes(filter, ExampleControllerAdvice.class)).isTrue(); assertThat(excludes(filter, ExampleWeb.class)).isTrue(); + assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isTrue(); assertThat(excludes(filter, ExampleMessageConverter.class)).isTrue(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); @@ -106,6 +111,7 @@ class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, Controller2.class)).isFalse(); assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse(); assertThat(excludes(filter, ExampleWeb.class)).isFalse(); + assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse(); assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isFalse(); @@ -121,6 +127,7 @@ class WebMvcTypeExcludeFilterTests { assertThat(excludes(filter, Controller2.class)).isFalse(); assertThat(excludes(filter, ExampleControllerAdvice.class)).isFalse(); assertThat(excludes(filter, ExampleWeb.class)).isFalse(); + assertThat(excludes(filter, ExampleWebMvcRegistrations.class)).isFalse(); assertThat(excludes(filter, ExampleMessageConverter.class)).isFalse(); assertThat(excludes(filter, ExampleService.class)).isTrue(); assertThat(excludes(filter, ExampleRepository.class)).isTrue(); @@ -180,6 +187,10 @@ class WebMvcTypeExcludeFilterTests { } + static class ExampleWebMvcRegistrations implements WebMvcRegistrations { + + } + static class ExampleMessageConverter extends MappingJackson2HttpMessageConverter { }