gpt4 book ai didi

java - Spring @ConfigurationProperties 绑定(bind)在测试中失败

转载 作者:行者123 更新时间:2023-11-30 06:20:25 25 4
gpt4 key购买 nike

我正在尝试在测试中从 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 上下文,请参阅 Can I manually load @ConfigurationProperties without the Spring AppContext?

关于java - Spring @ConfigurationProperties 绑定(bind)在测试中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48265981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com