2014-01-23 48 views
0

在我的应用程序中有很多基于Unitils的集成测试类,它使用@SpringApplicationContext(与来自不同模块的不同spring xmls)。为了减少正在创建的spring上下文的数量,我们计划创建一个基本测试类并仅从此基类初始化spring上下文,并使所有其他测试扩展此基类。Spring xml在子类中声明时初始化bean,但是如果在父类中初始化bean初始化失败

基类:

com.org.module1.mypack;

@SpringApplicationContext({ “弹簧module1.xml”, “弹簧module2.xml”, “弹簧module3.xml”}) 公共抽象类BaseTest { ... }

变更前

子类:

com.org.module1.mypack.performance;

@SpringApplicationContext({ “弹簧module4.xml”}) 公共类ChildTest延伸BaseTest { .. }变更后

基类:

com.org.module1.mypack;

@SpringApplicationContext({ “弹簧module1.xml”, “弹簧module2.xml”, “弹簧module3.xml”, “弹簧module4.xml”}) 公共抽象类BaseTest { ... }变更后

子类:

com.org.module1.mypack.performance;

// @ SpringApplicationContext({ “弹簧module4.xml”}) 公共类ChildTest扩展BaseTest { .. }

现在,我得到以下错误消息,我的上下文没有被创建: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.org.module5.prepare.MyBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=myBean)}

当spring-module4.xml在子类中时,上下文被正常创建并且所有测试都照常执行。

注意:除spring-module4外,所有spring xml文件都位于src/main/resources中。xml在src/test/resources中

+0

'NoSuchBeanDefinitionException'清楚地表明父类没有MyBean的可见性。这对任何人来说都响个不停? – EmeraldTablet

回答

0

默认情况下,类的注释不会被继承,除非它们的定义中有@Inherited。

+0

这意味着您必须将@SpringApplicationContext也放在孩子身上。但遵循以下文档:http://www.unitils.org/tutorial-spring.html,您可以保持您的新配置。 –

+0

我试着向子类添加'@ SpringApplicationContext' ...但仍然出现相同的错误 – EmeraldTablet