runwith springjunit4classrunner给出了错误,无法加载ApplicationContext使



我正在使用初始化bean来模态类中的初始化静态属性。这个对象我是在服务中自动接线

当我编写服务的测试案例时,我会引发错误:无法加载ApplicationContext

配置类

public class AppConfig {
    private String prop1;
    protected void setProp1(String prop) {
        this.prop1 = prop;
    }
    public String getProp1() {
        return prop1;
    }
}

PropertyIntilizer类

public class PropertyIntializer implements InitializingBean {
    @Autowired
    private AppConfig appConfig;
    @Override
    public void afterPropertiesSet() throws Exception {
        appConfig.setProp1("PROP");
    }
}

服务类

@Service
public class Service {
    @Autowired
    private AppConfig appConfig;
    public void doSomething(){
        System.out.println(appConfig.getProp1());
    }
}

TestClass

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { TestConfig.class })
public class ServiceTest {
    @Autowired
    private Service service;
    @Test
    public void testService(){
        service.doSomething();
    }
}

这给出了一个错误:无法加载ApplicationContext

但是,当我删除自动启用appconfig时,它起作用

编辑:TestConfig类

@Configuration
@ComponentScan(basePackages = { "base.package" })
public class TestConfig {
}

我的主要类别在base.package..main和base.package.test

在BuID路径中添加JRE(在我的情况WebSphere中随附应用程序服务器)

通过添加jre(在我的情况WebSphere中随附),也解决了类似的问题。

最新更新