2016-09-27 141 views
1

我想測試我的應用程序和運行到麻煩:測試執行失敗,因爲失敗配置負載

@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 

我沒有做任何線索,我嘗試了很多不同的東西,毫無效果。

感謝提前:)

+0

有沒有任何其他特定propertie文件(例如的 「文件:$ {}用configPath /application.properties」)覆蓋此設置,通過任何機會呢?不可能知道爲什麼它不能提供信息。您可以嘗試在NumberUtils.java中放置一個斷點:194並檢查無法轉換爲數字的特定值,然後搜索您的項目。 – kryger

+0

嘿kryger,被解析的值是「$ {app.historySize}」。沒有指定configPath,所以也沒有加載其他文件。不幸的是我不能提供更多的信息,這似乎很棘手,我不明白爲什麼@Value註釋中的字符串沒有被解釋。 –

+0

嘗試從您的配置中刪除所有其他屬性。我同意@ kryger一些其他的財產正在壓倒你的第一個財產。另外在路徑中應該是classpath:conf/application.properties。 –

回答

0

有解決這個問題的一個非常簡單的方法:

// To resolve ${} in @Value 
@Bean 
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 

這必須加載配置文件的方法,並幫助解決佔位符。

感謝您的幫助;)