弹簧单元/集成测试设置
我没有编写单元或集成测试,但现在我正在尝试。我很难设置环境。
我的应用程序上下文位于 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配置位置的最佳实践是什么?
请指教
谢谢
I didn't write unit or integration testing but now I am trying. I am having a hard time setting up the environment.
I have my application context under WEB-INF/applicationContext*.xml
and in my applicationContext.xml, it has a reference to a properties file for DB user/pass, LDAP host, etc
<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>
I have another properties for log4j config (diff config for DEV/Staging/Production). ${webapp.root}
is defined in 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>
And now I am trying to put the following in a test class.
@Override
protected String[] getConfigLocations() {
return new String[]{
"file:trunk/code/web/WEB-INF/applicationContext.xml",
};
}
This references my xml correctly, but all the properties are screwed up.
I want to know the following:
- Is there a way to set up in the test class properly? If not, should I move these classes?
- How can I set up Log4j if there is a reference to webroot which only exist in a container?!
- What is the best practice of Spring config location?
Please Advise
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的这篇博文 描述实现目标的基本步骤。
请注意,单元测试不应该知道您有 webapp-root - 它们通常在没有启动任何 servlet 容器的情况下运行。因此,将替代配置文件放入测试包中并尝试。
This blogpost of mine describes the basic steps to achieve your goal.
Note that the unit tests shouldn't know that you have a webapp-root - they are usually run without any servlet container started. So place the alternative config files in the test packages and try.
对于单元测试,您不应该使用 Spring 应用程序上下文。您应该单独测试所有 spring bean 和控制器,因为它们是系统中的单独单元。由于它们是 POJO,因此很容易在测试用例代码中以编程方式将所有内容连接在一起。它还解决了日志记录属性文件的位置等问题,因为您可以以编程方式指定不依赖于 webroot 属性的不同路径。
测试章节 Spring 参考很好地概述了如何对使用 Spring 的应用程序进行单元测试和集成测试。它还提供了 Spring 提供的各种支持类的详细信息,以帮助编写单元和集成测试。
For unit testing you should not be using the Spring application context. You should be testing all your spring beans and controllers individually as they are the individual units within the system. As they are POJOs it is easy to wire everything together programatically in your test case code. The also solves issues such as the location of the logging properties file as you can programatically specify a different path that does not rely on the webroot property.
The testing chapter in the Spring Reference provides a good overview of how to approach unit and integration testing of applications that use Spring. It also provides details of the various support classes that Spring provides to help with writing unit and integration tests.
您可以使用注释来引用测试中的必要配置,如下所示:
You could use annotations to reference the necessary configuration from the tests, like this: