如何通过spring-boot-test使用ApplicationContextRunner测试@ConfigurationProperties?

时间:2019-12-05 08:24:03

标签: spring-boot spring-boot-test

我需要测试使用@ConfigurationProperties bean的自动配置类。我正在使用https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-test-autoconfig中记录的ApplicationContextRunner来加快测试速度,并避免在每个变体之间启动servlet容器。但是,用@AutoconfigurationProperties注释的bean不会使用注入ApplicationContextRunner的值填充。

我怀疑我遇到的问题类似于https://stackoverflow.com/a/56023100/1484823

  

@ConfigurationProperties不受您在测试中构建的应用程序上下文的管理,尽管它们会在应用程序启动时加载,因为您的应用程序主类上具有@EnableConfigurationProperties。

如何通过ApplicationContextRunner启用对@ConfigurationProperties的支持?

这是对应的代码

    @Test
    void ServiceDefinitionMapperPropertiesAreProperlyLoaded() {
        ApplicationContextRunner contextRunner = new ApplicationContextRunner()
            .withConfiguration(AutoConfigurations.of(
                SingleServiceDefinitionAnswerAutoConfig.class,
                DynamicCatalogServiceAutoConfiguration.class
            ))
//          .withPropertyValues(DynamicCatalogProperties.OPT_IN_PROPERTY + "=true") //Not sure why this seems ignored
            .withSystemProperties(DynamicCatalogConstants.OPT_IN_PROPERTY + "=true",
                ServiceDefinitionMapperProperties.PROPERTY_PREFIX
                +ServiceDefinitionMapperProperties.SUFFIX_PROPERTY_KEY+ "=suffix")
        ;
        contextRunner.run(context -> {
            assertThat(context).hasSingleBean(ServiceDefinitionMapperProperties.class);
            ServiceDefinitionMapperProperties serviceDefinitionMapperProperties
                = context.getBean(ServiceDefinitionMapperProperties.class);
            assertThat(serviceDefinitionMapperProperties.getSuffix()).isEqualTo("suffix");
        });
    }

失败,并显示以下信息:

 org.opentest4j.AssertionFailedError: 
Expecting:
 <"">
to be equal to:
 <"suffix">
but was not.
Expected :suffix
Actual   :
<Click to see difference>
    at org.springframework.cloud.appbroker.autoconfigure.DynamicCatalogServiceAutoConfigurationTest
public class DynamicCatalogServiceAutoConfiguration {

[...]

    @Bean
    @ConfigurationProperties(prefix=ServiceDefinitionMapperProperties.PROPERTY_PREFIX, ignoreUnknownFields = false)
    public ServiceDefinitionMapperProperties serviceDefinitionMapperProperties() {
        return new ServiceDefinitionMapperProperties();
    }
[...]
}

可在https://github.com/orange-cloudfoundry/osb-cmdb-spike/blob/0da641e5f2f811f48b0676a25c8cbe97895168d1/spring-cloud-app-broker-autoconfigure/src/test/java/org/springframework/cloud/appbroker/autoconfigure/DynamicCatalogServiceAutoConfigurationTest.java#L89-L107

上找到完整的来源

ps:我正要向https://github.com/spring-projects/spring-boot/issues提交一个问题,以提出文档增强建议,以警告ApplicationContext中的此类限制,并寻求打开对@ConfigurationProperties的支持的方法。按照https://raw.githubusercontent.com/spring-projects/spring-boot/master/.github/ISSUE_TEMPLATE.md的指导,我首先要确保在这里我不会误解问题。

2 个答案:

答案 0 :(得分:1)

据我所知,测试中涉及的所有类均未启用配置属性绑定。结果,没有任何属性绑定到ServiceDefinitionMapperProperties。您可以使用@EnableConfigurationProperties启用配置属性绑定。添加它的典型位置是在DynamicCatalogServiceAutoConfiguration上,因为其serviceDefinitionMapperProperties bean依赖于启用的配置属性。

答案 1 :(得分:1)

如果作为测试的一部分要填充带有@ConfigurationProperties类注释的bean,并且通常依赖于带有@EnableConfigurationProperties注释的配置类来填充该bean,则可以创建一个琐碎的仅用于测试的配置类:

@ConfigurationProperties("app")
public class ConfigProps {
    private int meaningOfLife;

    public int getMeaningOfLife() { return meaningOfLife; }
    public void setMeaningOfLife(int meaning) { this.meaningOfLife = meaning; }
}

class ConfigPropsTest {

    private final ApplicationContextRunner runner = new ApplicationContextRunner();

    @EnableConfigurationProperties(ConfigProps.class)
    static class TrivialConfiguration {
    }

    @Test
    void test() {
        runner.withUserConfiguration(TrivialConfiguration.class)
            .withPropertyValues("app.meaning-of-life=42")
            .run(context -> {
                assertEquals(42, context.getBean(ConfigProps.class).getMeaningOfLife());
            });
    }

}

TrivialConfiguration传递到ApplicationContextRunner足以使其创建ConfigProps并使用可用属性进行填充。