> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/43.-testing/43.3-testing-spring-boot-applications/43.3.3-excluding-test-configuration.md).

# 43.3.3 排除测试配置

如果应用使用组件扫描，比如`@SpringBootApplication`或`@ComponentScan`，你可能发现为测试类创建的顶级配置类在任何地方都可能偶然被扫描到。

向[上面看到](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications-detecting-config)的那样，为了自定义原始的配置，`@TestConfiguration`可以用在一个测试的内部类上。当把`@TestConfiguration`放在顶级类上时，`@TestConfiguration`表明在`src/test/java`中的类扫描无效。你可以在需要的地方，明确地导入那个类：

```java
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(MyTestsConfiguration.class)
public class MyTests {

    @Test
    public void exampleTest() {
        ...
    }

}
```

**注** 如果直接使用`@ComponentScan`（比如不通过`@SpringBootApplication`），你需要为它注册`TypeExcludeFilter`，具体参考[Javadoc](https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/api/org/springframework/boot/context/TypeExcludeFilter.html)。
