ApplicationContextInitializer interface can be used for some programmatic initialization of the application context. For example, registering property sources or activating profiles with the context environment.
This example demonstrates how to use ApplicationContextInitializer with Spring tests by using 'initializers' element of @ContextConfiguration.
Example
Creating a simple Spring application
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class AppConfig {
@Bean
@Profile("windows")
public MyService myServiceA() {
return new MyServiceA();
}
@Bean
@Profile("other")
public MyService myServiceB() {
return new MyServiceB();
}
}
package com.logicbig.example;
public interface MyService {
public String doSomething();
}
package com.logicbig.example;
public class MyServiceA implements MyService {
@Override
public String doSomething() {
return "in " + this.getClass().getSimpleName();
}
}
package com.logicbig.example;
public class MyServiceB implements MyService {
@Override
public String doSomething() {
return "in " + this.getClass().getSimpleName();
}
}
Implementing the Initializer
package com.logicbig.example;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class MyApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext ac) {
String os = System.getProperty("os.name");
String profile = (os.toLowerCase().startsWith("windows")) ? "windows" : "other";
ConfigurableEnvironment ce = ac.getEnvironment();
ce.addActiveProfile(profile);
}
}
The JUnit test
Following test will fail if run other than Windows O.S.
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = AppConfig.class,
initializers = MyApplicationContextInitializer.class)
public class MyServiceTests {
@Autowired
private MyService dataService;
@Test
public void testDoSomething() {
String s = dataService.doSomething();
System.out.println(s);
assertEquals("in MyServiceA", s);
}
}
Example ProjectDependencies and Technologies Used: - spring-context 7.0.3 (Spring Context)
Version Compatibility: 5.0.0.RELEASE - 7.0.3 Version compatibilities of spring-context with this example: Versions in green have been tested.
- spring-test 7.0.3 (Spring TestContext Framework)
- junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
- JDK 25
- Maven 3.9.11
|