MockUtils (Grails 2.4.4)
Date: 2014-10-28

Package: grails.test

## [Groovy] Class MockUtils

- grails.test.MockUtils

- A utility/helper class for mocking various types of Grails artifacts and is one of the foundations of the Grails unit testing framework.

  Authors:
    Peter Ledbrook

- - ### Properties Summary

    Properties
    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`

  - ### Methods Summary

    Methods
    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()`

  - ### Inherited Methods Summary

    Inherited Methods
    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()`

- - ### 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<java.lang.Class, java.lang.Long> IDS

    - #### static java.util.Map TEST_INSTANCES

    - #### static final java.lang.Object errorsObjects

  - ### Method Detail

    - #### 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.

      Parameters:
        `clazz` - The class to add the properties to.

    - #### 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

      Parameters:
        `clazz` - The class or interface to mock

    - #### 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()".

      deprecated:
        Use mockCommandObject(Class, Map)

    - #### 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. It adds mock versions of the various methods available on controllers that are normally provided by Grails such as render and redirect.

      Parameters:
        `clazz` - The class of the controller to mock

    - #### static GrailsDomainClass mockDomain(java.lang.Class clazz)

      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.

      deprecated:
        Use mockDomain(Class, Map, List)
      Parameters:
        `clazz` - The domain class to mock.

    - #### static GrailsDomainClass mockDomain(GrailsDomainClass dc, 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.

      deprecated:
        Use mockDomain(Class, Map, List)
      Parameters:
        `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.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 = [])

      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.

      Parameters:
        `dc` - The optional GrailsDomainClass
        `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)

      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.

      Parameters:
        `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)

      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.

      Parameters:
        `clazz` - The class of the tag library to mock.

    - #### 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. 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".

      deprecated:
        Use prepareForConstraintsTests(Class, Map, List)
      Parameters:
        `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.

      deprecated:
        Use prepareForConstraintsTests(Class, Map, List)
      Parameters:
        `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.

      Parameters:
        `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()