실행

2013-08-31 1 views
3

나는 아래와 같은 여러 환경에서 내 테스트 스위트를 실행하려면 여러 환경에있는 TestSuite :실행

ApplicationContextTest.class

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

<beans profile="env1"> 

</beans> 

<beans profile="env2"> 

</beans> 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("/application-context.xml") 
public class MyTest{ 

@Autowired 
private ApplicationContext applicationContext; 

@Test 
public void test1() { 
    ((ConfigurableEnvironment)applicationContext.getEnvironment()).setActiveProfiles("env1"); 
    ((GenericXmlApplicationContext)applicationContext).refresh(); 
} 

@Test 
public void test2() { 
    ((ConfigurableEnvironment)applicationContext.getEnvironment()).setActiveProfiles("env2"); 
    ((GenericXmlApplicationContext)applicationContext).refresh(); 
} 

}

applicationContext.xml을

내가 테스트를 실행하면

, 나는 예외

Caused by: java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once

의 ApplicationContext가 시작된 후 Active 프로필을 설정할 수있는 방법이있어?

또는 위의 예외를 해결할 수있는 솔루션은 무엇입니까?

감사합니다.

+0

아무도이 문제에서 나를 도울 수 있습니까? 너무 슬퍼 :( –

답변

0

하나 이상의 특정 프로파일 (들)을 선택하는 일반적인 방법은 또한 시스템에 기본으로 하나 개 이상의 프로파일 (들)을 설정할 수 있습니다

-Dspring.profiles.active=env1 

테스트를 실행할 때 시스템 속성을 설정하는 것입니다 부동산

-Dspring.profiles.default=env1 

나는 보통이 내 시험의 정적 초기화 메서드에서 할

@BeforeClass 
public static void init() { 
    // set default spring test profile 
    System.setProperty("spring.profiles.default", "default"); 
} 

Y ou는 이러한 속성을 검색하면 몇 가지 정보를 찾을 수 있습니다 (Spring 코드 참조가 필요하면 AbstractEnvironment 클래스를 살펴 봐야합니다).

시작한 후에 프로필을 변경하는 방법을 모르겠습니다. 이 경우 각 테스트에서 새 JVM을 사용해야 할 수도 있습니다 (예 : reuseForks 설정을 사용하여 maven surefire 플러그인으로 수행 할 수 있음). 또는 동일한 테스트 클래스에서 동일한 프로필을 사용하는 테스트를 배치하고 필요에 따라 프로필을 설정하십시오.

IMHO 테스트는 특정 프로필에 의존해서는 안됩니다. 어떤 프로파일이 사용 되든지 테스트가 통과되어야합니다. 예를 들어 쉽게 변경할 수 있습니다. 테스트 (모의) 또는 '실제'환경 사이.