0

I have config files for my different environmnets: prod.properties, dev.propertites, and test.properties.

Properties file is loaded via application-context.xml file using

${spring.profiles.active}.properties

and passing in -Dspring.profiles.active flag. This works file for my 3 environments.

For Junit testing I'm trying to use

@ActiveProfiles("test")

in my test class but $(spring.profiles.active) is not resolving. But when I use

System.getProperties().setProperty("spring.profiles.active", "test");

it resolved fine.

I know @ActiveProfiles is only for test classes but why doesn't it set the spring.profiles.active property? Does it only affect which beans to use?

This also doesn't clarify who best to handle profiles and property files. Spring profiles and Testing

Update: I have so far resorted to using the following in my application context file:

<beans profile="dev,prod">
<context:property-placeholder
      location="classpath:META-INF/properties/generic.properties,
        classpath:META-INF/properties/${spring.profiles.active}/${spring.profiles.active}.properties"/>
        <context:component-scan base-package="com.my.package.to.scan"/>
 </beans>       

<beans profile="test">
    <context:property-placeholder
      location="classpath:META-INF/properties/generic.properties,
        classpath:META-INF/properties/test/test.properties"/>
<context:component-scan base-package="com.my.package.to.scan"/>
</beans>
Micho Rizo
  • 1,000
  • 3
  • 12
  • 27

1 Answers1

0

As the name suggest @ActiveProfiles, this is array of profiles.

/**
 * The bean definition profiles to activate.
 * <p>This attribute may <strong>not</strong> be used in conjunction with
 * {@link #value}, but it may be used <em>instead</em> of {@link #value}.
 */
@AliasFor("value")
String[] profiles() default {};

Try with @ActiveProfiles({"test"}).

Rohit
  • 2,132
  • 1
  • 15
  • 24
  • I wish it were that easy ... still does not pick it up ... Caused by: java.io.FileNotFoundException: class path resource [META-INF/properties/${spring.profiles.active}/${spring.profiles.active}.properties] cannot be opened because it does not exist – Micho Rizo Jul 04 '17 at 15:49
  • To get it to work, I have had to define bean profiles in my application content file for the property placeholder. For my envinronments i keep the ${spring.profiles.active} property in the property file path but for junit purposes I have it hardcoded. – Micho Rizo Jul 04 '17 at 16:24
  • @MichoRizo FileNotFoundException is something different from what you initially mentioned in question. For it can you share further information like project structure your file location . Better if you make new question out of it as i think this does not need to be hardcoded. – Rohit Jul 04 '17 at 16:33
  • the filenotfound issue is due to ${spring.profiles.active} not resolving to the ActiveProfiles annotation. I can pass in -Dspring.profiles.active=test or set the System.getProperties().setProperty("spring.profiles.active", "test"); and it works. – Micho Rizo Jul 04 '17 at 17:51