在Spring项目中与Maven进行集成测试

时间:2013-03-23 20:01:01

标签: spring integration-testing maven-3

问题是我如何使用不同的属性文件进行集成测试。详细解释如下。

我正在尝试使用tomcat7-maven-plugin与Maven进行容器集成测试。该项目使用Spring和JPA。目前我发现的是以下内容:

  • 单元测试类名称遵循模式*测试并通过mvn test
  • maven-surefire-plugin运行
  • 集成测试类名称遵循模式* IT并通过mvn verify
  • maven-failsafe-plugin运行
  • 通过执行,我可以触发启动和停止tomcat并部署war文件

所有这一切都有效,当我运行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>

有关如何加载替代属性文件以进行测试的任何建议吗?

1 个答案:

答案 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

中也可以在pom外部定义配置文件

另请参阅:Building For Different Environments with Maven 2 - 请注意,副本中的overwrite参数必不可少,或者只能随机使用,并且在链接文档中未提及。