Overriding dateCreated for testing in Grails

后端 未结 10 1397
日久生厌
日久生厌 2021-02-12 12:27

Is there any way I can override the value of dateCreated field in my domain class without turning off auto timestamping?

I need to test controller and I ha

10条回答
  •  北荒
    北荒 (楼主)
    2021-02-12 13:19

    I got this working by simply setting the field. The trick was to do that after the domain object has been saved first. I assume that the dateCreated timestamp is set on save and not on object creation.

    Something along these lines

    class Message {
      String content
      Date dateCreated
    }
    
    // ... and in test class
    
    def yesterday = new Date() - 1
    def m = new Message( content: 'hello world' )
    m.save( flush: true )
    m.dateCreated = yesterday
    m.save( flush: true )
    

    Using Grails 2.3.6

提交回复
热议问题