Maven 在集成测试阶段运行码头

ses*_*ses 5 java integration-testing jetty maven maven-failsafe-plugin

我用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>
Run Code Online (Sandbox Code Playgroud)

met*_*bed 5

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

Maven 故障安全插件 – 用法

它提供了一个示例配置,用于将 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>
Run Code Online (Sandbox Code Playgroud)

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

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

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


fro*_*rik 2

我更喜欢在测试用例中以编程方式动态启动 jetty。造成这种情况的主要原因是:

  • 测试变得独立并且不依赖于 Maven 配置
  • 可以像在任何 IDE 中一样运行测试

  • 但使用 Maven,集成和单元测试变得孤立/不可靠。这有利于持续集成,例如,您可以先开始单元测试,然后再开始集成测试。不过,与 IDE 集成的观点是有道理的。 (3认同)