A utility/helper class for mocking various types of Grails artifacts and is one of the foundations of the Grails unit testing framework.
Type | Name and description |
---|---|
static java.lang.Object |
COMPARATORS |
static java.lang.Object |
COMPARATORS_RE |
static java.lang.Object |
DYNAMIC_FINDER_RE |
static java.util.Map<java.lang.Class, java.lang.Long> |
IDS |
static java.util.Map |
TEST_INSTANCES |
static java.lang.Object |
errorsObjects |
Type Params | Return Type | Name and description |
---|---|---|
|
static void |
addCommonWebProperties(java.lang.Class clazz) Adds the properties common to controllers and tag libraries (and anything else HTTP based) to objects of the given class. |
|
static void |
mockAttributeAccess(java.lang.Class clazz) Enhances a class that has the method signatures setAttribute/getAttribute (such as the Request object) to allow property style access |
|
static void |
mockCommandObject(java.lang.Class clazz) Mocks a command object class by adding a "validate()" method and errors-related methods, like "getErrors()" and "hasErrors()". |
|
static void |
mockCommandObject(java.lang.Class clazz, java.util.Map errorsMap) Mocks a command object class by adding a "validate()" method and errors-related methods, like "getErrors()" and "hasErrors()". |
|
static void |
mockController(java.lang.Class clazz) Call this to mock the given controller class. |
|
static GrailsDomainClass |
mockDomain(java.lang.Class clazz) Call this to mock the given domain class. |
|
static GrailsDomainClass |
mockDomain(GrailsDomainClass dc, java.lang.Class clazz, java.util.List testInstances) |
|
static GrailsDomainClass |
mockDomain(java.lang.Class clazz, java.util.List testInstances) |
|
static GrailsDomainClass |
mockDomain(java.lang.Class clazz, java.util.Map errorsMap, java.util.List testInstances = []) |
|
static GrailsDomainClass |
mockDomain(GrailsDomainClass dc, java.lang.Class clazz, java.util.Map errorsMap, java.util.List testInstances = []) |
|
static void |
mockLogging(java.lang.Class clazz, boolean enableDebug = false) Provides a mock implementation of the "log" property for the given class. |
|
static void |
mockTagLib(java.lang.Class clazz) Call this to mock the given taglib class. |
|
static void |
prepareForConstraintsTests(java.lang.Class clazz) Adds a validate() method to the given domain class
that performs validation against the constraints and returns a
map of errors. |
|
static void |
prepareForConstraintsTests(java.lang.Class clazz, java.util.List testInstances) |
|
static void |
prepareForConstraintsTests(java.lang.Class clazz, java.util.Map errorsMap, java.util.List testInstances = [], java.util.Map defaultConstraints = [:]) |
|
static void |
resetIds() |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Adds the properties common to controllers and tag libraries (and anything else HTTP based) to objects of the given class.
clazz
- The class to add the properties to.Enhances a class that has the method signatures setAttribute/getAttribute (such as the Request object) to allow property style access
clazz
- The class or interface to mockMocks a command object class by adding a "validate()" method and errors-related methods, like "getErrors()" and "hasErrors()".
Mocks a command object class by adding a "validate()" method and errors-related methods, like "getErrors()" and "hasErrors()".
Call this to mock the given controller class. It adds mock versions of the various methods available on controllers that are normally provided by Grails such as render and redirect.
clazz
- The class of the controller to mock Call this to mock the given domain class. It adds mock versions
of the various static and instance methods that are normally
injected by Grails. The methods behave as if there is no data
in the database, i.e. DomainClass.list()
would
return an empty list.
clazz
- The domain class to mock.
Call this to mock the given domain class. It adds mock versions
of the various static and instance methods that are normally
injected by Grails. The methods behave as if the given list of
domain instances are already in the database. In fact, the finders
and other query methods return the instances in the same order as
they appear in the list testInstances
. This makes
testing much easier as you can rely on this ordering.
clazz
- The domain class to mock.testInstances
- A list of instances of type clazz
or of anything that can act as that type, such as a map with keys
that match the domain class's fields.
Call this to mock the given domain class. It adds mock versions
of the various static and instance methods that are normally
injected by Grails. The methods behave as if the given list of
domain instances are already in the database. In fact, the finders
and other query methods return the instances in the same order as
they appear in the list testInstances
. This makes
testing much easier as you can rely on this ordering.
dc
- The optional GrailsDomainClassclazz
- The domain class to mock.errorsMap
- A map that Errors instances will be stored in.
Each Errors object will be stored against the domain instance it
is attached to, so this should ideally be an "identity" map of
some sort.testInstances
- A list of instances of type clazz
or of anything that can act as that type, such as a map with keys
that match the domain class's fields. Provides a mock implementation of the "log" property for the
given class. By default, debug and trace levels are ignored
but you can enable printing of debug statements via the
enableDebug
argument.
clazz
- The class to add the log method to.enableDebug
- An optional flag to switch on printing of
debug statements.Call this to mock the given taglib class. It adds mock versions of the various methods and properties available on tag libraries that are normally provided by Grails. For example, it adds "flash", "request", and the "render" tag. Other tags should be mocked separately.
clazz
- The class of the tag library to mock. Adds a validate()
method to the given domain class
that performs validation against the constraints and returns a
map of errors. Each key in the map is the name of a field with
at least one error, while the value is the name of the constraint
that triggered the error, e.g. "nullable", or "min".
clazz
- The domain class to mock.
Adds a validate()
method to the given domain class
that performs validation against the constraints and returns a
map of errors. Each key in the map is the name of a field with
at least one error, while the value is the name of the constraint
that triggered the error, e.g. "nullable", or "min".
The main use for this version of the method is to test unique constraints. The domain instance being validated will be checked against the given list of objects and if a uniqueness constraint is violated, it will appear in the returned list of errors.
clazz
- The domain class to mock.testInstances
- A list of instances of type clazz
or of anything that can act as that type, such as a map with keys
that match the domain class's fields. In fact, the instances only
need properties that match the fields taking part in the unique
constraints.
Adds a validate()
method to the given domain class
that performs validation against the constraints and returns a
map of errors. Each key in the map is the name of a field with
at least one error, while the value is the name of the constraint
that triggered the error, e.g. "nullable", or "min".
The main use for this version of the method is to test unique constraints. The domain instance being validated will be checked against the given list of objects and if a uniqueness constraint is violated, it will appear in the returned list of errors.
clazz
- The domain class to mock.errorsMap
- A map that Errors instances will be stored in.
Each Errors object will be stored against the domain instance it
is attached to, so this should ideally be an "identity" map of
some sort.testInstances
- A list of instances of type clazz
or of anything that can act as that type, such as a map with keys
that match the domain class's fields. In fact, the instances only
need properties that match the fields taking part in the unique
constraints.