3

My Active profiles are working normally if I set them as VM args.

I've got a test with which I want to use @ActiveProfiles("local").

Here are the class annotation's I'm using:

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

When I try to run I get the following in my trace:

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

Any thoughts on why this value is not being used?

Joe Essey
  • 3,457
  • 8
  • 40
  • 69

1 Answers1

5

When you are resolving the placeholder value of spring.profiles.active in your configuration file, Spring is defaulting to the system properties for the value and gets the value when you set it as a system property.

Now, in your test, since this system property is not being set, the placeholder is not getting cleanly resolved. One fix could to load the properties a little differently, through bean profiles:

<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>
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • 4
    The problem is that the `@ActiveProfiles` annotation does not set the `spring.profiles.active` property in the `Environment` (it just sets the active profiles, which is different). If you don't want to change your config like suggested, you could add an `ApplicationContextInitializer` to the test (in the `@ContextConfiguration`) that adds a property source with the active profile key. – Dave Syer Feb 27 '14 at 11:23