I am attempting to write integration tests for a Grails service that does, among other things, send email via the excellent Mail Plugin. I can disable the actual sending of email via configuration, which is great, but I would like to verify that the parameters are correct body is being rendered correctly, or at very least that the method has been called when I expect. As per the documentation, the full path to GSP for the body must be supplied.
As part my test I’d like to do something like this – is there a way to access the email body and other parameters programmatically after sending?
sendMail {
to myemailparams.to
subject myemailparams.subject
body( view:"/emailviews/someemailview",
model:[contentparam: myemailparams.somecontentvalue)
}
//verify correct sending to and subject parameters, and that body contains correct contentvalue
//or at least that the method has been called (Mock it out?)
Note I realize that I can encapsulate the testing of the email body rendering into a separate isolated test that doesn’t involve the mail plugin. But the purpose of this integration test is to ensure a lot of things, including the email send, happen correctly upon call to the service method. I would even be satisfied with an answer that describes how to mock the service, and a check that verifies ‘sendMail’ has been called when expected.
You can override the sendMail method using metaClass and then do some checking to ensure that sendMail was called: