2017-08-15 59 views
0

我有兩個配置用於我的TestSuite,它們提供了一個不同的注入bean。只要我使用註釋設置我的個人資料,這可以工作。@RunWith的配置文件(SpringJUnit4ClassRunner.class)

@ActiveProfiles(profiles={"a"})@ActiveProfiles(profiles={"b"})

但我似乎無法從財產來源設置文件

我的註釋看起來像

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {AConfig.class, BConfig.class }) 
@PropertySource("classpath:/application.properties") 
@TestPropertySource(locations = {"classpath:/application.properties"}) 
public abstract class AbstractTestIT { 
    ... 
} 

application.properties內容

spring.profiles.active="a" 

和導致不滿意的依賴關係

如上所述使用@ActiveProfiles進行的設置工作和正確使用。

這幾乎就像PropertySource和/或TestPropertySource不工作與@RunWith(SpringJUnit4ClassRunner.class)

+0

嘗試使用EL來查看您是否實際可以訪問測試中的任何屬性?這將確定是否正在加載屬性文件 – Plog

回答

1

這工作,只要我把我的個人資料與註釋。

它是預期的。

ActiveProfiles不依賴spring.profiles.active屬性。

ActiveProfiles是用於聲明 ,其活性bean定義分佈應當爲測試類加載 ApplicationContext中時使用的類級註釋。需要

而且profiles屬性,它是爲value屬性的別名與配置文件(一個或多個)進行估值,以激活測試

的bean定義配置文件來激活。

它不使用spring.profiles.active屬性。

spring.profiles.active屬性指定哪些配置文件在應用程序的整體配置中處於活動狀態,而不是用於單元測試上下文。

+0

感謝您的答案。我打算在'''application.properties'''文件中提供默認配置文件,然後在CI下運行時,將使用env覆蓋配置文件。我怎樣才能達到同樣的結果? –