From 6bd6279eff0d00f701cfa0ad48f64c028603a401 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 20 Mar 2019 17:45:42 -0700 Subject: [PATCH] Document slice test behavior with @Configuration classes Closes gh-16274 --- .../main/asciidoc/spring-boot-features.adoc | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 74e2bae787..c85ea7018b 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -7185,6 +7185,10 @@ NOTE: `@WebFluxTest` cannot detect routes registered via the functional web fram testing `RouterFunction` beans in the context, consider importing your `RouterFunction` yourself via `@Import` or using `@SpringBootTest`. +NOTE: `@WebFluxTest` cannot detect custom security configuration registered via a `@Bean` +of type `SecurityWebFilterChain`. To include that in your test, you will need to import +the configuration that registers the bean via `@Import` or use `@SpringBootTest`. + TIP: Sometimes writing Spring WebFlux tests is not enough; Spring Boot can help you run <>. @@ -7813,6 +7817,34 @@ NOTE: Depending on the complexity of your application, you may either have a sin approach lets you enable it in one of your tests, if necessary, with the `@Import` annotation. +Test slices exclude `@Configuration` classes from scanning. For example, for a `@WebMvcTest`, +the following configuration will not include the given `WebMvcConfigurer` bean in the application +context loaded by the test slice: + +[source,java,indent=0] +---- + @Configuration + public class WebConfiguration { + @Bean + public WebMvcConfigurer testConfigurer() { + return new WebMvcConfigurer() { + ... + }; + } + } +---- + +The configuration below will, however, cause the custom `WebMvcConfigurer` to be loaded +by the test slice. + +[source,java,indent=0] +---- + @Component + public class TestWebMvcConfigurer extends WebMvcConfigurer { + ... + } +---- + Another source of confusion is classpath scanning. Assume that, while you structured your code in a sensible way, you need to scan an additional package. Your application may resemble the following code: