0

I have an integration test with the following configuration:

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("integration-test")
@ContextConfiguration(classes = { PersistenceJpaConfig.class, ContextConfig.class, ServiceConfig.class, WebConfig.class})
@WebAppConfiguration
public class LeadsIntegrationTest {

...

}

The PersistenceJpaConfig class is the following:

@Configuration
@EnableTransactionManagement
@ComponentScan({ "org.persistence", "org.common.persistence" })
@PropertySource({ "persistence-${spring.profiles.active}.properties" })
@EnableJpaRepositories(basePackages = org.persistence.dao")
public class PersistenceJpaConfig {

...

}

The ${spring.profiles.active} resolves fine when the active profile is "dev", but when set to "integration-test" in the @ActiveProfiles("integration-test), it fails to resolve. Both the persistence-dev.properties and persistence-integration-test.properties are located in src/main/resources . The properties for dev has mysql configuration, and the integration-test has h2 embedded configuraiton for running integration tests that are isolated from dev data. When the test runs, I get the following:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in string value "persistence-${spring.profiles.active}.properties"

What is going on here?

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
John Stafford
  • 565
  • 2
  • 9
  • 29

1 Answers1

0

You haven't told how you set the dev profile but my best guess is that you set it with a system property. When you use @ActiveProfiles the resolution of the profile comes too late for the @PropertySource annotation to pick up the value.

Regardless, do you know that 100% of that custom code there is supported out of the box with spring boot? Why can't you put that stuff in application-integration-test.properties and application-dev.properties?

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • Thanks Stephane for your response. I think you are absolutely correct about the ActiveProfiles resolution. I got rid of that and just created a seperate JpaPersistenceConfigTest.java class and pesistence-integration-test.properties file in my src/test/java and src/test/resources folder. The properties file location is hard coded into the JpaPersistenceConfigTest.java class. No variable resolution now. Tricky part was getting Spring to find this properties file on the class path from a unit test location. Had to explicitly put classpath in @PropertySource of new JpaPersistenceConfigTest.java . – John Stafford Dec 11 '16 at 16:06
  • I am glad you make it work. Too bad you didn't try to use what Spring Boot has to offer, you wouldn't have to write a single line of Java configuration.... – Stephane Nicoll Dec 12 '16 at 08:05
  • Hi Stephane. How would you have done it using SpringBoot? – John Stafford Dec 12 '16 at 18:13