I can't figure out, how to exclude a configuration (e.g. as described here) in a test. What I really want is to ignore a configuration in a @WebMvcTest, but even the following simpler example does not work for me:
@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 { }
}
Why is the ExcludedBean
found in testExclusion()
?
How do I correctly exclude a configuration?