且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven Failsafe插件:如何使用集成前和集成后测试阶段

更新时间:2023-09-18 08:28:10

在常规内置maven生命周期(jar,war ...) pre-integration-test post-integration-test 测试阶段没有绑定到任何maven插件(即默认行为这些阶段无所事事)。如果要为 integration-test 阶段中执行的测试设置和填充数据库,则需要将执行该作业的maven插件绑定到这些阶段。

In the regular built-in maven lifecycles (jar, war...) the pre-integration-test and post-integration-test test phases are not bound to any maven plugin (ie. the default behavior of these phases is "do nothing"). If you want to setup and populate a database for the tests executed in the integration-test phase you need to bind a maven plugin doing that job to these phases.

SQL maven插件执行SQL脚本在maven构建中。将此插件绑定到前/后集成阶段的配置非常简单:

The SQL maven plugin executes SQL script in a maven build. The configuration to bind this plugin to the pre/post-integration-phase is pretty straightforward:

build > plugins pom.xml文件的一部分,添加sql-maven-plugin

In the build>plugins section of the pom.xml file, add the sql-maven-plugin

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <version>1.5</version>
    <dependencies>
      <!-- include the JDBC driver dependency here -->
      <dependency>
        <groupId>...</groupId>
        <artifactId>...</artifactId>
        <version>...</version>
      </dependency>
    </dependencies>

    <!-- common plugin configuration -->
    <configuration>
      <driver>...</driver>
      <url>...</url>
      <username>...</username>
      <password>...</password>
      <!-- other parameters -->
    </configuration>

    <!-- the executions section binds the phases with some plugin goals and optional additional configuration parameters -->
    <executions>
      <execution>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <!-- specific configuration for this execution -->
        <configuration>
          <!-- Include here the SQL scripts to create the DB, inject some test data -->
        </configuration>
      </execution>
      <execution>
        <phase>post-integration-test</phase>
        <goals>
          <goal>execute</goal>
        </goals>
        <configuration>
          <!-- Include here the SQL scripts to drop the database -->
        </configuration>
      </execution>
      [...]
    </executions>
  </plugin>

这应该可以解决问题。