Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

101 lines (80 sloc) 2.399 kb

Grails Time-Travel

Build Status

Grails plugin to mock and test data as if created on a specific date or time.

WARNING: Time-travel is dangerous

The conde in this plugin anipulates the space-time continuum so it's not advised to use it on your production code. The plugin was developed with testing environment in mind so be cautious.

Recommended use

We recommend to use the groovy.time.TimeCategory mIxin on the setup to have a more idiomatic use of the plugin.

class TestDomainServiceIntegrationSpec extends IntegrationSpec {
    def setupSpec() {
        Integer.mixin(groovy.time.TimeCategory)
    }
    ...
}

Then you have to add the TimeTravel mixin to add the time manipulation functions.

@Mixin(org.grails.plugins.timetravel.TimeTravel)
class TestDomainServiceIntegrationSpec extends IntegrationSpec {
    ...
}

Functions

Travel

All the domain objects saved inside the closure code (that have timestamping enabled) will be saved with the creationDate/lastUpdated as the specified time.

When you're ready to travel in time just select your date and make the jump:

void 'My awesome test'() {
    setup:
        travel(10.years.ago) {
            testDomainService.insertDomainObject(name, value)
        }

    when:
        ...

    then:
        ....
}

Jump Forward / Jump Backward

Once you're inside the travel context you can move the internal time clock advancing some time with the jumpForward/jumpBackward method.

void 'My awesome test'() {
    setup:
        travel(10.years.ago) {
            10.times {
                jumpForward 1.year
                testDomainService.insertDomainObject(name, value)
            }
        }

    when:
        ...

    then:
        ....
}
void 'My awesome test'() {
    setup:
        travel(10.years.ago) {
            12.times {
                jumpBackward 1.month
                testDomainService.insertDomainObject(name, value)
            }
        }

    when:
        ...

    then:
        ....
}

Version info

  • 0.1 - 17/Nov/2013 - Initial version
  • 0.2 - 17/Nov/2013 - Fixed problem in plugin descriptor
  • 0.3 - 20/Nov/2013 - Solved problems with flush and listener collision
Jump to Line
Something went wrong with that request. Please try again.