如何在Junit测试中更改弹簧轮廓

时间:2014-05-15 16:26:46

标签: java xml spring junit spring-profiles

我使用了@Activeprofiles并将包含.xml的弹簧配置文件导入到上下文配置中,但它似乎没有将配置文件加载到相应的Junit类。下面是我所做的事情的片段。断言比较值未根据设置的配置文件进行更改。任何改进的提示,以激活弹簧配置文件。 TEST CLASS。

@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"));
    }
}

2 个答案:

答案 0 :(得分:0)

尝试在个人资料特定配置的 beans 标记中指定个人资料属性:

<!--language:xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans profile="dev">
...
</beans>

答案 1 :(得分:0)

只要您在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>
相关问题