title: Overriding dateCreated for testing in Grails
body: <p>Is there any way I can override the value of <code>dateCreated</code> field in my domain class without turning off auto timestamping? </p>
  
  <p>I need to test controller and I have to provide specific domain objects with specific creation date but GORM seems to override values I provide.</p>
  
  <h2>Edit</h2>
  
  <p>My classes look like this:</p>
  
  <pre><code>class Message {
  
      String content
      String title
      User author
  
      Date dateCreated
      Date lastUpdated
  
      static hasMany = [comments : Comment]
  
      static constraints = {
          content blank: false
          author nullable: false
          title nullable: false, blank: false
      }
  
      static mapping = {
          tablePerHierarchy false
          tablePerSubclass true
          content type: "text"
          sort dateCreated: 'desc'
      }
  }
  
  class BlogMessage extends Message{
  
      static belongsTo = [blog : Blog]
  
      static constraints = {
          blog nullable: false
      }
  
  }
  </code></pre>
  
  <p>I'm using console to shorten things up. The problem which I encountered with Victor's approach is, when I write:</p>
  
  <pre><code>Date someValidDate = new Date() - (20*365)
  
  BlogMessage.metaClass.setDateCreated = {
              Date d -&gt;            
              delegate.@dateCreated = someValidDate
  }
  </code></pre>
  
  <p>I get following exception:</p>
  
  <pre><code>groovy.lang.MissingFieldException: No such field: dateCreated for class: pl.net.yuri.league.blog.BlogMessage
  </code></pre>
  
  <p>When I tried</p>
  
  <pre><code>Message.metaClass.setDateCreated = {
                  Date d -&gt;            
                  delegate.@dateCreated = someValidDate
  }
  </code></pre>
  
  <p>Script goes well, but unfortunately <code>dateCreated</code> is not being altered.</p>