【发布时间】:2017-06-10 04:14:59
【问题描述】:
我正在使用 Spring Boot 1.4.3.RELEASE 并希望在运行测试时排除某些组件被扫描。
@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan(
basePackages = {"com.foobar"},
excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {AmazonKinesisRecordChecker.class, MyAmazonCredentials.class}))
public class ApplicationTests {
@Test
public void contextLoads() {
}
}
尽管有过滤器,但当我运行测试时,会加载不需要的组件并且 Spring Boot 崩溃,因为这些类需要 AWS 环境才能正常工作:
2017-01-25 16:02:49.234 ERROR 10514 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'amazonKinesisRecordChecker' defined in file
问题:如何使过滤器正常工作?
【问题讨论】:
-
@SpringBootTest(classes=> ) 。文档:docs.spring.io/spring-boot/docs/current/api/org/springframework/…
-
@Barath 你是说只有一种方法可以包含类,但不能在 Spring Boot Test 中排除它们?
-
不,我不是这么说的,您可以使用 WebMvcTest 排除过滤器 Doc :docs.spring.io/spring-boot/docs/current/api/org/springframework/…
-
@ComponentScan在测试类上不起作用也没有用。它仅适用于@Configuration类。除此之外,即使它会被接受,它也不会起作用。由于您的应用程序类上会有一个额外的@ComponentScan,因此一个会检测到该组件,而另一个则不会。结果相同。
标签: java spring spring-boot spring-annotations spring-test