测试执行失败,因为失败配置负载
问题描述:
我想测试我的应用程序和运行到麻烦:测试执行失败,因为失败配置负载
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MainConfiguration.class)
public class ProductionDataRestClientTest {
@Test
public void testGetProductionDataAsync() {
}
}
的MainConfiguration类如下:
@Data
@Configuration
@Slf4j
@PropertySource(value = {
"classpath:/conf/application.properties", "classpath:/conf/application.build.properties",
"file:${configPath}/application.properties" }, ignoreResourceNotFound = true)
public class MainConfiguration {
@Value("${app.configName}")
private String configName;
@Value("${project.name}")
private String appName;
@Value("${project.version}")
private String appVersion;
@Value("${app.historySize}")
private int historySize;
@Value("${app.processOnlyVisibleEnvironments}")
private Boolean processOnlyVisibleEnvironments;
@PostConstruct
private void logConfig() {
appName = WordUtils.capitalize(appName);
log.info("constructed config: " + configName);
log.info(this.toString());
}
}
和应用.properties文件在这里:
app.configName=Internal Classpath
app.historySize=25
app.processOnlyVisibleEnvironments=true
client.timeout.connect=30000
client.timeout.read=30000
threading.pool.size.environmentProcessing=5
threading.pool.size.webServiceRequesting=10
我的问题是,春天似乎不解决吨他值标识符作为正常执行,所以下面的堆栈跟踪抛出:
Caused by: java.lang.NumberFormatException: For input string: "${app.historySize}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.valueOf(Integer.java:766)
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:194)
at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:464)
at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:437)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:195)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:125)
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:61)
... 47 more
我没有做任何线索,我尝试了很多不同的东西,毫无效果。
感谢提前:)
答
有解决这个问题的一个非常简单的方法:
// To resolve ${} in @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
这必须加载配置文件的方法,并帮助解决占位符。
感谢您的帮助;)
有没有任何其他特定propertie文件(例如的 “文件:$ {}用configPath /application.properties”)覆盖此设置,通过任何机会呢?不可能知道为什么它不能提供信息。您可以尝试在NumberUtils.java中放置一个断点:194并检查无法转换为数字的特定值,然后搜索您的项目。 – kryger
嘿kryger,被解析的值是“$ {app.historySize}”。没有指定configPath,所以也没有加载其他文件。不幸的是我不能提供更多的信息,这似乎很棘手,我不明白为什么@Value注释中的字符串没有被解释。 –
尝试从您的配置中删除所有其他属性。我同意@ kryger一些其他的财产正在压倒你的第一个财产。另外在路径中应该是classpath:conf/application.properties。 –