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

FBB*_*FBB 14 java integration-testing maven maven-failsafe-plugin

我不完全清楚如何最好地使用Maven Failsafe插件进行集成测试.我的用例是针对本地MySQL数据库测试SQL查询.

我知道数据库应该在pre-integration-test阶段期间启动,并在关闭期间关闭post-integration-test.但是我该如何指定呢?我应该在我的pom.xml中放一个命令行吗?或者我应该使用特定注释进行注释的方法?

Jcs*_*Jcs 15

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

SQL Maven插件在Maven构建执行SQL脚本.将此插件绑定到的配置pre/post-integration-phase非常简单:

在pom.xml文件的build> plugins部分中,添加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>
Run Code Online (Sandbox Code Playgroud)

这应该够了吧.

  • 您可以使用exec插件启动一个mysql实例:http://mojo.codehaus.org/exec-maven-plugin/ (3认同)
  • 我建议使用以下插件:http://www.jcabi.com/jcabi-mysql-maven-plugin/index.html. (2认同)