Integration testing emails

Recently I received a task where I had to modify a function I’ve wrote a long time ago that send out emails to users based on some criteria. The problem is that when I wrote that function I:

1. Didn’t have that good understanding and knowledge of Alfresco
2. Alfresco’s email sending capabilities weren’t as good as today
3. It was for a 3.x version of Alfresco

I then decided to first write a test for the function. The thing is that the essential thing in the function is the actual subject, content and other properties of the mail. I also want this to work regardless of where the test is executed (dev computer, CI environment, etc). I googled some and found some rather nice frameworks for setting up a simple SMTP server in a junit test. I also found out that one of the more simple ones to use is actually included in Alfresco, GreenMail.

I promptly started to write my integration test. Define a private variable for the test first.

private GreenMail _mailServer;

In the “@Before” part (or similar) create and start the SMTP instance.

_mailServer = new GreenMail(ServerSetupTest.SMTP);
_mailServer.start();

In the “@After” part (or similar) stop the SMTP instance.

_mailServer.stop();

You also have to modify the alfresco-global.properties file for your test suite so that Alfresco sends emails to the correct port. As default GreenMail ServerSetupTest.SMTP picks 25 + an offset number (3000) as it’s port, so set the value to:

mail.port=3025

Now, in your test just execute the code that sends the emails. Now comes the nice stuff. GreenMail has a function to get all received messages. Getting the messages and doing the proper assertions is now a walk in the park :)

MimeMessage[] messages = _mailServer.getReceivedMessages();
assertEquals(1, messages.length);

assertThat(message.getSubject(), containsString("Urget message!"));
assertThat(message.getContent().toString(), containsString("Kind regards,"));

No more excuses not to test your email sending capabilities…

This entry was posted in Uncategorized. Bookmark the permalink.

14 Responses to Integration testing emails

  1. Very useful information I will post the post and come back to find out your information. Thanks for the dark post that will definitely come back.

  2. Name says:

    comment text
    link

  3. Leo says:

    Thanks for taking the time to go over this, I’m strongly about it and enjoy learning longer on this issue. amazon.faimbd.com

  4. aqua fashion says:

    Thanks a lot for the blog post.Much thanks again. Cool.

  5. flesh ligt says:

    Major thankies for the post.Thanks Again. Keep writing.

  6. sihns says:

    Looking forward to reading more. Great blog article.Really looking forward to read more. Awesome.

  7. SMM says:

    Thanks for the article.Thanks Again. Fantastic.

  8. C_TS4C_2020 says:

    Im thankful for the blog.Much thanks again. Much obliged.

  9. Tattoo says:

    Some truly fantastic information, Gladiolus I detected this.

  10. Tattoo says:

    Muchos Gracias for your article post.Thanks Again. Fantastic.

  11. Thanks again for the blog post.Much thanks again.

  12. You are my function designs. Many thanks for that post

  13. wow, awesome article post.Really thank you! Much obliged.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>