4

Using Spring 4, I've got the following test setup:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JpaConfig.class)
@ActiveProfiles(resolver = TestResolver.class)
public class SimpleTest {

The TestResolver has been implemented as:

public class TestResolver implements ActiveProfilesResolver {
    @Override
    public String[] resolve(Class<?> aClass) {
        String[] profiles = new String[1];
        profiles[0] = "test";
        return profiles;
    }
}

JpaConfig has been annotated with PropertySource

@Configuration
@PropertySource("classpath:properties/application-${spring.profiles.active:dev}.properties")
@EnableJpaRepositories(basePackages={"com.my.namespace.repositories"})
public class JpaConfig {

Whenever I run the SimpleTest it tries to locate: properties/application-dev.properties while I expected it to be properties/application-test.properties.

What's I'm trying to accomplish here has been based on the following post: Spring integration tests with profile

Community
  • 1
  • 1
Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60

2 Answers2

11

I believe this is, actually, the issue you are facing. And in that same post you have an explanation from Dave Syer and a possible solution from another user. To follow Dave's advice, this would be a possible implementation of an ApplicationContextInitializer:

public class MyApplicationContextInitializer implements
    ApplicationContextInitializer<GenericApplicationContext> {

public void initialize(GenericApplicationContext context) {
    context.getEnvironment().getSystemProperties().put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "some_profile");
}

}

and on your test class:

@ContextConfiguration(classes = JpaConfig.class, initializers = MyApplicationContextInitializer.class)

But I would say that the suggested approach (with different .properties files loaded for different profiles) in that SO post is a more elegant approach.

Community
  • 1
  • 1
Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
1

I think you should change @PropertySource to:

@PropertySource("classpath:properties/application-${spring.profiles.active}.properties")

Also for simplicity (shouldn't have any effect on how the code runs) your @ActiveProfile could be

@ActiveProfiles("test")
geoand
  • 60,071
  • 24
  • 172
  • 190
  • 1
    When I do this I get: Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in string value "classpath:properties/application-${spring.profiles.active}.properties" – Jonas Geiregat Apr 22 '14 at 08:21
  • How about if you swap the position of @ActiveProfile and @ContextConfiguration? – geoand Apr 22 '14 at 08:32
  • Doesn't change anything – Jonas Geiregat Apr 22 '14 at 08:39
  • Seems strange that it doesn't work... Do you have the code in GitHub or something to share? – geoand Apr 22 '14 at 08:45
  • Are there any news on the subject of 'spring.profiles.active' not being resolved ? Cause I face the very same issue and am stuck loosing points of sanity :( – Ar3s Oct 27 '14 at 00:04