1. for not confusing the stubs, I suggest to rename the current stub()
method to anyTimes().
2. DSL for stubs:
def mailer = stub(MailService) // stubs are nice, i.e., if no result is
set for a method, a default null value
will be returned
mailer.toString.returns('mailer') // any method named 'toString' will
return 'mailer', no matter what
params are passed in. BUT it is
inconsistent with the property
mocking, it may need more discuss
mailer.reset.raises(RuntimeException) // methods named 'reset' will
throw an exception
mailer.address.getter.returns('some address') // getter of address will
return 'some address'
mailer.send.params('message', 'user') // name the params of send
mailer.receive.params(1: 'timeout') // name the second param of receive
mailer.close().returns('success') // the close method without params
will return 'success'
play {
2.times { mailer.send 'test' }
mailer.address = 'some address'
def address = mailer.address
}
assert mailer.send.called // 'called' returns the times of call of the
method
assert mailer.send.called == 2
assert mailer.send[0].params[0] == 'test' // the first param of the
first call of 'send'
assert mailer.send[1].params.message == 'test' // the param named
'message' of the
second call of 'send'
assert mailer.send[1].message == 'test' // shortcut of 'params.message'
assert mailer.send('test').called // get the times of call with
specified params
assert mailer.address.setter.called // get the times of call of setter
assert mailer.address.setter[0].value == 'some address' // the value of
the first
call of the
setter
assert mailer.address.set('some address').called in 1..2 // setter with
specified
value
assert mailer.address.getter.called < 2 // times of call of getter