问题是我如何使用不同的属性文件进行集成测试。详细解释如下。
我正在尝试使用tomcat7-maven-plugin
与Maven进行容器集成测试。该项目使用Spring和JPA。目前我发现的是以下内容:
mvn test
maven-surefire-plugin
运行
mvn verify
maven-failsafe-plugin
运行
所有这一切都有效,当我运行mvn verify
Tomcat时就启动了。
但是,我想使用内存数据库,而不是使用常规数据库。我的数据库配置在通过src/main/resources/META-INF/spring/database.properties
加载的文件context:property-placeholder
中定义。
我尝试在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>
有关如何加载替代属性文件以进行测试的任何建议吗?
答案 0 :(得分:2)
一种方法是使用Maven配置文件和ant插件。我怀疑这不是最优雅的方式,我愿意倾听更好的解决方案,但现在它解决了我的问题。
配置文件是一种根据命令行参数获得不同Maven配置的方法,因此在我的情况下运行集成测试时,我运行命令mvn -Pintegration clean verify
。此处integration
是个人资料的名称,并在pom.xml
之后的properties
中定义,如下所示:
<profiles>
<profile>
<id>standard</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Copying test database.properties to ${project.build.outputDirectory}/META-INF/spring/</echo>
<copy file="src/test/resources/META-INF/spring/database.properties"
todir="${project.build.outputDirectory}/META-INF/spring/" verbose="true" overwrite="true" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
使用mvn clean package
代替使用标准配置文件。有趣的是,在.m2 / settings.xml
另请参阅:Building For Different Environments with Maven 2 - 请注意,副本中的overwrite
参数必不可少,或者只能随机使用,并且在链接文档中未提及。