Maven Failsafe插件:如何使用集成前和集成后测试阶段 [英] Maven Failsafe Plugin: how to use the pre- and post-integration-test phases

查看:174
本文介绍了Maven Failsafe插件:如何使用集成前和集成后测试阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It is not completely clear to me how to best use the Maven Failsafe plugin for integration tests. My use case would be to test SQL queries against a local MySQL database.

我知道数据库应该在预集成期间启动 - 测试阶段,并在集成后测试期间关闭。
但我如何指定?我应该在我的pom.xml中放一个命令行吗?或者我应该使用特定注释进行注释的方法?

I understand that the database should be started during the pre-integration-test phase, and shut down during the post-integration-test. But how do I specify that? Is there a command line I should put in my pom.xml? Or a method that I should annotate with a specific annotation?

推荐答案

在常规内置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>

这应该可以解决问题。

这篇关于Maven Failsafe插件:如何使用集成前和集成后测试阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆