【问题标题】:How to change spring profiles in Junit testing如何在 Junit 测试中更改弹簧轮廓
【发布时间】:2014-05-15 16:26:45
【问题描述】:
我已经使用@Activeprofiles 以及将包含 .xml 的 spring 配置文件导入到上下文配置中,但它似乎没有将配置文件加载到相应的 Junit 类中。下面是我所做的sn-p。断言比较值不会根据设置的配置文件进行更改。任何改进以激活弹簧轮廓的提示。
测试班。
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("unittest-hsql")
@ContextConfiguration(locations = {
"classpath:spring/Services.xml"
"classpath:spring/profiles/dev.xml"
})
public class TestSpringProfile
{
@Test
public void testGetCronExpression()
{
String expression = EventLimitation.getExpression();
assertThat(expression, is("20"));
}
}
【问题讨论】:
标签:
java
xml
spring
junit
spring-profiles
【解决方案1】:
尝试在您的配置文件特定配置的 beans 标记中指定 profile 属性:
<!--language:xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans profile="dev">
...
</beans>
【解决方案2】:
只要您在 spring/profiles/dev.xml 文件中有类似下面的内容,您就可以在其中指定名为“unittest-hsql”的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<beans profile="unittest-hsql">
<bean id="applicationPropertiesPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:profiles/hsqldb.profile.properties</value>
</list>
</property>
</bean>
</beans>
</beans>