Contents

Maven Cargo 插件集成测试

1. 概述

项目生命周期中一个非常普遍的需求是设置集成测试。在本教程中,我们将了解如何使用 Maven Cargo 插件设置此场景。

2. Maven 集成测试构建阶段

幸运的是,Maven 内置了对这个确切场景的支持,默认构建生命周期的以下阶段(来自 Maven文档 ):

  • pre-integration-test:在执行集成测试之前执行所需的操作。这可能涉及诸如设置所需环境之类的事情。
  • integration-test:如有必要,处理包并将其部署到可以运行集成测试的环境中。
  • post-integration-test:执行集成测试后执行所需的操作。这可能包括清理环境。

3.设置货运插件

让我们逐步了解所需的设置。

3.1. 从 Surefire 插件中排除集成测试

首先,配置maven-surefire-plugin 以便将集成测试排除在标准构建生命周期之外:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.22.2</version>
   <configuration>
      <excludes>
         <exclude>**/*IntegrationTest.java</exclude>
      </excludes>
   </configuration>
</plugin>

排除 是通过 ant 样式的路径表达式完成的,因此所有集成测试都必须遵循此模式并以“ IntegrationTest.java ”结尾。

3.2. 配置cargo插件

接下来,使用cargo-maven3-plugin ,因为Cargo 为嵌入式 Web 服务器提供了一流的开箱即用支持。当然,如果服务器环境需要特定的配置,cargo 也知道如何从归档包中构建服务器以及部署到外部服务器。

<plugin>
   <groupId>org.codehaus.cargo</groupId>
   <artifactId>cargo-maven3-plugin</artifactId>
   <version>1.9.9</version>
   <configuration>
      <configuration>
         <properties>
            <cargo.servlet.port>8080</cargo.servlet.port>
         </properties>
      </configuration>
   </configuration>
</plugin>

定义了一个默认的嵌入式 Jetty 9 Web 服务器,侦听端口 8080。

在新版本的 cargo(1.1.0 以上)中,对于cargo:start wait标志的默认值已更改为false。这个目标应该只用于运行集成测试并且绑定到 Maven 生命周期;对于开发,应该执行cargo:run 目标——它具有wait=true

为了使package阶段生成可部署的war文件,项目的打包必须是*war*。

3.3. 添加新的 Maven 配置文件

接下来,创建一个新的integration Maven 配置文件,以便仅在此配置文件处于活动状态时才能够运行集成测试,而不是作为标准构建生命周期的一部分。

<profiles>
   <profile>
      <id>integration</id>
      <build>
         <plugins>
            ...
         </plugins>
      </build>
   </profile>
</profiles>

正是此配置文件将包含所有剩余的配置详细信息。

现在,Jetty 服务器配置为在pre-integration-test阶段启动并在集成测试后阶段停止

<plugin>
   <groupId>org.codehaus.cargo</groupId>
   <artifactId>cargo-maven3-plugin</artifactId>
   <executions>
      <execution>
         <id>start-server</id>
         <phase>pre-integration-test</phase>
         <goals>
            <goal>start</goal>
         </goals>
      </execution>
      <execution>
         <id>stop-server</id>
         <phase>post-integration-test</phase>
         <goals>
            <goal>stop</goal>
         </goals>
      </execution>
   </executions>
</plugin>

这确保了cargo:start 目标和cargo:stop 目标将在集成测试阶段之前和之后执行。请注意,因为有两个单独的执行定义,id元素必须在两者中都存在(并且不同),以便 Maven 可以接受配置。

3.4. 在新配置文件中配置集成测试

接下来,需要在集成配置文件中覆盖maven-surefire-plugin配置,以便现在包含并运行在默认生命周期中排除的集成测试:

<plugins>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <executions>
         <execution>
            <phase>integration-test</phase>
            <goals>
               <goal>test</goal>
            </goals>
            <configuration>
               <excludes>
                  <exclude>none</exclude>
               </excludes>
               <includes>
                  <include>**/*IntegrationTest.java</include>
               </includes>
            </configuration>
         </execution>
      </executions>
   </plugin>
</plugins>

有几点值得注意:

  1. maven-surefire-plugin的测试目标在integration-test阶段执行;此时,Jetty 已经开始部署项目,因此集成测试应该可以正常运行。
  2. 集成测试现在包含在执行中。为了实现这一点,排除项也被覆盖——这是因为 Maven 处理配置文件中覆盖插件配置的方式。基本配置没有被完全覆盖,而是在配置文件中增加了新的配置元素。因此,最初排除集成测试的原始<excludes>配置仍然存在于配置文件中并且需要被覆盖,否则它将与<includes>配置冲突并且测试仍然无法运行.
  3. 请注意,由于只有一个<execution>元素,因此无需定义id

现在,整个过程可以运行:

mvn clean install -Pintegration