在spring-junit测试类中访问Spring配置文件

时间:2016-08-02 16:38:48

标签: spring spring-mvc spring-boot spring-junit

我正在使用Spring 4.3.0。我正在编写SDK,因为我有以下类,

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;
}
}

我有Spring-Junit测试类的以下配置,

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);
}
}

在我的一个测试课程中,我正在尝试打印所有配置文件,

@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
}
}

在日志中,我只能看到application.properties中配置的配置文件,但我想获取在ProvidersBuilder.java中动态添加的配置文件。

基本上我只为特定的配置文件运行ProvidersTest,因为我正在使用以下注释, @IfProfileValue(name =" spring.profiles.active",values = {" charge"}) 由于应用程序上下文始终返回在application.properties中配置的默认配置文件,因此该类永远不会有机会运行。

有没有人可以帮我解决这个问题。为什么ProvidersBuilder.java中添加的配置文件在ProvidersTest.java中不可用?

**编辑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;
}
}

现在我们不会在应用程序的任何位置创建新的AnnotationConfigApplicationContext。

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;
}
}

在ProvidersBuilder.java中获取" AnnotationConfigApplicationContext context"使用@Autowired它返回null。

0 个答案:

没有答案