【发布时间】:2014-09-04 21:22:20
【问题描述】:
有没有办法覆盖由 Spring 中的测试超类设置的 @ActiveProfile?
这是我的配置:
<beans profile="integration">
<bean class="org.easymock.EasyMock" factory-method="createMock">
<constructor-arg value="com.mycompany.MyInterface" />
</bean>
</beans>
<beans profile="production,one-off-test">
<bean class="com.mycompany.MyInterfaceImpl" />
</beans>
所有测试的超类如下所示:
@ActiveProfiles("integration")
public abstract class TestBase {
在我的新测试课中,我想这样做:
@ActiveProfiles("one-off-test")
public class MyTest extends TestBase {
不从 TestBase 继承并不是一个真正的选择。 当我尝试运行它时,我得到的错误是:
No qualifying bean of type [com.mycompany.MyInterface] is defined: expected single matching bean but found 2
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.mycompany.MyInterface] is defined: expected single matching bean but found 2: org.easymock.EasyMock#1,com.mycompany.MyInterfaceImpl#0
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:970)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 46 more
更好的是能够对配置文件进行分层,以便如果存在配置文件one-off-test 的 bean,则注入该配置文件,否则注入 integration 配置文件 bean。
任何帮助将不胜感激。
【问题讨论】:
标签: java spring integration-testing