Search code examples
groovygrails-domain-classgrails-constraints

Grails unit test for domain class insertBefore


How can I test the initBefore method of Groovy Domain-Classes with a unit test in Grails?

I created the dummy object but the beforeInsert-method is not called until myObject.save() is invoked and save is unavailable in the testing environments.

Edit: its a unit-test. there is no error, but the method beforeInsert is not called


Solution

  • beforeInsert is called during unit tests. I can verify this in my tests. A couple things to consider:

    1. ensure you use a beforeInsert method, and not a closure. A closure will not work correctly.
    2. it is called when the object is flushed, so perhaps you are having silent save errors beforehand. In your test when you save the object do .save(flush: true, failOnError: true)