At some point between Grails 2.2 and 2.5, the functionality around the
autoTimestamp
flag has changed in a way that has caused it to not function during
integration tests.
What this means is that if you are testing integrations that involve the
creation of a domain that has the autoTimestamp flag, you may be hit with
not-nullable errors:
Obviously the reason for this is that the dateCreated (and lastUpdated if
you use it) field is not being set in the way that it would if the application
was running.
If you are unable to manually add these in your test, (e.g. in my case the
object was being created and saved by a service that was under test - no
chance to mock or modify before the save), you might look to somehow disabling
the autoTimeout and setting the default value
An answer lies in Bootstrap.groovy, where we can catch the grails context
and modify the domain class before the tests are executed.
Note that if you're developing a plugin, you wouldn't normally have a
Bootstrap.groovy file, since its unlikely you're interacting with a
database. But you can create it manually, and it won't get packaged up in your
plugin build.
Bootstrap
In your `Bootstrap.groovy`, you'll need the
following function. This will disable the `autoTimestamp`, and set the
`dateCreated` and `lastUpdated` fields to the current date:
Note that prior to grails 2.4, the GrailsDomainBinder().getMapping() was static.
I found that this didn't work when proccessing all domains, so I specify them
explicitly.
The rest of the Bootstrap.groovy looks like this (Note the conditional check
for Environment - Very important!)