我不知道如何在测试中排除配置(例如,如此处 所述)。我真正想要的是忽略@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()
? 如何正确排除配置?