Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Email 101 Unit Tests (See related posts)

// Two unit tests for my Email 101 code. The first expects connection information to
// be in the System properties. That can be setup as part of running the unit tests
// from either Ant or Eclipse. Note: The first test only confirms that the send occurs
// without error, not that the email was actually received or received at the correct
// address.
//
// The second test trashes the SMTP server name and then confirms that it results in
// an exception being thrown from the mail sending code.

   1  
   2  package com.johnmunsch.util;
   3  
   4  import javax.mail.MessagingException;
   5  import javax.mail.internet.AddressException;
   6  
   7  import junit.framework.TestCase;
   8  
   9  public class MailTest extends TestCase {
  10      String from = null;
  11      String to = null;
  12      String title = null;
  13      String textBody = null;
  14      String htmlBody = null;
  15      String smtpServer = null;
  16      
  17      /* (non-Javadoc)
  18       * @see junit.framework.TestCase#setUp()
  19       */
  20      protected void setUp() throws Exception {
  21          from = System.getProperty("from");
  22          to = System.getProperty("to");
  23          title = System.getProperty("title");
  24          textBody = System.getProperty("textBody");
  25          htmlBody = System.getProperty("htmlBody");
  26          smtpServer = System.getProperty("smtpServer");
  27      }
  28  
  29      /**
  30       * This is a terrible test. It could fail just as easily because it is 
  31       * incorrectly supplied with parameters as due to any failure in the code.
  32       * @throws MessagingException 
  33       * @throws AddressException 
  34       */
  35      public void testSendMail() throws AddressException, MessagingException {
  36          Mail.sendMail(from, to, title, textBody, htmlBody, smtpServer);
  37      }
  38      
  39      /**
  40       * Same test as above except that we screw up the smtpServer address so it
  41       * is absolute garbage. In that case we should see a failure, which we are
  42       * looking for, and we throw an error if we _don't_ see some kind of
  43       * exception.
  44       */
  45      public void testSendMailWithBadServer() {
  46          smtpServer = "garbageingarbageout";
  47          
  48          try {
  49              Mail.sendMail(from, to, title, textBody, htmlBody, smtpServer);
  50              
  51              fail();
  52          } catch (AddressException e) {
  53              // Exception good, no exception bad.
  54          } catch (MessagingException e) {
  55              // Exception good, no exception bad.
  56          }
  57      }
  58  }

You need to create an account or log in to post comments to this site.


Click here to browse all 5338 code snippets

Related Posts