MockUtils (Grails 2.3.8)
Date: 2014-04-24

Overview | Package | Class | Deprecated | Index | Help
| Groovy Documentation
| FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | PROPERTY | CONSTR | METHOD | DETAIL: FIELD | PROPERTY | CONSTR | METHOD

## grails.test
[Groovy] Class MockUtils

    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.

Authors:
  Peter Ledbrook

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

### COMPARATORS

    static final java.lang.Object COMPARATORS

### COMPARATORS_RE

    static final java.lang.Object COMPARATORS_RE

### DYNAMIC_FINDER_RE

    static final java.lang.Object DYNAMIC_FINDER_RE

### IDS

    static final java.util.Map IDS

### TEST_INSTANCES

    static java.lang.Object TEST_INSTANCES

### errorsObjects

    static final java.lang.Object errorsObjects

Method Detail

### addCommonWebProperties

    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.

### mockAttributeAccess

    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

### mockCommandObject

    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)

### mockCommandObject

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

### mockController

    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

### mockDomain

    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.

### mockDomain

    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.

### mockDomain

    static GrailsDomainClass mockDomain(java.lang.Class clazz, java.util.List testInstances)

### mockDomain

    static GrailsDomainClass mockDomain(java.lang.Class clazz, java.util.Map errorsMap, java.util.List testInstances = [])

### mockDomain

    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.

### mockLogging

    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.

### mockTagLib

    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.

### prepareForConstraintsTests

    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.

### prepareForConstraintsTests

    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.

### prepareForConstraintsTests

    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.

### resetIds

    static void resetIds()

Groovy Documentation