Grails and DBUnit integration testing

I need to do a bit of integration testing with Grails and both of the DBUnit plugins I found didn’t seem to be maintained.  To create a simple base class for DBUnit based integration tests turned out to be relatively simple to create.

For the purposes of this sample I created a grails project called bookstore with a single domain class called Book in the bookstore package.  This is what book domain class looks like:

package bookstore

class Book {
String title
String author
}


First I had to add the DBUnit dependency to /grails-app/conf/BuildConfig.groovy and also enable the maven central repository.  This is what the grails.project.dependency.resolution section now looks like:



grails.project.dependency.resolution = {
inherits( "global" ) {
}
log "warn"
repositories {
grailsPlugins()
grailsHome()
mavenCentral()
}
dependencies {
test 'org.dbunit:dbunit:2.4.7'
}
}


Next I created a base class for my DBUnit based tests.  I created a class named DbunitGroovyTestCase in /test/integration/bookstore.  The DataSource property named dataSource is injected from the Spring container when the integration tests are run, and I used the DatabaseDataSourceConnection class to create the DBUnit connection.



package bookstore

import javax.sql.DataSource;

import org.dbunit.database.DatabaseDataSourceConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder
import org.dbunit.dataset.xml.FlatXmlDataSet
import org.dbunit.dataset.IDataSet
import org.dbunit.operation.DatabaseOperation;

import groovy.util.GroovyTestCase;

class DbunitGroovyTestCase extends GroovyTestCase {

DataSource dataSource
IDatabaseConnection connection

protected void setUp() {
connection = new DatabaseDataSourceConnection(dataSource)
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet)
}

protected void tearDown(){
connection.close()
}

protected IDataSet getDataSet() {
return new FlatXmlDataSetBuilder().build(new FileInputStream("test/dbunit/dataset.xml"));
}

}


Here is a sample data set I created for the book table, located in test/dbunit/dataset.xml:



<dataset>
<book id="1" version="1" author="Mark Twain" title="The Adventures of Tom Sawyer"/>
<book id="2" version="1" author="Mark Twain" title="The Prince and the Pauper"/>
<book id="3" version="1" author="Mark Twain" title="Adventures of Huckleberry Finn"/>
<book id="4" version="1" author="Mark Twain" title="A Connecticut Yankee in King Arthur's Court"/>
</dataset>


Finally here is a sample integration test, located in test/integration/bookstore/BookPersistenceTest.groovy



package bookstore;

import grails.test.*

class BookPersistenceTest extends DbunitGroovyTestCase {

public void testFindAllByAuthor() throws Exception {
def books = Book.findAllByAuthor("Mark Twain")
assertEquals(4,books.size)
}

public void testFindByTitle() throws Exception {
def book = Book.findByTitle("Adventures of Huckleberry Finn")
assertEquals("Mark Twain",book.author)
}

}