I am using Spring 4.3.0.I am writing a SDK in that i am having the following classes,
Providers.java
@ComponentScan
@Service
//@PropertySource("classpath:application.properties")
public class Providers {
@Autowired
ApplicationContext context;
public Providers(){
}
public Providers(ApplicationContext applicationContext){
this.context = applicationContext;
}
//...Other SDK component code
}
ProvidersBuilder.java
public class ProvidersBuilder {
//Set providers property
public Providers build() throws LifecycleException, InsufficientPropertiesException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
StandardEnvironment env = new StandardEnvironment();
context.setEnvironment(env);
if(cond1){
context.getEnvironment().addActiveProfile("profile1");
}
if(cond2){
context.getEnvironment().addActiveProfile("profile2");
}
...etc
context.setParent(null);
context.register(Providers.class);
context.refresh();
Providers Providers = new Providers(context);
return Providers;
}
}
I have following configuration for Spring-Junit test classes,
SpringContextLoader.java
@ComponentScan(basePackages = "com.providers.global")
@PropertySource("classpath:application.properties")
public class SpringContextLoader {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SpringContextLoader.class);
}
}
In one of my test class, I am trying to print all the profiles,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringContextLoader.class)
public class ProvidersTest{
@Autowired
ApplicationContext context;
@Before
public void beforeMethod() throws Exception {
String[] profiles = context.getEnvironment().getActiveProfiles();
if(profiles != null && profiles.length > 0){
for (String string : profiles) {
logger.info(String.format("Active Profiles in test::%s",string));
}
}
}
@Test
public void activateProviders() throws Exception{
...invoking test call
}
}
In the logs i am able to see only the profiles configured in application.properties, but i would like to get the profiles which are dynamically added in ProvidersBuilder.java.
Basically i would run ProvidersTest only for particular profiles for that i am using the following annotation, @IfProfileValue(name = "spring.profiles.active", values = { "charging" }) Since application context always returns default profile configured in application.properties this class never get a chance to run.
Could anyone please help me to resolve this issue.Why the profiles added in ProvidersBuilder.java is not available in ProvidersTest.java?
**Edit 1 **
SpringContextLoader.java
@ComponentScan(basePackages = "com.providers.global")
@PropertySource("classpath:application.properties")
public class SpringContextLoader {
@Bean(name = "ConfigApplicationContext")
public AnnotationConfigApplicationContext applicationContext() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
return context;
}
}
Now we are not creating new AnnotationConfigApplicationContext anywhere in application.
ProvidersBuilder.java
public class ProvidersBuilder {
@Autowired
@Qualifier("ConfigApplicationContext")
public AnnotationConfigApplicationContext context;
//Set providers property
public Providers build() throws LifecycleException, InsufficientPropertiesException {
context.setEnvironment(env); **Here i am getting getting NullPointerException**
if(cond1){
context.getEnvironment().addActiveProfile("profile1");
}
if(cond2){
context.getEnvironment().addActiveProfile("profile2");
}
...etc
context.setParent(null);
context.register(Providers.class);
context.refresh();
Providers Providers = new Providers(context);
return Providers;
}
}
In the ProvidersBuilder.java while getting "AnnotationConfigApplicationContext context" using @Autowired it returns null.