【问题标题】:ComponentScan ignores excludeFilters (in test)ComponentScan 忽略 excludeFilters(在测试中)
【发布时间】: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


    【解决方案1】:

    上面的测试类会通过@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 {
        }
    }
    

    更新:以下代码适用于@ComponentScan

    创建一个配置类并根据需要使用@ComponentScan进行注释

    @Configuration
    @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
            ComponentScanTest.ExcludedConfig.class }))
    public class TestConfiguration {
    
    }
    

    并为测试类提供ApplicationContext如下

    @ExtendWith(SpringExtension.class)
    @ContextConfiguration(classes= {TestConfiguration.class})
    class ComponentScanTest {
      //.. Everything else remains the same.
    }
    

    【讨论】:

    • 谢谢!创建一个单独的TestConfiguration 将解决我的问题。
    猜你喜欢
    • 2018-06-14
    • 2014-10-22
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多