【发布时间】:2020-01-25 18:24:45
【问题描述】:
我不知道如何在测试中排除配置(例如described here)。我真正想要的是忽略@WebMvcTest 中的配置,但即使是以下更简单的示例也不适合我:
@ExtendWith(SpringExtension.class)
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
ComponentScanTest.ExcludedConfig.class }))
class ComponentScanTest {
@Autowired
private ApplicationContext applicationContext;
@Test
void testInclusion() throws Exception { // This test succeeds, no exception is thrown.
applicationContext.getBean(IncludedBean.class);
}
@Test
void testExclusion() throws Exception { // This test fails, because ExcludedBean is found.
assertThrows(NoSuchBeanDefinitionException.class, () -> applicationContext.getBean(ExcludedBean.class));
}
@Configuration
static class IncludedConfig {
@Bean
public IncludedBean includedBean() {
return new IncludedBean();
}
}
static class IncludedBean { }
@Configuration
static class ExcludedConfig {
@Bean
public ExcludedBean excludedBean() {
return new ExcludedBean();
}
}
static class ExcludedBean { }
}
为什么ExcludedBean 会出现在testExclusion() 中?
如何正确排除配置?
【问题讨论】:
标签: java spring component-scan