@@ -5084,16 +5084,29 @@ potentially time consuming process of loading the context will only happen once.
5084
5084
[[boot-features-testing-spring-boot-applications-excluding-config]]
5085
5085
==== Excluding test configuration
5086
5086
If your application uses component scanning, for example if you use
5087
- `@SpringBootApplication` or `@ComponentScan`, you may find components or configurations
5087
+ `@SpringBootApplication` or `@ComponentScan`, you may find top-level configuration classes
5088
5088
created only for specific tests accidentally get picked up everywhere.
5089
5089
5090
- To help prevent this, Spring Boot provides `@TestComponent` and `@TestConfiguration`
5091
- annotations that can be used on classes in `src/test/java` to indicate that they should
5092
- not be picked up by scanning.
5090
+ As we <<boot-features-testing-spring-boot-applications-detecting-config,have seen above>>,
5091
+ `@TestConfiguration` can be used on an inner class of a test to customize the primary
5092
+ configuration. When placed on a top-level class, `@TestConfiguration` indicates that
5093
+ classes in `src/test/java` should not be picked up by scanning. You can then import that
5094
+ class explicitly where it is required:
5093
5095
5094
- NOTE: `@TestComponent` and `@TestConfiguration` are only needed on top level classes. If
5095
- you define `@Configuration` or `@Component` as inner-classes within a test (any class
5096
- that has `@Test` methods or `@RunWith`), they will be automatically filtered.
5096
+ [source,java,indent=0]
5097
+ ----
5098
+ @RunWith(SpringRunner.class)
5099
+ @SpringBootTest
5100
+ @Import(MyTestsConfiguration.class)
5101
+ public class MyTests {
5102
+
5103
+ @Test
5104
+ public void exampleTest() {
5105
+ ...
5106
+ }
5107
+
5108
+ }
5109
+ ----
5097
5110
5098
5111
NOTE: If you directly use `@ComponentScan` (i.e. not via `@SpringBootApplication`) you
5099
5112
will need to register the `TypeExcludeFilter` with it. See
0 commit comments