Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7979115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:41:15+00:00 2026-06-04T09:41:15+00:00

I’d like to test a Grails controller that is sending out emails using the

  • 0

I’d like to test a Grails controller that is sending out emails using the grails Email plugin. I’m at a loss exactly how to mock the sendMail closure in order for interactions to work. Here’s my latest version of the test code:

def 'controller should send a multipart email'() {
    given: 'a mocked mailService'
        controller.mailService = Mock(grails.plugin.mail.MailService)
        controller.mailService.sendMail(*_) >> Mock(org.springframework.mail.MailMessage)
    when:
        controller.sendNow()
    then:
        1* _.multipart(true)
}

The controller code looks something like what you’d expect, e.g.:

def mailService
def sendNow() {
    mailService.sendMail {
        multipart true
        to 'example@example.org'
        from 'me@here.com'
        subject 'a subject'
        body 'a body'
    }
}

If I run this test, I get 0 invocations of my multipart interaction instead of 1. The second line of the given: block seems suspicious to me, but if I try to mock a Closure instead of org.springframework.mail.MailMessage my test crashes. I should also mention that the controller itself works as expected (it couldn’t wait for me to figure out the unit tests first).

Edited

Aha, looking at the code with a fresh mind a few hours later, I can see why the above code does not work; in order for me to catch multipart and other DSL calls, I would have to mock the closure itself, not the sendMail method (and I can’t do that since the closure is defined inside the controller itself). What I probably can do is check the arguments to the sendMail method to see everything necessary was passed into it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:41:17+00:00Added an answer on June 4, 2026 at 9:41 am

    You can install the greenMail plugin, and use it in an integration test:

    From the greenmail plugin home page:

    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()
        }
    }
    

    I’m not a Spock expert but you should be able to translate this junit test to spock style.

    Source: http://grails.org/plugin/greenmail

    Udpate, alternative by mocking sendMail

    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.

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.