<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: unit tests code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 16:08:37 GMT</pubDate>
    <description>DZone Snippets: unit tests code</description>
    <item>
      <title>Velocity 101 Unit Tests</title>
      <link>http://snippets.dzone.com/posts/show/1592</link>
      <description>// Unit tests for the Velocity 101 code. Note that the second test is&lt;br /&gt;// actually a test to confirm that the function returns a failure&lt;br /&gt;// correctly when the Velocity template file is not found.&lt;br /&gt;//&lt;br /&gt;// Be sure to get the test.vm file that goes with these unit tests.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.johnmunsch.util;&lt;br /&gt;&lt;br /&gt;import org.apache.velocity.VelocityContext;&lt;br /&gt;&lt;br /&gt;import junit.framework.TestCase;&lt;br /&gt;&lt;br /&gt;public class BoilerplateTest extends TestCase {&lt;br /&gt;    private VelocityContext context = null;&lt;br /&gt;    private String testTemplate = null;&lt;br /&gt;&lt;br /&gt;    protected void setUp() throws Exception {&lt;br /&gt;        context = new VelocityContext();&lt;br /&gt;        testTemplate = System.getProperty("testTemplate");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * A very basic test to see if we can get a word inserted into a template&lt;br /&gt;     * with the Boilerplate class.&lt;br /&gt;     * @throws Exception &lt;br /&gt;     */&lt;br /&gt;    public void testApply() throws Exception {&lt;br /&gt;        context.put("foo", "Velocity");&lt;br /&gt;        &lt;br /&gt;        String output = Boilerplate.apply(context, testTemplate);&lt;br /&gt;        &lt;br /&gt;        assertEquals(output, "&lt;html&gt;&lt;body&gt;Hello Velocity World!&lt;/body&gt;&lt;html&gt;");&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Same sort of test as above except this time we specify a bogus name for&lt;br /&gt;     * the template file and we expect to get an exception. The failure occurs&lt;br /&gt;     * only if we don't have an exception thrown.&lt;br /&gt;     */&lt;br /&gt;    public void testApply2() {&lt;br /&gt;        context.put("foo", "Velocity");&lt;br /&gt;&lt;br /&gt;        try {&lt;br /&gt;            Boilerplate.apply(context, "heyheypaula.vm");&lt;br /&gt;            &lt;br /&gt;            fail();&lt;br /&gt;        } catch (Exception e) {&lt;br /&gt;            // We expect an exception. Failure is when we don't see one.&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;// This is a required file for the unit tests in "Velocity 101 Unit Tests". &lt;br /&gt;// Put it into a file and make sure that the filename is passed into the unit&lt;br /&gt;// test above as a System property.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;html&gt;&lt;body&gt;Hello $foo World!&lt;/body&gt;&lt;html&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 04:00:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1592</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
    <item>
      <title>Email 101 Unit Tests</title>
      <link>http://snippets.dzone.com/posts/show/1591</link>
      <description>// Two unit tests for my Email 101 code. The first expects connection information to&lt;br /&gt;// be in the System properties. That can be setup as part of running the unit tests&lt;br /&gt;// from either Ant or Eclipse. Note: The first test only confirms that the send occurs&lt;br /&gt;// without error, not that the email was actually received or received at the correct&lt;br /&gt;// address.&lt;br /&gt;//&lt;br /&gt;// The second test trashes the SMTP server name and then confirms that it results in&lt;br /&gt;// an exception being thrown from the mail sending code.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.johnmunsch.util;&lt;br /&gt;&lt;br /&gt;import javax.mail.MessagingException;&lt;br /&gt;import javax.mail.internet.AddressException;&lt;br /&gt;&lt;br /&gt;import junit.framework.TestCase;&lt;br /&gt;&lt;br /&gt;public class MailTest extends TestCase {&lt;br /&gt;    String from = null;&lt;br /&gt;    String to = null;&lt;br /&gt;    String title = null;&lt;br /&gt;    String textBody = null;&lt;br /&gt;    String htmlBody = null;&lt;br /&gt;    String smtpServer = null;&lt;br /&gt;    &lt;br /&gt;    /* (non-Javadoc)&lt;br /&gt;     * @see junit.framework.TestCase#setUp()&lt;br /&gt;     */&lt;br /&gt;    protected void setUp() throws Exception {&lt;br /&gt;        from = System.getProperty("from");&lt;br /&gt;        to = System.getProperty("to");&lt;br /&gt;        title = System.getProperty("title");&lt;br /&gt;        textBody = System.getProperty("textBody");&lt;br /&gt;        htmlBody = System.getProperty("htmlBody");&lt;br /&gt;        smtpServer = System.getProperty("smtpServer");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * This is a terrible test. It could fail just as easily because it is &lt;br /&gt;     * incorrectly supplied with parameters as due to any failure in the code.&lt;br /&gt;     * @throws MessagingException &lt;br /&gt;     * @throws AddressException &lt;br /&gt;     */&lt;br /&gt;    public void testSendMail() throws AddressException, MessagingException {&lt;br /&gt;        Mail.sendMail(from, to, title, textBody, htmlBody, smtpServer);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Same test as above except that we screw up the smtpServer address so it&lt;br /&gt;     * is absolute garbage. In that case we should see a failure, which we are&lt;br /&gt;     * looking for, and we throw an error if we _don't_ see some kind of&lt;br /&gt;     * exception.&lt;br /&gt;     */&lt;br /&gt;    public void testSendMailWithBadServer() {&lt;br /&gt;        smtpServer = "garbageingarbageout";&lt;br /&gt;        &lt;br /&gt;        try {&lt;br /&gt;            Mail.sendMail(from, to, title, textBody, htmlBody, smtpServer);&lt;br /&gt;            &lt;br /&gt;            fail();&lt;br /&gt;        } catch (AddressException e) {&lt;br /&gt;            // Exception good, no exception bad.&lt;br /&gt;        } catch (MessagingException e) {&lt;br /&gt;            // Exception good, no exception bad.&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Feb 2006 03:53:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1591</guid>
      <author>JohnMunsch (John Munsch)</author>
    </item>
  </channel>
</rss>
