My application.yaml has the following entries:
spring:
profiles:
active: test
include: h2, test
key:
enabled: on
I have the following three bean definitions and would like to load the TestConfig bean in my tests. But it takes NoConfig Bean in the tests:
@ConditionalOnProperty(name = "key.enabled", matchIfMissing = true)
@Profile("test")
public class TestConfig {}
@ConditionalOnMissingBean(Config.class)
public class NoConfig {}
@ConditionalOnProperty(name = "key.enabled", matchIfMissing = true)
@Profile("!test")
public class Config{}
I also check the active profile in the test, and it is indeed test profile.
@Value("${spring.profiles.active:Unknown}")
private String activeProfile;
Bean
NoConfigyou have is always created when there is no beanConfig. You can try to use@TestConfigurationto define beans needed for a particular test, or you can write aBeanFactoryPostProcessorto exclude unnecessary bean from initialisationOr try to add
havingValueattribute in@ConditionalOnPropertyannotation and changekey.enabledattribute in yaml file to set the value of it totrue