2014-02-26 63 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'中),它添加一个属性源和激活的配置文件密钥。 –