Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can install the <strong>greenMail</strong> plugin, and use it in an <strong>integration</strong> test:</p> <p>From the greenmail plugin home page:</p> <pre><code>import com.icegreen.greenmail.util.* class GreenmailTests extends GroovyTestCase { def mailService def greenMail void testSendMail() { Map mail = [message:'hello world', from:'from@piragua.com', to:'to@piragua.com', subject:'subject'] mailService.sendMail { to mail.to from mail.from subject mail.subject body mail.message } assertEquals(1, greenMail.getReceivedMessages().length) def message = greenMail.getReceivedMessages()[0] assertEquals(mail.message, GreenMailUtil.getBody(message)) assertEquals(mail.from, GreenMailUtil.getAddressList(message.from)) assertEquals(mail.subject, message.subject) } void tearDown() { greenMail.deleteAllMessages() } } </code></pre> <p>I'm not a Spock expert but you should be able to translate this junit test to spock style.</p> <p>Source: <a href="http://grails.org/plugin/greenmail" rel="nofollow">http://grails.org/plugin/greenmail</a></p> <p><strong>Udpate, alternative by mocking sendMail</strong></p> <p>This is an answer to Gregor's update. In my opinion, you would have to mock the sendMail method, and inside this method have an stub that implements the different properties and methods that are used in the closure. Lets call it an evaluator. The you would initialize the closure's delegate to the evaluatro, and execute the closure. The evaluator should have the assertions. You see that I'm using more junit concepts here. I don't know how easily you can translate that into spock concepts. You probably would be able to us the behaviour checking facilities of spock.</p> <pre><code>class MailVerifier { void multiPart(boolean v){ //... } void to(String address){ //... } boolean isVerified() { //check internal state obtained by the appropriate invocation of the methods } } def sendMail(Closure mailDefintion) { def evaluator = createMailVerifier() mailDefinition.delegate = evaluator mailDefinition() assert evaluator.verified } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload