简体   繁体   English

如何使用 Spring Boot 测试条件创建 Bean?

[英]How to test conditional creation of a Bean with Spring Boot?

In module AI have this class hierarchy:在模块 AI 中有这个 class 层次结构:

public interface Sanitizer<C extends DocumentContext> {
    CommonWorkflowResult<C> sanitizeForInbound(C documentContext);
    CommonWorkflowResult<C> sanitizeForOutbound(C documentContext);
}

An abstract base class implements this interface:一个抽象基 class 实现了这个接口:

public abstract class BaseSanitizer<C extends DocumentContext> implements Sanitizer<C> { ... }

The first subclass extending the base class:扩展基础 class 的第一个子类:

@Component
public class SaxXmlSanitizer<C extends DocumentContext> extends BaseSanitizer<C> { ... }

The second class extending the base class:第二个 class 扩展基础 class:

@Component
public class RegExXmlSanitizer<C extends DocumentContext> extends BaseSanitizer<C> { ... }

In an different module BI import the dependency (module A) containing the classes above and here I have a configuration defining the beans conditionally on a property being set via application.properties:在另一个模块 BI 中,导入包含上述类的依赖项(模块 A),这里我有一个配置,有条件地根据通过 application.properties 设置的属性定义 bean:

@Bean
@ConditionalOnProperty(name = "sanitizer.type", havingValue = "REGEX", matchIfMissing = true)
public Sanitizer regExSanitizer() {
    return new RegExXmlSanitizer();
}

@Bean
@ConditionalOnProperty(name = "sanitizer.type", havingValue = "SAX", matchIfMissing = false)
public Sanitizer xmlSanitizer() {
    return new SaxXmlSanitizer();
}

How can I write a Spring Boot test to see that a RegExXmlSanitizer Bean exists in the application context if the property sanitizerType=REGEX is set (and the SaxXmlSanitizer Bean does not exist) and vice versa if the property sanitizerType=SAX is set?如果设置了属性 sanitizerType=REGEX(并且 SaxXmlSanitizer Bean 不存在),我如何编写 Spring 引导测试以查看应用程序上下文中是否存在 RegExXmlSanitizer Bean,反之亦然如果设置了属性 sanitizerType=SAX?

I tried:我试过了:

@Test
void testSanitizerExists() {
    this.contextRunner//.withPropertyValues("sanitizerType=SAX")
            .run(context -> Assertions.assertNotNull(context.getBean(SaxXmlSanitizer.class)));
}

but I always get a但我总是得到一个

NoSuchBeanDefinitionException: No qualifying bean of type 'SaxXmlSanitizer' available: expected at least 1 bean which qualifies as autowire candidate. NoSuchBeanDefinitionException:没有可用的“SaxXmlSanitizer”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。

I got it running doing it this way:我让它这样运行:

@SpringBootTest(classes = {RegExXmlSanitizer.class}, properties = {"sanitizerType=REGEX"})
@ActiveProfiles("test")
class RegExXmlSanitizerTest {

    @Autowired
    private RegExXmlSanitizer<DocumentContext> sanitizer;

    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
            .withPropertyValues("sanitizerType=REGEX")
            .withConfiguration(AutoConfigurations.of(SanitizerTestConfig.class));

    @TestConfiguration
    static class SanitizerTestConfig {
        @Bean
        @ConditionalOnProperty(name = "sanitizerType", havingValue = "REGEX", matchIfMissing = true)
        public Sanitizer regExXmlSanitizer() {
            return new RegExXmlSanitizer();
        }
    }

    @Test
    void testSanitizerExists() {
        this.contextRunner.withPropertyValues("sanitizerType=SAX")
                .run(context -> Assertions.assertNotNull(context.getBean(RegExXmlSanitizer.class)));
    }
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM