5

我使用failsafe插件。

因此,当我键入mvn failsafe:integration-test它时,我的集成测试会加注星号(这很棒)。

但我希望我jetty serverpre-integration舞台上开始。我应该怎么办?

(我不想启动mvn verify,因为它涉及整个循环运行,但是mvn failsafe:integration-test- 它似乎应该以这种方式工作)

有两个插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>                                                              <!-- for starting jetty for integration tests -->
    <version>2.16</version>
    <executions>
        <execution>
            <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
        <execution>
            <id>verify</id>
            <goals>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
    <configuration>
        <!--<jettyConfig>${project.basedir}/src/main/resources/config/jetty9.xml</jettyConfig>-->
        <stopKey>STOP</stopKey>
        <stopPort>9999</stopPort>
        <stopWait>5</stopWait>
        <scanIntervalSeconds>5</scanIntervalSeconds>
        <scanTargets>
            <scanTarget>${project.basedir}/src/main</scanTarget>
            <scanTarget>${project.basedir}/src/test</scanTarget>
        </scanTargets>
        <contextXml>${project.basedir}/src/test/resources/jetty-context.xml</contextXml>
        <webAppConfig>
            <contextPath>/${project.artifactId}-${project.version}</contextPath>
        </webAppConfig>
    </configuration>

    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>                                                         <!-- In the pre-integration-test phase the Jetty server will be started -->
            <goals>
                <goal>run-exploded</goal>
            </goals>
            <configuration>
                <scanIntervalSeconds>0</scanIntervalSeconds>
                <daemon>true</daemon>
            </configuration>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>                                                        <!-- in the "post-integration-phase" it will be stopped -->
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>
4

2 回答 2

5

这是码头和 maven-failsafe-plugin 使用手册:

Maven Failsafe 插件 – 使用

它提供了将 Jetty 集成到集成测试生命周期中的示例配置。

Jetty 在阶段启动,pre-integration-test在 v 阶段停止post-integration-test

<plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.16</version>
    <executions>
      <execution>
        <id>start-jetty</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <scanIntervalSeconds>0</scanIntervalSeconds>
          <daemon>true</daemon>
        </configuration>
      </execution>
      <execution>
        <id>stop-jetty</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>stop</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

但是,它也特别建议您使用verify阶段:

建议不要直接调用 pre-integration-test、integration-test 或 post-integration-test 阶段,而是通过指定 verify 阶段来运行集成测试。[...]

这允许您在集成测试前阶段设置集成测试环境,在集成测试阶段运行集成测试,在集成测试后阶段干净地拆除集成测试环境,然后最终检查集成测试结果并在必要时使构建失败。

于 2014-08-05T00:41:10.047 回答
2

我更喜欢在测试用例中以编程方式即时启动码头。主要原因是:

  • 测试变得独立,不依赖于 Maven 配置
  • 可以在任何 IDE 中一样运行测试
于 2014-08-06T13:05:20.037 回答