From a8860ba7e9f024a490a629c78d008ba5022c6dca Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 10 Apr 2017 15:54:08 +0200 Subject: [PATCH] Clarify use of `@TestConfiguration` As `@TestComponent` is an implementation detail at this point really, this commit removes any reference to it from the doc. Closes gh-8421 --- .../main/asciidoc/spring-boot-features.adoc | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 8df80b2b482..e2c09eb2d82 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5084,16 +5084,29 @@ potentially time consuming process of loading the context will only happen once. [[boot-features-testing-spring-boot-applications-excluding-config]] ==== Excluding test configuration If your application uses component scanning, for example if you use -`@SpringBootApplication` or `@ComponentScan`, you may find components or configurations +`@SpringBootApplication` or `@ComponentScan`, you may find top-level configuration classes created only for specific tests accidentally get picked up everywhere. -To help prevent this, Spring Boot provides `@TestComponent` and `@TestConfiguration` -annotations that can be used on classes in `src/test/java` to indicate that they should -not be picked up by scanning. +As we <>, +`@TestConfiguration` can be used on an inner class of a test to customize the primary +configuration. When placed on a top-level class, `@TestConfiguration` indicates that +classes in `src/test/java` should not be picked up by scanning. You can then import that +class explicitly where it is required: -NOTE: `@TestComponent` and `@TestConfiguration` are only needed on top level classes. If -you define `@Configuration` or `@Component` as inner-classes within a test (any class -that has `@Test` methods or `@RunWith`), they will be automatically filtered. +[source,java,indent=0] +---- + @RunWith(SpringRunner.class) + @SpringBootTest + @Import(MyTestsConfiguration.class) + public class MyTests { + + @Test + public void exampleTest() { + ... + } + + } +---- NOTE: If you directly use `@ComponentScan` (i.e. not via `@SpringBootApplication`) you will need to register the `TypeExcludeFilter` with it. See