3
votes

I'm trying to integration test a class that uses the Mail Plugin. When I run my test (grails test-app -integration EmailerIntegration) I get the error:

Could not locate mail body layouts/_email. Is it in a plugin? If so you must pass the plugin name in the [plugin] variable

Is there some initialization code I'm missing from the setUp method of my test case?

Here is the code for the test case:

package company

import grails.test.*

class EmailerIntegrationTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSomething() {
        User owner = new User()
        owner.displayName = "Bob"
        owner.email = "[email protected]"

        Emailer emailer = new Emailer()
        emailer.sendReadyEmail(owner)
    }
}

Here is the code for the class being tested:

package company

import org.apache.log4j.Logger;
import org.codehaus.groovy.grails.commons.ApplicationHolder;
import org.springframework.context.ApplicationContext;

class Emailer {
    private Logger log = Logger.getLogger(this.getClass());
    ApplicationContext ctx = (ApplicationContext)ApplicationHolder.getApplication().getMainContext();
    def mailService = ctx.getBean("mailService");

    def sendReadyEmail = { owner ->
            mailService.sendMail {
                    to owner.email
                    subject "Ready to go"
                    body( view:"layouts/_email", model:[ownerInstance:owner])
            }
    }
}

Thanks,

Everett

2

2 Answers

3
votes

After looking at the plugin author's own tests for the mail plugin at https://github.com/gpc/grails-mail/blob/master/test/integration/org/grails/mail/MailServiceTests.groovy I realized that the paths in the values for the view parameter all begin with a '/'. I changed my method to

def sendReadyEmail = { owner ->
        mailService.sendMail {
                to owner.email
                subject "Ready to go"
                body( view:"/layouts/_email", model:[ownerInstance:owner])
        }

And now it works in integration tests and normal program execution.

0
votes

The body parameter in the sendMail(..) method is a map with the keys view, model, and plugin. A value for plugin is required, and points to some other, supporting, plugin, for instance, the name "email-confirmation" for that corresponding plugin.

Your error message is thrown in org.grails.mail.MailMessageBuilder.renderMailView(Object, Object, Object). You can find this class in your Grails project's plugin folder.

Unfortunately, I haven't found too much documentation on the Mail plugin. Thus, at the moment, I cannot easily tell about how to use the aforementioned supporting plugins. If you can't get forward, however, I might try to further investigate. Thanks