Maven在集成测试阶段运行jetty

时间:2014-08-04 21:37:28

标签: java maven jetty integration-testing maven-failsafe-plugin

我使用failsafe插件。

因此,当我输入mvn failsafe:integration-test时,它会出现我的集成测试(非常棒)。

但我想在jetty server舞台上开始pre-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>

2 个答案:

答案 0 :(得分:4)

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

Maven Failsafe Plugin – Usage

它提供了将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阶段:

  

建议您不要直接调用   预集成测试,集成测试或集成后测试阶段   但是,您可以通过指定验证来运行集成测试   相。 [...]

     

这允许您设置集成测试   在预集成测试阶段的环境,运行你的   在集成测试阶段进行集成测试,彻底拆除   集成后测试期间的集成测试环境   在最终检查集成测试结果并失败之前的阶段   必要时构建。

答案 1 :(得分:2)

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

  • 测试变为自包含且不依赖于maven配置
  • 可以在任何IDE中运行