弹簧单元/集成测试设置

Roy*_*han 1 java junit spring log4j

我没有写单元或集成测试,但现在我正在尝试.我很难设置环境.

我在WEB-INF/applicationContext*.xml下有我的应用程序上下文,在我的applicationContext.xml中,它有一个对DB用户/传递,LDAP主机等的属性文件的引用

<bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/spring-config/dev.properties</value>
            </list>
        </property>
    </bean>
Run Code Online (Sandbox Code Playgroud)

我有另一个log4j配置属性(DEV/Staging/Production的diff配置).${webapp.root}在web.xml中定义

 <!-- log4j setting -->
    <bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>${webapp.root}/${log4j.properties.location}</value>
            </list>
        </property>
    </bean>
Run Code Online (Sandbox Code Playgroud)

现在我试图将以下内容放入测试类中.

@Override
protected String[] getConfigLocations() {
   return new String[]{
            "file:trunk/code/web/WEB-INF/applicationContext.xml",
        };
}
Run Code Online (Sandbox Code Playgroud)

这正确引用了我的xml,但所有属性都被搞砸了.

我想知道以下内容:

  • 有没有办法在测试类中正确设置?如果没有,我应该移动这些课程吗?
  • 如果存在仅存在于容器中的webroot的引用,我该如何设置Log4j?
  • Spring配置位置的最佳实践是什么?

请指教

谢谢

Boz*_*zho 6

我的这篇博文描述了实现目标的基本步骤.

请注意,单元测试不应该知道您有一个webapp-root - 它们通常在没有启动任何servlet容器的情况下运行.因此,将备用配置文件放在测试包中并尝试.