问题是如何使用不同的属性文件进行集成测试。详细解释如下。在Spring项目中使用Maven进行集成测试
我正在尝试使用Maven使用tomcat7-maven-plugin
进行容器集成测试。该项目使用Spring和JPA。目前我已经想通如下:
- 单元测试的类名遵循的模式*测试,并与
mvn test
由maven-surefire-plugin
- 集成测试的类名运行遵循的模式* IT和
mvn verify
通过运行maven-failsafe-plugin
- 随着处决我可以触发启动和停止Tomcat和部署WAR文件
所有这一切工作,当我运行mvn verify
在Tomcat启动。
但是,不使用常规数据库,我想使用内存数据库。我的数据库配置在通过context:property-placeholder
加载的文件src/main/resources/META-INF/spring/database.properties
中定义。
我试图在src/test/resources/META-INF/spring/database.properties
中定义一个替代文件,但被忽略。我知道可以在tomcat7-maven-plugin
中定义系统属性,但我不知道如何使用它们来触发加载不同的属性文件。
我tomcat7-maven-plugin
配置如下:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>9090</port>
<path>/processing</path>
<useTestClasspath>true</useTestClasspath>
<systemProperties>
<example.value.1>alpha</example.value.1>
</systemProperties>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war-only</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
</plugin>
属性由context-main.xml
使用以下行加载:
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
予加载上下文配置从web.xml
与下列:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/context-*.xml</param-value>
</context-param>
任何suggestio有关如何加载替代属性文件进行测试?
'src/test/resources/META-INF/spring/database。属性“将在正常测试中被考虑进去,但在故障测试中不会被考虑,因为失败测试是基于正常的'war',并且不包括测试src或测试资源 – Ralph 2013-03-23 21:21:27
对,我怀疑是这样,现在我正在试验配置文件和'maven-antrun-plugin'来直接在build文件夹中复制修改过的属性文件。如果有人有一个更好的主意,我全是耳朵。 – stivlo 2013-03-23 21:32:23