javaspringcomponent-scan

ComponentScan ignores excludeFilters (in test)


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?


Solution

  • The above test class will pass with the @Profile annotation to control the bean creation.

    @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 : Following code works on @ComponentScan

    Create a Configuration class and annotate with @ComponentScan as required

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

    and provide the ApplicationContext for the test class as follows

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