15

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

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

4

1 回答 1

20

在常规的内置 maven 生命周期(jar、war ...)中,pre-integration-testpost-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>

这应该够了吧。

于 2013-11-06T22:35:23.877 回答