我没有编写单元或集成测试,但现在我正在尝试。我很难设置环境。
我的应用程序上下文在 WEB-INF/applicationContext*.xml 下 在我的 applicationContext.xml 中,它引用了数据库用户/密码、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>
我有另一个 log4j 配置属性(DEV/Staging/Production 的差异配置)。 ${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>
现在我正在尝试将以下内容放入测试类中。
@Override
protected String[] getConfigLocations() {
return new String[]{
"file:trunk/code/web/WEB-INF/applicationContext.xml",
};
}
这正确引用了我的 xml,但所有属性都搞砸了。
我想知道以下内容:
- 有没有办法在测试类中正确设置?如果不是,我应该移动这些类吗?
- 如果存在对仅存在于容器中的 webroot 的引用,我该如何设置 Log4j?!
- Spring 配置位置的最佳实践是什么?
请指教
谢谢
最佳答案
This blogpost of mine描述实现目标的基本步骤。
请注意,单元测试不应该知道您有一个 webapp-root - 它们通常在没有启动任何 servlet 容器的情况下运行。所以将替代配置文件放在测试包中并尝试。
关于java - Spring 单元/集成测试设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2255451/