When you write an integration test for a Grails app, you will likely want to have a clean situation with reproducible test data in your database before the test actually starts. Enter the Fixtures Plugin for Grails.
This plugin helps you to define a fixture, that is a defined set of data used for testing. The plugin defines a DSL in which you can express your need for data. Example:
fixture { guillaume(Author) { name = "Guillaume Laforge" } dierk(Author) { name = "Dierk Koenig" } gina(Book) { title = "Groovy In Action" authors = [guillaume, dierk] } }
Or a little shorter:
fixture { guillaume(Author, name: "Guillaume Laforge") dierk(Author, name: "Dierk Koenig") gina(Book, title: "Groovy In Action", authors: [guillaume, dierk]) }
Then, in your test case, you will want to load that fixture, like this:
class GinaTests extends GroovyTestCase { def fixtureLoader void testFinderMethod() { fixtureLoader.load("gina") def gina = Book.findByTitle("Groovy In Action") assertNotNull(gina) assertEquals(2, gina.authors.size()) } }
Pretty simple, isn’t it? Find out more about how to install and use the Fixtures plugin on its plugin page at grails.org.