////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

Use the link:{api}grails/testing/gorm/DomainUnitTest.html[`grails.testing.gorm.DomainUnitTest`]
trait to unit test single domain class.

[source,groovy]
.grails-app/domain/demo/Person.groovy
----
include::{functionalSourceDir}/demo33/grails-app/domain/demo/Person.groovy[tags=basic_declaration,indent=0]
----

[source,groovy]
.src/test/groovy/demo/PersonSpec.groovy
----
include::{functionalSourceDir}/demo33/src/test/groovy/demo/PersonSpec.groovy[tags=basic_declaration,indent=0]
----

Alternatively, the link:{api}grails/testing/gorm/DataTest.html[`grails.testing.gorm.DataTest`]
trait may be used.  When using `DataTest`, an explicit call to the
link:{api}grails/testing/gorm/DataTest.html#mockDomain(Class<?>)[mockDomain] or
link:{api}grails/testing/gorm/DataTest.html#mockDomains(Class<?>)[mockDomains]
method may be used to specify which domain class(es) should be mocked for this
test. This is useful when mocking more than one Domain class at a time to test persistence.

[source,groovy]
.src/test/groovy/demo/DataTestTraitSpec.groovy
----
include::{functionalSourceDir}/demo33/src/test/groovy/demo/DataTestTraitSpec.groovy[tags=basic_declaration,indent=0]
----

Another way to express which domain classes should be mocked for this test is
to provide a `Class[] getDomainClassesToMock()` method in the test.

[source,groovy]
.src/test/groovy/demo/GetDomainClassesToMockMethodSpec.groovy
----
include::{functionalSourceDir}/demo33/src/test/groovy/demo/GetDomainClassesToMockMethodSpec.groovy[tags=basic_declaration,indent=0]
----

When mocking domain classes in a test for another artifact type (like a
`ControllerUnitTest` test, for example), the test must implement the `DataTest`
trait in order to mock the related domain classes.

[source,groovy]
.src/test/groovy/demo/PersonControllerSpec.groovy
----
include::{functionalSourceDir}/demo33/src/test/groovy/demo/PersonControllerSpec.groovy[tags=basic_declaration,indent=0]
----
