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

Package: grails.test

## [Groovy] Class GrailsMock

- grails.test.GrailsMock

- Provides similar behaviour to MockFor and StubFor, but uses ExpandoMetaClass to mock the methods. This means that it fits much better with the rest of the Grails unit testing framework than the classes it replaces.

  Instances of this class support the exact same syntax as MockFor/ StubFor with the addition of explicit support for static methods. For example:

          def mockControl = new GrailsMock(MyDomainClass)
          mockControl.demand.save() {-> return true}           // Instance method
          mockControl.demand.static.get() {id -> return null}  // Static method
          ...
          mockControl.verify()

  You can even create a mock instance of the target class by calling the GrailsMock.createMock method.

  Note that you have to be careful when using this class directly because it uses ExpandoMetaClass to override methods. Any of the demanded methods will stay on the class for the life of the VM unless you either override them or save the previous meta-class and restore it at the end of the test. This is why you should use the GrailsUnitTestCase.mockFor method instead, since it handles the meta-class management automatically.

- - ### Properties Summary

    Properties
    Type | Name and description
    --- | ---
    `DemandProxy` | `demand`
    `ExplicitDemandProxy` | `demandExplicit`
    `java.lang.Class` | `mockedClass`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `GrailsMock (java.lang.Class clazz)` Creates a new strict mock for the given class.
    `GrailsMock (java.lang.Class clazz, boolean loose)` Creates a new mock for the given class.

  - ### Methods Summary

    Methods
    Type | Name and description
    --- | ---
    `java.lang.Object` | `createMock()` Creates a mock instance that can be passed as a collaborator to classes under test.
    `DemandProxy` | `getDemand()` Returns a "demand" object that supports the "control.demand.myMethod() {}" syntax.
    `ExplicitDemandProxy` | `getDemandExplicit()` Returns a "demandExplicit" object that supports the "control.demandExplicit.myMethod {}" syntax and checks that myMethod exists on the class
    `void` | `verify()` Checks that all the expected methods have been called.

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

    - #### DemandProxy demand

    - #### ExplicitDemandProxy demandExplicit

    - #### java.lang.Class mockedClass

  - ### Constructor Detail

    - #### GrailsMock(java.lang.Class clazz)

      Creates a new strict mock for the given class.

      Parameters:
        `clazz` - The class to mock.

    - #### GrailsMock(java.lang.Class clazz, boolean loose)

      Creates a new mock for the given class.

      Parameters:
        `clazz` - The class to mock.
        `loose` - If `true`, a loose-expecation mock is created, otherwise the mock is strict.

  - ### Method Detail

    - #### java.lang.Object createMock()

      Creates a mock instance that can be passed as a collaborator to classes under test.

    - #### DemandProxy getDemand()

      Returns a "demand" object that supports the "control.demand.myMethod() {}" syntax.

    - #### ExplicitDemandProxy getDemandExplicit()

      Returns a "demandExplicit" object that supports the "control.demandExplicit.myMethod {}" syntax and checks that myMethod exists on the class

    - #### void verify()

      Checks that all the expected methods have been called. Throws an assertion failure if any expected method call has not occurred.