我没有编写单元测试或集成测试,但现在我正在尝试。我正在艰难地建立环境。
我的应用程序上下文位于WEB/applicationContext*..xml之下,在我的applicationContext.xml中,它引用了DB用户/pass、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/暂存/生产的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>现在我试着把下面的内容放到一个测试类中。
@Override
protected String[] getConfigLocations() {
return new String[]{
"file:trunk/code/web/WEB-INF/applicationContext.xml",
};
}这正确地引用了我的xml,但是所有的属性都搞砸了。
我想知道以下几点:
请告知
谢谢
发布于 2010-02-12 23:21:43
我的博客描述了实现目标的基本步骤。
注意,单元测试不应该知道您有一个webapp-root -它们通常是在没有启动任何servlet容器的情况下运行的。因此,将替代配置文件放在测试包中,然后尝试。
发布于 2010-02-12 23:10:09
对于单元测试,您不应该使用Spring应用程序上下文。您应该单独测试所有spring和控制器,因为它们是系统中的各个单元。由于它们是POJO,所以很容易在测试用例代码中以编程方式将所有内容连接在一起。它还解决了诸如日志属性文件的位置等问题,因为您可以编程地指定不依赖webroot属性的不同路径。
Spring参考中的测试章很好地概述了如何处理使用Spring的应用程序的单元测试和集成测试。它还提供了Spring为帮助编写单元和集成测试而提供的各种支持类的详细信息。
发布于 2012-07-11 18:39:16
您可以使用注释引用测试中的必要配置,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
@ContextConfiguration(locations = {
"file:../WebService/src/main/resources/application-context.xml",
"file:../ServiceLayer/src/test/resources/ServiceLayer-dao-test-context.xml" })
public class MyTest {
// class body...
}https://stackoverflow.com/questions/2255451
复制相似问题