Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: Integration testing the Mail Plugin

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

like image 780
Everett Toews Avatar asked Jan 30 '26 10:01

Everett Toews


1 Answers

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.

like image 159
Everett Toews Avatar answered Feb 01 '26 02:02

Everett Toews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!