我想運行我的測試套件在多個環境下像下面一個測試套件:運行在多種環境
ApplicationContextTest.class
@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
<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>
當我運行測試,我得到了一個異常
Caused by: java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once
在那裏設置的活動配置文件的ApplicationContext啓動後的任何方式?
或解決上述異常的任何解決方案?
謝謝。
沒人能幫我解決這個問題嗎?好難過 :( –