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?
The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.
The Failsafe Plugin is used during the integration-test and verify phases of the build lifecycle to execute the integration tests of an application. The Failsafe Plugin will not fail the build during the integration-test phase, thus enabling the post-integration-test phase to execute.
maven-surefire-plugin is designed for running unit tests and if any of the tests fail then it will fail the build immediately. maven-failsafe-plugin is designed for running integration tests, and decouples failing the build if there are test failures from actually running the tests.
During Pre-Integration Testing (PIT), the Central Service Providers (CSPs) and Data Service Providers (DSP) work independently to test their own communication processes and data systems.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With