blob: 7ebf58989f2fa14d92ed3e77884121a456c856e0 [file] [log] [blame]
For many simple cases you can use the built in [GORM unit testing|http://grails.org/doc/latest/guide/testing.html#unitTestingDomains] provided by Grails.
For more complex cases a @grails.test.mixin.mongodb.MongoDbTestMixin@ class is provided which can run tests against a running MongoDB instance or a mock instance (such as one provided by the [Fongo|https://github.com/fakemongo/fongo] project).
Example against a running MongoDB instance:
{code}
import org.bson.types.ObjectId
@Entity
class Person {
ObjectId id
Long version
String name
}
...
import grails.test.mixin.mongodb.*
@TestMixin(MongoDbTestMixin)
class PersonSpec extends Specification{
void "Test count people"() {
given:"A mongo domain model"
mongoDomain([Person])
expect:"Count the number of people"
Person.count() == 0
}
}
{code}
Example using [Fongo|https://github.com/fakemongo/fongo]:
{code}
import com.github.fakemongo.Fongo
import grails.test.mixin.mongodb.*
@TestMixin(MongoDbTestMixin)
class PersonSpec extends Specification{
void "Test count people"() {
given:"A mongo domain model"
Fongo fongo = new Fongo("mongo server 1");
mongoDomain(fongo.mongo, [Person])
expect:"Count the number of people"
Person.count() == 0
}
}
{code}
This library dependency is required in grails-app/conf/BuildConfig.groovy for adding support for @MongoDbTestMixin@.
{code}
dependencies {
test 'org.grails:grails-datastore-test-support:1.0.1-grails-2.4'
}
{code}
Fongo dependency example
{code}
dependencies {
test 'com.github.fakemongo:fongo:1.5.4'
}
{code}
h4. Configuring domain classes for MongoDbTestMixin tests with the Domain annotation
The @grails.test.mixin.gorm.Domain@ annotation is used to configure the list of domain classes that gets configured when the unit test runtime is initialized.
@Domain@ annotations will be collected from several locations:
* the annotations on the test class
* the package annotations in the package-info.java/package-info.groovy file in the package of the test class
* each super class of the test class and their respective package annotations
* the possible @[SharedRuntime|api:grails.test.runtime.SharedRuntime]@ class
@Domain@ annotations can be shared by adding them as package annotations to package-info.java/package-info.groovy files or by adding them to a @[SharedRuntime|api:grails.test.runtime.SharedRuntime]@ class which has been added for the test.
It's not possible to use DomainClassUnitTestMixin's @Mock@ annotation in MongoDbTestMixin tests. Use the @Domain@ annotation in the place of @Mock@ in MongoDbTestMixin tests.
Example:
{code}
import grails.test.mixin.TestMixin
import grails.test.mixin.gorm.Domain
import spock.lang.Specification
@Domain(Person)
@TestMixin(MongoDbTestMixin)
class DomainAnnotationSpec extends Specification{
void "should allow registering domains with Domain annotation"() {
given:
def person = new Person(name:'John Doe')
def personId = person.save(flush:true, failOnError:true)?.id
expect:"Dynamic finders to work"
Person.count() > 0
Person.get(personId).name == 'John Doe'
}
}
{code}