DBUnit Plugin
Dependency :
compile ":dbunit:not provided"
Summary
Description
Introduction
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 http://code.google.com/p/dbunit-plugin.So, how to use it?
First of all, install it typing inside you grails application:grails install-plugin http://dbunit-plugin.googlecode.com/files/grails-dbunit-plugin-0.1.zip
grails install-plugin /where/you/download/grails-dbunit-plugin-0.1.zip
Using only one dataset to the test
Consider the following class:class Person {
Long id
String name
String email
static constraints = {
name(blank:false)
email(email:true, unique:true)
}
}<?xml version="1.0" encoding="UTF-8"?> <dataset> <person id="1" name="Pessoa 1" email="p1@email.com" version="1" /> <person id="2" name="Pessoa 2" email="p2@email.com" version="1" /> <person id="3" name="Pessoa 3" email="p3@email.com" version="1" /> <person id="4" name="Pessoa 4" email="p4@email.com" version="1" /> <person id="5" name="Pessoa 5" email="p5@email.com" version="1" /> </dataset>
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 } }
- test/datasets/person/0.data.xml
- test/datasets/person/1.data.xml
- test/datasets/person/2.data.xml
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']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 } }