2014-02-26 62 views
3

我的活動配置文件正常工作,如果我將它們設置爲虛擬機參數。我有一個測試,我想用@ActiveProfiles("local")@ActiveProfiles值沒有被分配到配置

這裏是類註解的我使用的是:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("/jpaContext.xml") 
@ActiveProfiles("local") 
public class MyServiceTest { 

當我嘗試運行我得到了我的跟蹤如下:

Caused by: java.io.FileNotFoundException: class path resource [properties/database-configuration-${spring.profiles.active}.properties] cannot be opened because it does not exist 

爲什麼這個值是不是有什麼想法用過的?

回答

5

當你在你的配置文件解析的spring.profiles.active佔位符值,春天默認爲價值系統屬性,並得到,當你將它設置爲系統屬性值。

現在,在您的測試中,由於未設置此係統屬性,佔位符未得到徹底解決。一個修復可能以不同的方式一點點加載性能,通過豆型材:

<beans profile="local"> 
    <context:property-placeholder location="classpath:database-config-local.properties"/> 
</beans> 

<beans profile="dev"> 
    <context:property-placeholder location="classpath:database-config-dev.properties"/> 
</beans> 
+4

的問題是'@ ActiveProfiles'註釋不設置在'Environment'的'spring.profiles.active'屬性(其只是設置活動配置文件,這是不同的)。如果你不想像所建議的那樣改變你的配置,你可以在測試中添加一個'ApplicationContextInitializer'(在@ContextConfiguration'中),它添加一個屬性源和激活的配置文件密鑰。 –