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.
Type | Name and description |
---|---|
DemandProxy |
demand |
ExplicitDemandProxy |
demandExplicit |
java.lang.Class |
mockedClass |
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. |
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. |
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() |
Creates a new strict mock for the given class.
clazz
- The class to mock.Creates a new mock for the given class.
clazz
- The class to mock.loose
- If true
, a loose-expecation mock is
created, otherwise the mock is strict.Creates a mock instance that can be passed as a collaborator to classes under test.
Returns a "demand" object that supports the "control.demand.myMethod() {}" syntax.
Returns a "demandExplicit" object that supports the "control.demandExplicit.myMethod {}" syntax and checks that myMethod exists on the class
Checks that all the expected methods have been called. Throws an assertion failure if any expected method call has not occurred.
Groovy Documentation