Search code examples
unit-testinggrailsgroovygrails-plugin

using build-test-data plugin with Grails 2


I'm trying to use the build-test-data plugin (v. 2.0.4) to build test data in a unit test of a Grails 2.1.4 application.

The app has the following domain classes

class Brochure {

  static constraints = {}

  static hasMany = [pageTags: PageTag]    
}

class PageTag {

    static constraints = {
    }

    static belongsTo = [brochure: Brochure]
}

Then in my unit test I try to build an instance of PageTag with

@Build([Brochure, PageTag])
class BrochureTests {

    void testSomething() {
       PageTag pageTag = PageTag.build()
    }
}

But it fails with the error

groovy.lang.MissingMethodException: No signature of method: btd.bug.Brochure.addToPageTags() is applicable for argument types: (btd.bug.PageTag) values: [btd.bug.PageTag : (unsaved)] Possible solutions: getPageTags()

My example looks exactly the same as that shown in the plugin's docs, so I've no idea why this isn't working. A sample app that demonstrates the issue is available here.


Solution

  • Fixed in version 2.0.5

    I commented on the linked github issue, but this is because of a perf "fix" in how grails @Mock annotation works.

    This change pretty much removes all of the linking code that made it possible for BTD to work in unit tests.

    The only way around it currently is to also add an explict @Mock annotation for all of the domain objects in the part of the domain graph that's required to build a valid object.

    The test code will be quicker with this change, which is great, but it puts a larger burden on the developer to know and maintain these relationships in their tests (which is what BTD was trying to avoid :).