【问题标题】:How to configure Maven to run tests after it starts tomcat server?如何配置 Maven 在启动 tomcat 服务器后运行测试?
【发布时间】:2018-09-24 00:33:02
【问题描述】:

当我运行我的 maven 项目时,它从测试开始,它们都失败了,因为 tomcat 服务器还没有启动,war 还没有部署? 测试时如何配置maven:

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

【问题讨论】:

    标签: java maven tomcat jakarta-ee junit


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多