title: Grails : Spock : Unit testing GORM domain class hooks
body: <p>Usually I was ending up writing test cases for a Domain by writing them for constraints and any custom methods(created by us in application) as we know we shouldn't test obvious.</p>
  
  <p>But the time we started using coverage plugin, we found that our domains line of code is not fully covered which was due to gorm hooks(onInsert, beforeUpdate) that we never wrote test cases for.</p>
  
  <p>Is there a way we can test these. One possible way that seems obvious but not suitable is to call another method(containing all code which was earlier in hooks) within these hooks and test that method only and be carefree for hooks.</p>
  
  <p>Any solutions...</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Sample code in domain that I want to unit-test:</p>
  
  <pre><code>class TestDomain{
     String activationDate
     def beforeInsert() {
        this.activationDate = (this.activationDate) ?: new Date()//first login date would come here though
        encodePassword()
     }
  }
  </code></pre>
  
  <p>How can I unit-test beforeInsert or I would end up writing integration test case?</p>