简体   繁体   中英

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

In module AI have this class hierarchy:

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

An abstract base class implements this interface:

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

The first subclass extending the base class:

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

The second class extending the base 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:

@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?

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.

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)));
    }
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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