2015-02-09 164 views
5

我有一個配置屬性實例,前綴爲「assets」。如何覆蓋@PropertySource中的配置值使用@TestPropertySource在單元測試@ConfigurationProperties配置類中使用

@Configuration 
@ConfigurationProperties(prefix = "assets", ignoreUnknownFields = true) 
public class AssetsProperties 
{ 
    @NotNull 
    private Resource  file; 

    public Resource getFile() 
    { 
    return file; 
    } 

    public void setFile(Resource file) 
    { 
    this.file = file; 
    } 
} 

其默認配置中定義:

@Configuration 
@PropertySource(name = "assetsConfig", value = "classpath:com/package/boot/web/ui/assets/config/default-assets-config.properties") 
@Order(LOW_ORDER) 
public class AssetsConfig 
{ 
} 

default-assets-config.properties包含:

assets.file=classpath:assets.json 

在我的單元測試我想重寫使用默認值:

@TestPropertySource(locations = "classpath:com/package/boot/web/ui/assets/tests/assets-config.properties") 

assets-config.properties包含

assets.file=classpath:com/package/boot/web/ui/assets/tests/assets.json 

不幸的是,這個值永遠不會被注入到AssetsProperties中。 我該怎麼辦錯了,我不明白,因爲spring fmk ref doc says

測試產權來源比那些從應用程序中添加操作系統的環境或Java系統屬性以及財產來源加載聲明通過@更高的優先級PropertySource或以編程方式。

由於提前,

Paskos

回答

7

你已經打了一個limitation in Spring Boot這意味着它忽略了使用@TestPropertySource配置屬性文件。作爲替代方案,您可以配置一個或多個內聯屬性:

@TestPropertySource(properties = "assets.file=classpath:com/package/boot/web/ui/assets/tests/assets.json") 
+0

謝謝@Andy。我訂閱了該問題,並在等待時使用內聯屬性。 – paskos 2015-02-11 15:03:32

+1

已升級爲彈簧引導1.2.3,現在可以使用。謝謝 – paskos 2015-04-10 19:11:54