Skip to content

Instantly share code, notes, and snippets.

@emedina
Created June 9, 2011 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emedina/1016842 to your computer and use it in GitHub Desktop.
Save emedina/1016842 to your computer and use it in GitHub Desktop.
package unittestfailwithobjectid
import grails.test.*
import grails.datastore.test.DatastoreUnitTestMixin
import org.bson.types.ObjectId
import org.springframework.core.convert.converter.Converter
@Mixin(DatastoreUnitTestMixin)
class SampleTests extends GrailsUnitTestCase {
protected void setUp() {
super.setUp()
connect()
}
protected void tearDown() {
disconnect()
super.tearDown()
}
void testSomething() {
mockDomain(Sample)
Sample.withSession { session ->
session.datastore.mappingContext.converterRegistry.addConverter(new
Converter<ObjectId, Long>() {
Long convert(ObjectId id) {
return 1.toLong()
}
}
)
session.datastore.mappingContext.converterRegistry.addConverter(new
Converter<Long, ObjectId>() {
ObjectId convert(Long id) {
return new ObjectId(id.toString())
}
}
)
}
def s = new Sample(name: 'Sample1')
s.save()
assert s.id != null
s = Sample.get(s.id)
assert s != null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment