In Grails, is there a good way to mock the current time using Joda time?

前端 未结 3 973
鱼传尺愫
鱼传尺愫 2020-12-30 09:50

I\'m writing some code that does date and time calculations against the current time. In Joda time, this is accessed through a (Java) constructor, as it is an immutable obje

3条回答
  •  长发绾君心
    2020-12-30 10:35

    We ended up creating dateService with now() method. In unit tests we mock it with

    domainInstance.dateService = [ now: { currentTime } ] as DateService
    

    where currentTime is a unit test class field. This imposes everybody's dependency on dateService (our only nearly-global dependency), and for src classes one has to pass it by hand.

    Unit tests, OTOH, look pretty clear with it.

提交回复
热议问题