Verify that an email is sent after a form submit.

205 views
Skip to first unread message

Sean Gilligan

unread,
Apr 29, 2015, 6:57:18 PM4/29/15
to geb-...@googlegroups.com
I'm working on some Geb tests for a site that sends a large number of
different email messages, triggered by various operations and states in
the browser.

Is there a Geb/Spock compatible framework/extension/library that I can
use to easily receive and verify email notifications?

Jeff Lowery

unread,
Apr 29, 2015, 8:19:04 PM4/29/15
to geb-...@googlegroups.com
I’ve sent them, haven’t actually tried receiving them, but the concept is the same:

import javax.mail.*
import javax.mail.internet.*


class Mail {
public static void simpleMail(String from, String to,
String subject, String body) throws Exception {

Properties props = new Properties();
props.putAll([
"mail.smtp.host": mail.gibberish.com, // your host and port here
"mail.smtp.port": "25"
]);

Session session = Session.getDefaultInstance(props);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
}
}

The details of receiving instead of sending can be found here:





— Jeff



--
You received this message because you are subscribed to the Google Groups "Geb User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geb-user+u...@googlegroups.com.
To post to this group, send email to geb-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/554161CC.4090407%40msgilligan.com.
For more options, visit https://groups.google.com/d/optout.

Bob Brown

unread,
Apr 30, 2015, 6:24:20 AM4/30/15
to geb-...@googlegroups.com
Greenmail:

https://github.com/greenmail-mail-test/greenmail

“””
GreenMail is an open source, intuitive and easy-to-use test suite of email servers for testing purposes. Supports SMTP, POP3, IMAP with SSL socket support. GreenMail also provides a JBoss GreenMail Service. GreenMail is the fist and only library that offers a test framework for both receiving and retrieving emails from Java.
“””
A relevant linked article:

http://jetlet.blogspot.de/2010/11/integration-testing-mail-function-with.html

On the off-chance that you are using Grails (and if you aren’t, you should be :-)):

https://grails.org/plugin/greenmail

HTH

BOB
> To view this discussion on the web visit https://groups.google.com/d/msgid/geb-user/C39CCC9A-BCEF-41D2-B760-4A3D40EB5A52%40mavericklabel.com.

Marcin Erdmann

unread,
Apr 30, 2015, 6:25:21 AM4/30/15
to geb-...@googlegroups.com
I can see several approaches here in my order of preference and how much work it would be to implement it:

1. When running the app for testing replace the service that does email sending (i.e. smtp communication) with one that stores the email in memory instead. Use remote control to connect to the app at the end of the test and check the email store for the expected emails. It does not allow you to verify that smtp handling is correct but allows you to verify that email sending logic is.
2. Start a fake smtp server (e.g. http://quintanasoft.com/dumbster/) server in your test, configure your app to use it (either in the test using remote control or prior to that using app config files), check if the expected email is sent. Checks that both the email sending logic as well as the code for smtp layer actually works. You can for example use Grails Greenmail plugin (https://grails.org/plugin/greenmail) if you're your app is written in Grails but given that this will run as part of your application and not your test you will need to use remote control to connect to your app and check for the sent emails.
3. Start a real smtp/pop3 server (e.g. http://james.apache.org/server/) at the beginning of your test, configure your app to use it, then use email addresses handled by this server in your test and check for the expected email using a pop3 client library (e.g. http://jodd.org/doc/email.html). Very end2end but way too much work and doesn't seem to have any benefits over 2.

Reply all
Reply to author
Forward
0 new messages