(Quick Reference)

8 Testing - Reference Documentation

Authors: Graeme Rocher

Version: 5.0.0.BUILD-SNAPSHOT

8 Testing

The Redis plugin provides a Groovy mixin called DatastoreUnitTestMixin for testing purposes. This mixin sets up a datastore implementation that operates against an in-memory ConcurrentHashMap. The datastore implementation that operates against an in-memory map is as complete as the one for Redis and provides support for:
  • Simple persistence methods
  • Dynamic finders
  • Criteria
  • Named queries
  • Inheritance

You can easily write tests that use the mixin using Groovy's Mixin annotation on any existing unit test:

import grails.datastore.test.DatastoreUnitTestMixin

@Mixin(DatastoreUnitTestMixin) class PersonTests extends GroovyTestCase { void testPersist() { mockDomain(Person) def s = new Simple(name:"Bob") s.save()

assert s.id != null

s = Simple.get(s.id)

assert s != null }

void tearDown() { disconnect() } }

You should call the mockDomain() method to mock a domain instance and then the remainder of the API is the same. Note that you should call disconnect() in tearDown() otherwise your tests will share data.