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 {
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(){

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:

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

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

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



Codezilla said...

If I try instead to make author a foreign key I can't get dbUnit to read my dataset.xml (I get error: non-uppercase input column: author)

author id="1" version="1" firstname="Mark" lastname="Twain"/
author id="2" version="1" firstname="Issac" lastname="Asimov"/
author id="3" version="1" firstname="Sarah" lastname="Zettel"/
book id="1" version="1""1" title="The Adventures of Tom Sawyer"/
book id="2" version="1""1" title="The Prince and the Pauper"/
book id="3" version="1""2" title="I, Robot"/
book id="4" version="1""3" title="Reclamation"/

David Holbrook said...

I've never seen that error before. Your column name of may be causing problems, I usually use the underscore "_" for database column names like author_id

You may want to check the dbunit users mailing list to find someone who can answer dbunit specific questions for you.

Codezilla said...

I was using grails notation rather than sql - it works now:

author id="1" version="1" firstname="Mark" lastname="Twain"
book id="1" version="1" author_id="1" title="The Adventures of Tom Sawyer"

Post a Comment