GriffonMock (Griffon 0.9.5-rc2)
Date: 2012-03-09

Overview | Package | Class | Deprecated | Index | Help
| Griffon 0.9.5-rc2
| FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | PROPERTY | CONSTR | METHOD | DETAIL: FIELD | PROPERTY | CONSTR | METHOD

## griffon.test
[Groovy] Class GriffonMock

    java.lang.Object
      griffon.test.GriffonMock

    class GriffonMock

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 Griffon 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 GriffonMock(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 GriffonMock#createMock()#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 GriffonUnitTestCase#mockFor(Class)#mockFor(Class) method instead, since it handles the meta-class management automatically.

Property Summary
`DemandProxy` | `demand`
`Class` | `mockedClass`

Constructor Summary
`GriffonMock(Class clazz)` Creates a new strict mock for the given class.
`GriffonMock(Class clazz, boolean loose)` Creates a new mock for the given class.

Method Summary
`def` | `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.
`def` | `verify()` Checks that all the expected methods have been called.

Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll

Property Detail

### demand

    DemandProxy demand

### mockedClass

    Class mockedClass

Constructor Detail

### GriffonMock

    GriffonMock(Class clazz)

  Creates a new strict mock for the given class.
  Parameters:
    `clazz` - The class to mock.

### GriffonMock

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

### createMock

    def createMock()

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

### getDemand

    DemandProxy getDemand()

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

### verify

    def verify()

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

Groovy Documentation