In previous projects I mostly skipped writing tests for sending mail. Mostly due to the fact that there is a dependency on a working SMTP server. This time I decided to invest some time in finding a solution for this problem.

It didn't have to look very far to find Wiser. Wiser is a very simple fake SMTP server designed for unit and system testing applications. You basically start it up, send mail to it and read the queue of messages it received.

Using it in unit test is indeed simple. First setup the server, and reconfigure mail senders in the Spring context:

JAVA:
  1. @SuppressWarnings("unchecked")
  2. @Override
  3. protected void onSetUp() throws Exception {
  4.   super.onSetUp();
  5.   wiser = new Wiser()
  6.   wiser.setPort(2525);
  7.   wiser.start();
  8.          
  9.   Map<String, JavaMailSenderImpl> ofType =   
  10.     getApplicationContext().getBeansOfType(org.springframework.mail.javamail.JavaMailSenderImpl.class);
  11.  
  12.   // reconfigure mailsenders in the spring context
  13.   for (Entry<String, JavaMailSenderImpl> bean : ofType.entrySet()) {
  14.     log.info(String.format("configuring mailsender %s to use local Wiser SMTP", bean.getKey()));
  15.     JavaMailSenderImpl mailSender = bean.getValue();
  16.     mailSender.setPort(2525);
  17.     mailSender.setHost("localhost");
  18.   }
  19. }

Now you are ready to run a test:

JAVA:
  1. public void testSendMailStrings() {
  2.   mailService.sendMail("test-sender@test.nl", "test-reciever@test.nl", "testing the mailservice", "testing the mail service, body");
  3.  
  4.   assertEquals(1, wiser.getMessages().size());
  5.   WiserMessage m = wiser.getMessages().get(0);
  6.   assertEquals("test-sender@test.nl", m.getEnvelopeSender());
  7.   assertEquals("test-reciever@test.nl", m.getEnvelopeReceiver());
  8.   try {
  9.     MimeMessage message = m.getMimeMessage();
  10.     assertEquals("testing the mailservice", message.getSubject());   
  11.     assertEquals("testing the mail service, body", (String)message.getContent());
  12.   } catch (MessagingException me) {
  13.     fail(String.format("MessagingException [%s] should not occur", me.getMessage()));
  14.   } catch (IOException ioe) {
  15.     fail(String.format("IOException [%s] should not occur", ioe.getMessage()));
  16.   }
  17. }

And after this, make sure to stop the server so it can be started again for consequent tests:

JAVA:
  1. @Override
  2. protected void onTearDown() throws Exception {
  3.   wiser.stop();
  4. }

Works like a dream!


2 Responses to “Unittesting e-mail sending using Spring”

  1. 1 erikvanoosten

    Indeed, SubethaSMTP is cool. I used it in my embedded e-mail server for Mule.

  2. 2 peter

    I knew I had seen it somewhere before!

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


sea_lion Scribbled Sun Logo Moscone Center - JavaOne Community One Keynote javaone2008 keynote Okke en Rudie Charles Nutter & Guillaume Laforge nearby hotel golden_gate_warning_sign alcatraz Stretched Limo Golden Gate Java + You on a cab pub javaone 2008 goodybag Greenland Rudie Cable Car line Joshua Bloch at JavaOne2008 Tim Bray introducing the (J)Ruby panel
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.