16

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 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?

FBB
  • 1,414
  • 2
  • 17
  • 29
  • Do you really need a MySQL or would it be sufficient to use an in memory database to test the queries? – khmarbaise Nov 06 '13 at 17:52
  • Well, I'd rather understand how to use these two phases. So, a solution about a MySQL in-memory database would be interesting, but I am more interested in details about these two phases (and I already know about the use of H2 with a MySQL mode. And the only real MySQL in-memory database I found was not updated since 2006...) – FBB Nov 06 '13 at 20:56

1 Answers1

21

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

  <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>

That should do the trick.

Juru
  • 1,623
  • 17
  • 43
Jcs
  • 13,279
  • 5
  • 53
  • 70
  • Great, it looks to be what I need. But then there is no way to launch a MySQL instance? – FBB Nov 06 '13 at 22:52
  • 3
    you may be able to start a mysql instance by using the exec plugin: http://mojo.codehaus.org/exec-maven-plugin/ – wemu Nov 07 '13 at 08:17
  • 2
    I would suggest to use the following plugin: http://www.jcabi.com/jcabi-mysql-maven-plugin/index.html. – khmarbaise Nov 09 '13 at 11:40