Skip to content

Commit a8860ba

Browse files
committed
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
1 parent 6564fb3 commit a8860ba

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5084,16 +5084,29 @@ potentially time consuming process of loading the context will only happen once.
50845084
[[boot-features-testing-spring-boot-applications-excluding-config]]
50855085
==== Excluding test configuration
50865086
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
50885088
created only for specific tests accidentally get picked up everywhere.
50895089

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:
50935095

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+
----
50975110

50985111
NOTE: If you directly use `@ComponentScan` (i.e. not via `@SpringBootApplication`) you
50995112
will need to register the `TypeExcludeFilter` with it. See

0 commit comments

Comments
 (0)