in grails groovy build-test-plugin ID batch ~ read.

build-test-data plugin: how to generate IDs for domain entities in a batch

I have faced an inssue on grails project: domain entities has assigned id, so in tests I have to manually set up id for every created entity:

new Entity(id: id_value, ...).save()

It is very verbose and creates additional task to create and maintain id variable. In addition, there is a build-test-data plugin in our tests, so I was wondering if it is possible to get assist from this plugin to generate IDs? And I found a solution: you have to add in TestDataConfig.groovy such expression:

testDataConfig {
    sampleData {
        ...
        Holders.grailsApplication.domainClasses.each { domainClass ->
            if (domainClass.fullName.startsWith('domain.package.to.get.ids')) {
                this."${domainClass.fullName}" {
                    long i = 1
                    this.id = { -> i++ }
                }
            }
        }
    }
}

Expression will generate next code for all domains in package domain.package.to.get.ids:

'domain.package.to.get.ids.SomeEntity' {
        long i = 1
        this.id = { -> i++ }
    }