如何配置Maven启动tomcat服务器后运行测试?

时间:2018-04-13 15:32:45

标签: java maven tomcat java-ee junit

当我运行我的maven项目时,它从测试开始,它们都失败了,因为tomcat服务器尚未启动并且战争尚未部署? 如何在测试时配置maven:

启动服务器/应用程序 - >然后运行测试 - >然后停止服务器

1 个答案:

答案 0 :(得分:1)

您可以使用Tomcat Maven插件在构建期间运行tomcat。 请尝试以下配置:

<build>
    <plugins>
        <!-- excludes tests that require application -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>**/TomcatPingTest.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- starts tomcat before test execution and stops after-->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>run-tomcat</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-tomcat</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>shutdown</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <fork>true</fork>
                <port>5555</port>
                <path>/app</path>
            </configuration>
        </plugin>
        <!-- runs tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.12</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includes>
                    <include>**/TomcatPingTest.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>