Spring Boot中多战应用的集成测试

时间:2014-10-01 20:09:01

标签: java spring maven junit spring-boot

我有一个由多个maven战争项目组成的应用程序。

我有另一个maven项目,它使用org.springframework.web.client.RestTemplate调用对手动启动的tomcat部署的多战应用程序运行JUnit集成测试。

但是,我希望我的集成测试项目能够在运行我的测试之前实际启动我的多战应用程序(一次是整个套件的持续时间)...在spring-boot中!

从我的集成测试项目中,我希望能够将所有war项目作为spring-boot应用程序一起运行,每个应用程序都有自己的contextPaths(例如localhost:8080 / a用于项目'a',localhost: 8080 / b用于项目'b'等),并且不更改原始战争项目(尚未启动弹簧启动)。如果我不能在spring-boot中从我的集成测试项目中运行这些项目而不更改它们那么我至少希望尽可能减少在打包的war文件中使用spring-boot依赖项和配置...尽可能

我能够让我的集成测试项目依赖于单个war项目,启动它并针对它运行测试......但是我没有成功将两个war项目在spring-boot中一起运行在单独的contextPaths下。 / p>

欢迎任何建议!

以下是我用来将这些资源整合在一起的一些资源:

1 个答案:

答案 0 :(得分:6)

根据Andy的建议,我使用了Tomcat7 Maven插件,它运行得很好。 Jetty Maven插件是另一种选择(并且更好地记录了IMO),虽然我找不到一种方法来避免必须为我的WAR文件提供“路径”。 Tomcat7 Maven插件,让我从我当地的.m2存储库加载我的WAR。我还应该说以下链接也很有用......

这是我的集成测试项目pom的一部分......

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <includes>
                    <include>**/*Test*</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/</path>
                <webapps>
                    <webapp>
                        <groupId>com.mycompany</groupId>
                        <artifactId>app1</artifactId>
                        <version>${project.version}</version>
                        <type>war</type>
                        <asWebapp>true</asWebapp>
                    </webapp>
                    <webapp>
                        <groupId>com.mycompany</groupId>
                        <artifactId>app2</artifactId>
                        <version>${project.version}</version>
                        <type>war</type>
                        <asWebapp>true</asWebapp>
                    </webapp>
                </webapps>
            </configuration>
            <executions>
                <execution>
                    <id>start-tomcat</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run-war</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>
    </plugins>
</build>