blob: 3553d2c54e01f5657608d4af9f146bb3b68059f9 [file] [log] [blame]
== Unit Testing
To write unit tests with MongoDB and Spock you can simply extend from `grails.test.mongodb.MongoSpec`.
`MongoSpec` is an abstract class that will initialise GORM in the setup phase of the specification being executed. It
uses by default a `MongoClient` instance that connects to a MongoDB instance as defined in your configuration
(by default, `localhost` and port `27017`, see <<gettingStarted>> for more details):
It is preferable to use testcontainers to automatically run MongoDB in a containerized environment and not have to run a MongoDB instance locally. The following examples use testcontainers:
[source,groovy]
----
include::{sourcedir}/examples/base/src/test/groovy/functional/tests/EmbeddedMongoClient.groovy[]
----
[source,groovy]
----
include::{sourcedir}/examples/base/src/test/groovy/functional/tests/LocalMongoUnitSpec.groovy[tags=structure]
----
You can also use your own low-level `MongoClient` instance, as shown in the following example:
[source,groovy]
----
include::{sourcedir}/examples/base/src/test/groovy/functional/tests/BookUnitSpec.groovy[]
----
Note that the default implementation is to scan your classpath searching for domain classes, from the package defined
in the configuration property `grails.codegen.defaultPackage`, and all the way down its subpackages. If your application
is large, classpath scanning may be slow, so it's better to override the method `getDomainClasses()`:
[source,groovy]
----
include::{sourcedir}/examples/hibernate5/src/test/groovy/functional/tests/BookFongoSpec.groovy[tags=getDomainClasses,indent=0]
----