Skip to content

Latest commit

 

History

History
21 lines (15 loc) · 1011 Bytes

File metadata and controls

21 lines (15 loc) · 1011 Bytes

41.3.2 排除测试配置

如果您的应用程序使用组件扫描,例如,如果使用了@SpringBootApplication@ComponentScan,您可能会发现仅为特定测试创建的顶级配置类意外地在任何地方都能被获取。

正如我们在前面看到的@TestConfiguration可用于一个测试的内部类以自定义主配置。当置于顶级类时,@TestConfiguration表明不应通过扫描选取src/test/java中的类。您可以在需要的位置显式导入该类:

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

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

}

如果直接使用@ComponentScan(即不通过@SpringBootApplication),则需要使用它注册TypeExcludeFilter。详细信息请参阅Javadoc