0

I have two configurations for my TestSuite which provide a different beans for injection. This works so long as I set my profile with an annotation.

@ActiveProfiles( profiles={"a"}) and @ActiveProfiles( profiles={"b"})

But I seem unable to set it from the Property Source file

My annotations look like

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AConfig.class, BConfig.class })
@PropertySource("classpath:/application.properties")
@TestPropertySource(locations = {"classpath:/application.properties"})
public abstract class AbstractTestIT {
   ...
}

And the content of application.properties is

spring.profiles.active="a"

And the results in an unsatisfied dependency

As mentioned setting with @ActiveProfiles as above works and the correct been is used.

It's almost as if PropertySource and/or TestPropertySource don't work with @RunWith(SpringJUnit4ClassRunner.class)

Christopher Hackett
  • 6,042
  • 2
  • 31
  • 41
  • Tried using EL to see if you can actually access any of the properties in your test? That would establish whether the properties files are being loaded – Plog Aug 15 '17 at 16:54

1 Answers1

1

This works so long as I set my profile with an annotation.

And it is expected.

ActiveProfiles doesn't rely on the spring.profiles.active property.

ActiveProfiles is a class-level annotation that is used to declare which active bean definition profiles should be used when loading an ApplicationContext for test classes.

And the profiles attribute that is an alias for the value attribute needs to be valued with the profile(s) to activate for the tests

The bean definition profiles to activate.

It doesn't use the spring.profiles.active property.

The spring.profiles.active property specifies which profiles are active in the overall configuration of the the application, not for the unit test context.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thanks for the answer. My intention it to provide the default profile in the ```application.properties``` file and then when been run under CI override the profile with an env. How can I reach this same result? – Christopher Hackett Aug 15 '17 at 21:19