Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Home

stokito edited this page · 6 revisions

The Build Test Data Grails Plugin

Creating maintainable test data is hard. Often an entire object graph needs to be created to support the instantiation of a single domain object. This leads to either the cutting and pasting of that creation code, or relying on a canned set of objects that we've grown over time and maintained as the domain objects change. After a while, adding just one more Widget to that set of canned data ends up breaking tests just about every time.

There has to be a better solution, right?

Yep! Due to the power and the glory of Grails, we have a lot of metadata at our fingertips about those domain objects. We know what constraints we've placed on our objects, and which objects depend on other objects to live.

Using this additional information, we've created a grails plugin that makes it easy to just provide those values that you want to exercise under test and not worry about the rest of the object graph that you need to create just to instantiate your domain objects.

This plugin is focused on creating test data for integration testing. As of Grails 2.0.0, build-test-data also supports unit tests through annotations and mixins.

Once installed, all you have to do is call the new "build" method on your domain class and you'll be given a valid instance with all of the required constraints given values.

    def a = Author.build()

In a unit test, you'll just use an annotation to let it know what domain class you'd like to build

    import grails.buildtestdata.mixin.Build

    @Build(Author)
    class AuthorUnitTests {

        void testAuthorStuff() {
            def author = Author.build()
        ...

Plugin Objectives

Something went wrong with that request. Please try again.