Groovy Documentation

grails.test
[Groovy] Class GrailsMock

java.lang.Object
  grails.test.GrailsMock

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


Property Summary
DemandProxy demand

java.lang.Class mockedClass

 
Constructor Summary
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.

 
Method Summary
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.

java.lang.Object verify()

Checks that all the expected methods have been called.

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), 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

demand

DemandProxy demand


mockedClass

java.lang.Class mockedClass


 
Constructor Detail

GrailsMock

GrailsMock(java.lang.Class clazz)
Creates a new strict mock for the given class.
Parameters:
clazz - The class to mock.


GrailsMock

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

createMock

java.lang.Object 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

java.lang.Object verify()
Checks that all the expected methods have been called. Throws an assertion failure if any expected method call has not occurred.


 

Groovy Documentation