2

I'm trying to utilize the build-test-data plugin in my Grails (v2.4.3) app to assist with test data creation for unit testing, but while running my unit tests the plugin cannot find TestDataConfig.groovy to load my specified values (for unique constraint tests, etc).


I've installed the plugin via including it in BuildConfig.groovy:

plugins {
    ...
    test ":build-test-data:2.2.0"
    ...
}

I've ran the following command to create the TestDataConfig.groovy template, which places the file at \grails-app\conf\:

grails install-build-test-data-config-template

I've followed the general instructions on the plugin wiki to come up with a properly formatted file:

testDataConfig {
    sampleData {
        '<path>.User' {
            def a = 1
            username = { -> "username${a++}" }
        }
    }
}

(Where path is the fully-qualified class name.)


In my tests, I am using the following general format:

import grails.buildtestdata.TestDataConfigurationHolder
import grails.buildtestdata.mixin.Build
import grails.test.mixin.TestFor
import spock.lang.Specification
import spock.lang.Unroll

@TestFor(User)
@Build(User)
class UserSpec extends Specification {

    def setup() {
        mockForConstraintsTests(User)

        TestDataConfigurationHolder.reset()
        user = User.buildWithoutSave()
    }

    @Unroll
    void "test #field must be unique"() {
        given: 'a User exists'
            user.save(flush: true)

        when: 'another User is created with a non-unique field value'
            def nonUniqueUser = User.buildWithoutSave()
            nonUniqueUser."$field" = user."$field"

        then: 'validation fails and the field has a unique constraint error'
            !nonUniqueUser.validate()
            nonUniqueUser.errors.errorCount == 1
            nonUniqueUser.errors.getFieldError("$field").code == 'unique'

        where:
            field << ['username', '<some other field>']
    }
}

But, when the test is run (using IntelliJ IDEA) TestDataConfig.groovy cannot be found via the following method in the plugin:

static Class getDefaultTestDataConfigClass() {
    GroovyClassLoader classLoader = new GroovyClassLoader(TestDataConfigurationHolder.classLoader)
    String testDataConfig = Holders.config?.grails?.buildtestdata?.testDataConfig ?: 'TestDataConfig'
    try {
        return classLoader.loadClass(testDataConfig)
    } catch (ClassNotFoundException ignored) {
        log.warn "${testDataConfig}.groovy not found, build-test-data plugin proceeding without config file"
        return null
    }
}

So the test continues on without a config file and I do not get uniquely generated data sets.

I've even tried explicitly including the file in Config.groovy:

grails.buildtestdata.testDataConfig = "TestDataConfig"

But, the same method in the plugin shows that Holders.config? is null.


I've looked at a few solutions to a similar problem here on StackOverflow with nothing working in my case; I cannot figure out how to get my app to detect the presence of the TestDataConfig.groovy file.

Any ideas? Thanks so much!

Community
  • 1
  • 1
Synn
  • 21
  • 2

0 Answers0