如何从 Spring IT 中排除配置?

How to Exclude Configuration from Spring IT?

这似乎是一个非常简单的问题,尤其是因为它有时已经在我的应用程序中起作用了。我不知道为什么会这样,所以如果我错过了一些重要信息,请告诉我。

首先我有一个测试用例:

package org.acme.webportal.service;

@SpringBootTest(classes = {TestApplication.class})
public class ServiceImplTest

}

此测试有一个这样定义的应用程序:

package org.acme.webportal;

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
@PropertySource(value = {"classpath:application.properties"})
@EnableConfigurationProperties
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
    value = {DConfiguration.class})})
public class TestApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

此测试从测试中排除了如下配置:

package org.acme.webportal.security.ds;

@Configuration
public class DConfiguration {

}

据我所知,它运行良好。现在我要排除这个配置:

package org.acme.webportal.service.jobmanager.logic;

@Configuration
public class JConfiguration {

}

如何排除这个?

我试过了:


// just as the working exclusion above-> does not work
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {JConfiguration.class})})

// add it to the working excludeFilter -> does not work for JConfiguration, only for DConfiguration
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {DConfiguration.class, JConfiguration.class})})

// add it to the working excludeFilters -> does not work for JConfiguration, only for DConfiguration
@ComponentScan(excludeFilters = {
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {DConfiguration.class}),
    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {JConfiguration.class})
})

// maybe as a pattern? the pattern might be wrong, the config is still present
@ComponentScan.Filter(type = FilterType.REGEX, pattern = {"org\.acme\.webportal\.service\.jobmanager\.logic.*"})

// does not work because it's no auto configuration
@SpringBootApplication(exclude = {JConfiguration.class})

// does not work because it's no auto configuration
@EnableAutoConfiguration(exclude = {JConfiguration.class})

我知道这不起作用的原因是因为此消息不断弹出:

The bean 'mapUploadJobLauncher', defined in class path resource [org/acme/webportal/service/jobmanager/logic/JConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/acme/webportal/service/jobmanager/logic/MockJConfiguration.class] and overriding is disabled.

(这意味着我专门为测试定义的模拟配置与生产配置冲突,我真的想排除它。)

我知道 Spring Boot 对包的处理确实变化无常,但我完全不知道我在这里做错了什么。如何从 IT 中排除配置?

所以在这种情况下的问题是附近的应用程序相互导入,但不是以有意义的方式。

有第二个应用程序没有排除,所以 Spring 认为它可以添加排除的 class 无论如何。我想要基于功能/包的 Spring 测试应用程序,这似乎是不可能的。我将它们合并到一个大应用程序中,现在排除正常工作了。