【问题标题】:Maven running jetty on integration-test stageMaven 在集成测试阶段运行码头
【发布时间】:2014-08-04 21:37:27
【问题描述】:

我使用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>

【问题讨论】:

  • 仅供参考:如果您只输入 integration-test 短语,则无法达到集成后测试。

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


【解决方案1】:

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

Maven Failsafe Plugin – Usage

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

Jetty 在pre-integration-test 阶段启动,在vpost-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阶段:

建议不要直接调用 预集成测试、集成测试或集成测试后阶段 而是通过指定验证来运行集成测试 阶段。 [...]

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

【讨论】:

  • 这行得通 - 我使用Maven Failsafe Plugin 实现了类似的设置,以在verify 阶段运行集成测试。
【解决方案2】:

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

  • 测试变得独立,不依赖于 Maven 配置
  • 可以在任何 IDE 中按原样运行测试

【讨论】:

  • 但是使用 maven,集成和单元测试变得孤立/不可靠。这有利于继续集成,例如,您可以先开始单元测试,然后再开始集成测试。不过,关于与 IDE 集成的观点是有道理的。
  • 我总是在不同的源代码树(或包)中分离单元测试和集成测试,这样它们也很容易从 IDE 中分离出来。 (在目录中运行所有测试有点东西......)
  • 我用这个建议作为答案。以编程方式/直接从集成/单元测试运行码头服务器。
  • 好吧-对于单元测试,您不应该启动码头-对吗? ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 2023-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多