Reputation: 6042
I want to use ApplicationContextRunner
for asserts on beans existence based on @ConditionalProperty
I've got in SomeConfig
. The problem is that SomeProperties
is defunct with all prop values as null, because conf properties are not activated. There is no direct access to EnableConfigurationPropertiesRegistrar
which is @Import
by annotation @EnableConfigurationProperties
:
contextRunner = new ApplicationContextRunner().withInitializer(applicationContext -> applicationContext.getEnvironment().setActiveProfiles("test"))
.withInitializer(new ConfigFileApplicationContextInitializer())
.withBean(SomeProperties.class)
.withConfiguration(UserConfigurations.of(SomeConfig.class));
The block above will have SomeProperties
content props as null during run.
How can I activate programmatically conf properties?
I need top code block to do exactly same as :
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@EnableConfigurationProperties
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class, classes = {SomeConfig.class, SomeProperties.class})
public class SomeTest
I could use withProperties
, but my properties are @Validated
, so they will blow up if I declare just the one for @ConditionalOnProperty
same time I don't want to load manually entire list of properties by hand.
Upvotes: 2
Views: 987
Reputation: 6042
Finally solved:
.withUserConfiguration(SomeConfig.class, ConfigurationPropertiesAutoConfiguration.class);
It is always AutoConfiguration classes making the magic, my bad forgot it.
Upvotes: 4