ComponentScan 忽略 excludeFilters(在测试中)
ComponentScan ignores excludeFilters (in test)
我不知道如何在测试中排除配置(例如 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()
中?
如何正确排除配置?
上面的测试class将通过@Profile注解来控制bean的创建。
@ExtendWith(SpringExtension.class)
@ComponentScan
@ActiveProfiles("web")
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
@Profile("web")
static class IncludedConfig {
@Bean
public IncludedBean includedBean() {
return new IncludedBean();
}
}
static class IncludedBean {
}
@Configuration
@Profile("!web")
static class ExcludedConfig {
@Bean
public ExcludedBean excludedBean() {
return new ExcludedBean();
}
}
static class ExcludedBean {
}
}
Update :以下代码适用于 @ComponentScan
创建配置 class 并根据需要使用 @ComponentScan
注释
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
ComponentScanTest.ExcludedConfig.class }))
public class TestConfiguration {
}
并为测试class提供ApplicationContext如下
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes= {TestConfiguration.class})
class ComponentScanTest {
//.. Everything else remains the same.
}
我不知道如何在测试中排除配置(例如 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()
中?
如何正确排除配置?
上面的测试class将通过@Profile注解来控制bean的创建。
@ExtendWith(SpringExtension.class)
@ComponentScan
@ActiveProfiles("web")
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
@Profile("web")
static class IncludedConfig {
@Bean
public IncludedBean includedBean() {
return new IncludedBean();
}
}
static class IncludedBean {
}
@Configuration
@Profile("!web")
static class ExcludedConfig {
@Bean
public ExcludedBean excludedBean() {
return new ExcludedBean();
}
}
static class ExcludedBean {
}
}
Update :以下代码适用于 @ComponentScan
创建配置 class 并根据需要使用 @ComponentScan
注释
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
ComponentScanTest.ExcludedConfig.class }))
public class TestConfiguration {
}
并为测试class提供ApplicationContext如下
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes= {TestConfiguration.class})
class ComponentScanTest {
//.. Everything else remains the same.
}