Integration Testing with the Maven Cargo plugin – 用Maven Cargo插件进行集成测试

最后修改: 2011年 10月 16日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

A very common need in the lifecycle of a project is setting up integration testing. In this tutorial, we’ll see how to set up this scenario using the Maven Cargo plugin.

在项目的生命周期中,一个很常见的需求是设置集成测试。在本教程中,我们将看到如何使用Maven Cargo插件设置这一场景。

2. Maven Integration Test Build Phases

2.Maven集成测试构建阶段

Luckily, Maven has built-in support for this exact scenario, with the following phases of the default build lifecycle (from the Maven documentation):

幸运的是,Maven内置了对这种情况的支持,默认构建生命周期的以下阶段(来自Maven 文档):更多

  • pre-integration-test: Perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
  • integration-test: Process and deploy the package if necessary into an environment where integration tests can be run.
  • post-integration-test: Perform actions required after integration tests have been executed. This may including cleaning up the environment.

3. Set Up Cargo Plugin

3.设置货物插件

Let’s go over the setup required, step by step.

让我们一步一步地看一下所需的设置。

3.1. Exclude Integration Tests from the Surefire Plugin

3.1.将集成测试排除在Surefire插件之外

First, the maven-surefire-plugin is configured so that integration tests are excluded from the standard build lifecycle:

首先,对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>

Exclusions are done via ant-style path expressions, so all integration tests must follow this pattern and end with “IntegrationTest.java“.

排除是通过ant风格的路径表达式完成的,所以所有的集成测试必须遵循这个模式,并以“IntegrationTest.java“结尾。

3.2. Configure the Cargo Plugin

3.2.配置 “货物 “插件

Next, the cargo-maven3-plugin is used, as Cargo comes with top-notch out-of-the-box support for embedded web servers. Of course, if the server environment requires a specific configuration, cargo also knows how to construct the server out of an archived package as well as deploy to an external server.

接下来,使用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>

A default embedded Jetty 9 web server is defined, listening on port 8080.

定义了一个默认的嵌入式Jetty 9网络服务器,监听端口为8080。

In the newer version of cargo (1.1.0 upwards), the default value of the wait flag has been changed to false, for cargo:start. This goal should only be used for running integration tests and is bound to the Maven lifecycle; for development, the cargo:run goal should be executed instead – which has wait=true.

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

In order for the package maven phase to generate a deployable war file, the packaging of the project must be <packaging>war</packaging>.

为了让package maven阶段生成一个可部署的war文件,项目的打包必须是<packaging>war</packaging>

3.3. Add a New Maven Profile

3.3.添加一个新的Maven配置文件

Next, a new integration Maven profile is created to enable running the integration tests only when this profile is active, and not as part of the standard build lifecycle.

接下来,我们创建了一个新的集成Maven配置文件,以便在该配置文件激活时运行集成测试,而不是作为标准构建生命周期的一部分。

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

         <plugins>
            ...
         </plugins>

      </build>
   </profile>
</profiles>

It is this profile that will contain all the remaining configuration details.

正是这个配置文件将包含所有剩余的配置细节。

Now, the Jetty server is configured to start in the pre-integration-test phase and stop in the post-integration-test phase.

现在,Jetty服务器被配置为在前集成测试阶段启动,在后集成测试阶段停止

<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>

This ensures the cargo:start goal and cargo:stop goals will execute before and after the integration-test phase. Note that because there are two individual execution definitions, the id element must be present (and different) in both, so that Maven can accept the configuration.

这确保了cargo:start目标和cargo:stop目标在integration-test阶段前后执行。注意,由于有两个单独的执行定义,id元素必须在两个定义中出现(并且不同),这样Maven才能接受该配置。

3.4. Configure Integration Tests in the New Profile

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

Next, the maven-surefire-plugin configuration needs to be overridden inside the integration profile, so that the integration tests which were excluded in the default lifecycle are will now be included and run:

接下来,maven-surefire-plugin配置需要在integration配置文件中被覆盖,这样在默认生命周期中被排除的集成测试就会被纳入并运行。

<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>

There are a few things worth noting:

有几件事值得注意。

1. The test goal of the maven-surefire-plugin is executed in the integration-test phase; at this point, Jetty is already started with the project deployed, so the integration tests should run with no problems.

1.maven-surefire-plugin测试目标在集成测试阶段执行;此时,Jetty已经开始部署项目,所以集成测试的运行应该没有问题。

2. The integration tests are now included in the execution. In order to achieve this, the exclusions are also overridden – this is because of the way Maven handles overriding plugin configurations inside profiles.

2.集成测试现在被纳入执行中。为了实现这一点,排除项也被重写了–这是因为Maven在配置文件中处理重写插件配置的方式。

The base configuration is not completely overridden, but rather augmented with new configuration elements inside the profile.

基本配置并没有被完全覆盖,而是用配置文件内的新配置元素进行了扩充。

Because of this, the original <excludes> configuration, which excluded the integration tests in the first place, is still present in the profile and needs to be overridden, or it would conflict with the <includes> configuration and the tests would still not run.

正因为如此,原始的<excludes>配置(首先排除了集成测试)仍然存在于配置文件中,需要被覆盖,否则它将与<includes>配置冲突,测试仍然无法运行。

3. Note that, since there is only a single <execution> element, there is no need for an id to be defined.

3.请注意,由于只有一个<execution>元素,所以不需要定义id

Now, the entire process can run:

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

mvn clean install -Pintegration

mvn clean install -Pintegration

4. Conclusion

4.结论

The step-by-step configuration of Maven covers the entire process of setting up the integration process as part of the project lifecycle.

Maven的逐步配置涵盖了作为项目生命周期一部分的集成流程设置的整个过程。

Usually, this is set up to run in a Continuous Integration environment, preferably after each commit. If the CI server already has a server running and consuming ports, then the cargo configuration will have to deal with that scenario, which I will cover in a future post.

通常,这被设置为在持续集成环境中运行,最好是在每次提交之后。如果CI服务器已经有一个服务器在运行并消耗端口,那么货物配置就必须处理这种情况,我将在以后的文章中介绍。

For a fully running configuration of this mechanism, checkout out the REST GitHub project.

关于该机制的完整运行配置,请查看REST GitHub项目

Also, check out this article for best practices of structuring a project and organizing the unit and integration tests.

此外,请查看这篇文章,了解构建项目和组织单元测试和集成测试的最佳实践。