I've seen some people use the maven-sql-plugin to do this. But it seems like a task that is better suited for DBUnit....perhaps at the beginning of an entire test suite.
What's the best practice here?
Junit Framework can be integrated with Eclipse, Ant and Maven, but in this article we will be using Maven.
We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.
Testing With the 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. We can name test classes with different patterns for those plugins to pick up the enclosed tests separately.
I use the Maven SQL Plugin
You're much better off using it and making sure that you create and populate before your tests and then drop after your tests. You'll also want to use create or replace, or drop if exists in your creation script (assuming your database supports it) in the event that a test fails and leaves the database in some inconsistent state.
It took some fiddling around, but I got it to drop, create, and create the schema for H2 and MySQL. Still need to finish it for Oracle and SQL*Server 2008. I tucked the exact DROP and CREATE commands into properties and in some cases (such as H2) needed to skip the create database altogether. Here is what it looks like:
<plugin>
<!-- Used to automatically drop (if any) and create a database prior to running integration test cases. -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<dependencies>
<dependency>
<!-- Adds the correct JDBC driver as a dependency of this plugin -->
<groupId>${database.groupId}</groupId>
<artifactId>${database.artifactId}</artifactId>
<version>${database.version}</version>
</dependency>
</dependencies>
<configuration>
<!-- common configuration shared by all executions -->
<driver>${database.class}</driver>
<username>${database.username}</username>
<password>${database.password}</password>
<url>${database.url}</url>
</configuration>
<executions>
<execution>
<!-- Start by dropping the database (we'll leave it intact when finished) -->
<id>drop-db</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- Can't use regular URL in case database doesn't exist -->
<url>${database.url.alternate}</url>
<skip>${database.sqlDrop.skip}</skip>
<autocommit>true</autocommit>
<sqlCommand>${database.sqlDrop};</sqlCommand>
<onError>continue</onError>
</configuration>
</execution>
<execution>
<!-- then create a new database -->
<id>create-db</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- Can't use regular URL in case database doesn't exist -->
<url>${database.url.alternate}</url>
<skip>${database.sqlCreate.skip}</skip>
<autocommit>true</autocommit>
<sqlCommand>${database.sqlCreate};</sqlCommand>
<onError>continue</onError>
</configuration>
</execution>
<execution>
<!-- and finally run the schema creation script we just made with the hibernate3-maven-plugin -->
<id>create-schema</id>
<phase>pre-integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<skip>${database.sqlSchema.skip}</skip>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>target/hibernate3/sql/create-${database.vendor}-schema.sql</srcFile>
</srcFiles>
<onError>continue</onError>
</configuration>
</execution>
</executions>
</plugin>
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