|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | PROPERTY | CONSTR | METHOD | DETAIL: FIELD | PROPERTY | CONSTR | METHOD |
java.lang.Object grails.test.MockUtils
class MockUtils extends java.lang.Object
A utility/helper class for mocking various types of Grails artifacts and is one of the foundations of the Grails unit testing framework.
Property Summary | |
---|---|
static java.lang.Object |
COMPARATORS
|
static java.lang.Object |
COMPARATORS_RE
|
static java.lang.Object |
DYNAMIC_FINDER_RE
|
static java.util.Map |
IDS
|
static java.lang.Object |
TEST_INSTANCES
|
static java.lang.Object |
errorsObjects
|
Method Summary | |
---|---|
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(java.lang.Class clazz, java.util.List testInstances)
|
static GrailsDomainClass
|
mockDomain(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 |
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 java.lang.Object | |
---|---|
java.lang.Object#wait(), java.lang.Object#wait(long, int), java.lang.Object#wait(long), 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() |
Property Detail |
---|
static final java.lang.Object COMPARATORS
static final java.lang.Object COMPARATORS_RE
static final java.lang.Object DYNAMIC_FINDER_RE
static final java.util.Map IDS
static java.lang.Object TEST_INSTANCES
static final java.lang.Object errorsObjects
Method Detail |
---|
static void addCommonWebProperties(java.lang.Class clazz)
clazz
- The class to add the properties to.
static void mockAttributeAccess(java.lang.Class clazz)
clazz
- The class or interface to mock
static void mockCommandObject(java.lang.Class clazz)
static void mockCommandObject(java.lang.Class clazz, java.util.Map errorsMap)
static void mockController(java.lang.Class clazz)
clazz
- The class of the controller to mock
static GrailsDomainClass mockDomain(java.lang.Class clazz)
DomainClass.list()
would
return an empty list.clazz
- The domain class to mock.
static GrailsDomainClass mockDomain(java.lang.Class clazz, java.util.List testInstances)
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.
static GrailsDomainClass mockDomain(java.lang.Class clazz, java.util.Map errorsMap, java.util.List testInstances = [])
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.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.
static void mockLogging(java.lang.Class clazz, boolean enableDebug = false)
enableDebug
argument.
clazz
- The class to add the log method to.enableDebug
- An optional flag to switch on printing of
debug statements.
static void mockTagLib(java.lang.Class clazz)
clazz
- The class of the tag library to mock.
static void prepareForConstraintsTests(java.lang.Class clazz)
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.
static void prepareForConstraintsTests(java.lang.Class clazz, java.util.List testInstances)
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.
static void prepareForConstraintsTests(java.lang.Class clazz, java.util.Map errorsMap, java.util.List testInstances = [], java.util.Map defaultConstraints = [:])
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.
static void resetIds()
Groovy Documentation