Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 组件扫描忽略排除过滤器(测试中)_Java_Spring_Component Scan - Fatal编程技术网

Java 组件扫描忽略排除过滤器(测试中)

Java 组件扫描忽略排除过滤器(测试中),java,spring,component-scan,Java,Spring,Component Scan,我不知道如何排除配置,例如在测试中。我真正想要的是忽略@WebMvcTest中的配置,但即使是下面的简单示例也不适用于我: @ExtendWith(SpringExtension.class) @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = { ComponentScanTest.ExcludedConfig.class })) c

我不知道如何排除配置,例如在测试中。我真正想要的是忽略@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 { }
}
为什么在testExclusion中找到ExcludedBean?
如何正确排除配置?

上述测试类将通过@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.
}

非常感谢。创建一个单独的测试配置将解决我的问题。