Plugins You can find out about all the publicly available Grails plugins.

DBUnit Plugin

  • Authors: Marc Palmer
0 vote
compile "org.grails.plugins:dbunit:not provided"




In this page you will see how to use the DBUnit-Plugin, a plugin that allows the usage of DBUnit datasets (think about them as test fixtures that use databases) in Grails. This plugin does not aim to cover all the points displayed in the proposal presented in the Grails wiki, but uses some ideas described there.

For a while, the project is hosted at

So, how to use it?

First of all, install it typing inside you grails application:

grails install-plugin
Or download the new version and install it using:
grails install-plugin /where/you/download/
The plugin is prepared to deal with datasets written as flat xml, as it is the approach I consider the easiest.

Let's take a look at some examples:

Using only one dataset to the test

Consider the following class:

class Person {
	Long id
	String name
	String email
	static constraints = {
		email(email:true, unique:true)
It is mapped to a table in the database with the columns id, version (automatically added by Grails), name and email. So, we should create a dataset as follows:
<?xml version="1.0" encoding="UTF-8"?>
	<person id="1" name="Pessoa 1" email="[email protected]" version="1" />
	<person id="2" name="Pessoa 2" email="[email protected]" version="1" />
	<person id="3" name="Pessoa 3" email="[email protected]" version="1" />
	<person id="4" name="Pessoa 4" email="[email protected]" version="1" />
	<person id="5" name="Pessoa 5" email="[email protected]" version="1" />
In order to the plugin correctly load this dataset, some conventions must be adopted:

Where to locate the dataset

The directory where the datasets should be located is test/datasets: inside this directory, sub-directories should be created to separate each set of datasets to the tests. For instance, if the test specifies that it will use the dataset "person", so create a directory test/datasets/person to contain the DBUnit xml files.

How to indicate to the test class which datasets to use

Totally simple! See next:

class PersonTests extends GroovyTestCase {

static datasets = ['person']

void testCountPerson() { def c = Person.count() assertEquals 5, c } }

The static property "datasets" is a list with all necessary datasets to that test. In this case, all .xml files in test/datasets/person will be loaded. If you save the xml previously described in test/datasets/person/data.xml, the plugin will load the described data and will let them available for the test.

Tip: if you want, you can indicate the order the files will be loaded, numerating them as follows:

  • test/datasets/person/
  • test/datasets/person/
  • test/datasets/person/
Note that the ordering is lexicographic.

Indicating more than one dataset per test

You only have to add all the datasets you want to the list:

static datasets = ['person', 'city', 'country']
Here, the datasets will be loaded in the order they appear in the list. Thus, first the files in test/datasets/person/ will be loaded, then the files in test/datasets/city/ and so on.

Datasets in "packages"

It is possible to separate the datasets in a structure similar to packages. For the following test class:

class PersonTests extends GroovyTestCase {

static datasets = ['com/google/dbunit-plugin/person']

void testCountPerson() { def c = Person.count() assertEquals 5, c } }

The plugin will list the ".xml" files in test/datasets/com/google/dbunit-plugin/person/. Be aware for the fact that the plugin will NOT list the files present in the sub-directories, only in the directory shown.


The datasets load/unload for a test happen only one time for each test class, in the same way as the JUnit anotation @BeforeClass. It works like this because the Grails integration tests are transactional and so it is not necessary to "clean" the environment for each test case (_in a future release, the plugin will decide whether it needs to do this if the test is not transactional_).

The operations executed in the datasets load/unload are, respectively, CLEAN_INSERT e DELETE.