Overriding dateCreated for testing in Grails

后端 未结 10 1555
日久生厌
日久生厌 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:23

    You can try to disable it by setting autoTimestamp = false in the domain class mapping. I doubt about global overriding because the value is taken directly from System.currentTimeMillis() (I'm looking at org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventListener.java).

    So I can only suggest that you override a setter for dateCreated field in your class, and assign your own value. Maybe even metaclass access will work, like

    Date stubDateCreated
    ...
    myDomainClass.metaClass.setDateCreated = 
        { Date d -> delegate.@dateCreated = stubDateCreated }
    

提交回复
热议问题