弹簧@ConfigurationProperties绑定在测试中失败



我正在尝试在测试中从application.yml加载influxDB属性。

@RunWith(SpringRunner.class)
@EnableConfigurationProperties(InfluxDBProperties.class)
public class StupidRepoIntegrationTest {
    @Autowired
    public InfluxDBProperties properties;
    @Test
    public void test() {
        System.out.println(stringify.apply(properties));
    }
    static Function<InfluxDBProperties, String> stringify = new Function<InfluxDBProperties, String>() {
        @Override
        public String apply(InfluxDBProperties influxDbProperties) {
            StringBuilder sb = new StringBuilder("InfluxDBProperties: [");
            sb.append("Url=").append(influxDbProperties.getUrl()).append(", ");
            sb.append("Database=").append(influxDbProperties.getDatabase()).append(", ");
            sb.append("Username=").append(influxDbProperties.getUsername()).append(", ");
            sb.append("Password=").append(influxDbProperties.getPassword()).append(", ");
            sb.append("RetentionPolicy=").append(influxDbProperties.getRetentionPolicy()).append(", ");
            sb.append("ConnectTimeout=").append(influxDbProperties.getConnectTimeout()).append(", ");
            sb.append("ReadTimeout=").append(influxDbProperties.getReadTimeout()).append(", ");
            sb.append("WriteTimeout=").append(influxDbProperties.getWriteTimeout()).append(", ");
            sb.append("GzipEnabled=").append(influxDbProperties.isGzip()).append("]");
            return sb.toString();
        }
    };
}
我有application.yml,

以及application-test.yml和application-integrationTest.yml,如下所示:

spring:
  influxdb:
    url: http://localhost:8086
    username: admin
    password: adminPass
    database: integrationTest
    retention-policy: autogen
    connect-timeout: 10
    read-timeout: 30
    write-timeout: 10
    gzip: true

执行测试时,出现以下异常:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    ...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.influxdb-org.springframework.data.influxdb.InfluxDBProperties': Could not bind properties to InfluxDBProperties (prefix=spring.influxdb, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 4 errors
Field error in object 'spring.influxdb' on field 'username': rejected value [null]; codes [NotEmpty.spring.influxdb.username,NotEmpty.username,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.username,username]; arguments []; default message [username]]; default message [may not be empty]
Field error in object 'spring.influxdb' on field 'url': rejected value [null]; codes [NotEmpty.spring.influxdb.url,NotEmpty.url,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.url,url]; arguments []; default message [url]]; default message [may not be empty]
Field error in object 'spring.influxdb' on field 'database': rejected value [null]; codes [NotEmpty.spring.influxdb.database,NotEmpty.database,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.database,database]; arguments []; default message [database]]; default message [may not be empty]
Field error in object 'spring.influxdb' on field 'retentionPolicy': rejected value [null]; codes [NotEmpty.spring.influxdb.retentionPolicy,NotEmpty.retentionPolicy,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.retentionPolicy,retentionPolicy]; arguments []; default message [retentionPolicy]]; default message [may not be empty]
    ...
    ... 48 common frames omitted
Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 4 errors
Field error in object 'spring.influxdb' on field 'username': rejected value [null]; codes [NotEmpty.spring.influxdb.username,NotEmpty.username,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.username,username]; arguments []; default message [username]]; default message [may not be empty]
Field error in object 'spring.influxdb' on field 'url': rejected value [null]; codes [NotEmpty.spring.influxdb.url,NotEmpty.url,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.url,url]; arguments []; default message [url]]; default message [may not be empty]
Field error in object 'spring.influxdb' on field 'database': rejected value [null]; codes [NotEmpty.spring.influxdb.database,NotEmpty.database,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.database,database]; arguments []; default message [database]]; default message [may not be empty]
Field error in object 'spring.influxdb' on field 'retentionPolicy': rejected value [null]; codes [NotEmpty.spring.influxdb.retentionPolicy,NotEmpty.retentionPolicy,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.influxdb.retentionPolicy,retentionPolicy]; arguments []; default message [retentionPolicy]]; default message [may not be empty]
    at org.springframework.boot.bind.PropertiesConfigurationFactory.checkForBindingErrors(PropertiesConfigurationFactory.java:359)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:276)
    at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:329)
    ... 66 common frames omitted

我应该更改什么以使 spring 在测试运行中拾取配置?

@SPringBootTest添加到您的测试中,

如果您不想加载 Spring 上下文,请参阅我可以在没有 Spring AppContext 的情况下手动加载@ConfigurationProperties吗?

最新更新