FBB FBB - 1 year ago 119
Java Question

Maven Failsafe Plugin: how to use the pre- and post-integration-test phases

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.

I understand that the database should be started during the

phase, and shut down during the
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?

Jcs Jcs
Answer Source

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.

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:

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

      <!-- include the JDBC driver dependency here -->

    <!-- common plugin configuration -->
      <!-- other parameters -->

    <!-- the executions section binds the phases with some plugin goals and optional additional configuration parameters -->
        <!-- specific configuration for this execution -->
          <!-- Include here the SQL scripts to create the DB, inject some test data -->
          <!-- Include here the SQL scripts to drop the database -->

That should do the trick.