2014-09-04 2 views
1

봄 내에 테스트 수퍼 클래스가 설정 한 @ActiveProfile을 재정의하는 방법이 있습니까?Spring 프레임 워크에서 ActiveProfiles를 레이어링하거나 덮어 쓰기

<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("one-off-test") 
public class MyTest extends TestBase { 
:
@ActiveProfiles("integration") 
public abstract class 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이있는 경우, 그래서 프로필을 레이어 할 수있는 무엇

integration 프로필 빈.

도움을 주시면 감사하겠습니다. 당신이 제시 한 문제에

답변

0

하나 개의 솔루션은 다음과 같이 primary을 사용하는 것입니다

<beans profile="integration"> 
    <bean class="org.easymock.EasyMock" factory-method="createMock" primary=true> 
     <constructor-arg value="com.mycompany.MyInterface" /> 
    </bean> 
</beans> 

봄의 프로필 integration에서 빈을 사용하기 때문에 다른 작업을 변경할 필요가 없습니다 어디 유형 MyInterface의 콩. Spring은 그 타입의 여러 콩이 있다는 사실에도 불구하고 그렇게합니다.

primary의 작동 방식에 대한 자세한 내용은 this을 확인하십시오.

+0

아름답고 우아한 해결책 :

그럼 당신은 서브 클래스 프로파일을 discart 현재 주석 테스트 케이스를 방지 할 수 inheritProfiles 주석 propertyto false을 설정해야합니다. 고맙습니다! –

+0

당신이 그것을 좋아했기 때문에 기쁩니다 : -) !!! – geoand

5

당신은 시스템 등록 정보를 통해 활성 프로파일을 지정해야 할 수도 있습니다 : 당신이 일회성 테스트를 사용하도록 관련 빈 구현 또는

-Dspring.profiles.active="one-off-test" 

를 사용하려면

-Dspring.profiles.active="integration" 

을 경우 프로파일 빈.

@ActiveProfiles(profiles = {"one-off-test"}, inheritProfiles= false) 
public class MyTest extends TestBase {} 
+0

니스! +1! 'inheritProfiles'의 존재는 내 마음을 미끄러 뜨 렸습니다. – geoand

+1

예, 비슷한 상황에 직면했을 때 그것을 발견하지 못하면 광산을 미끄러 뜨릴 수도 있습니다 : D – tmarwen

관련 문제