<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: junit code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:41:21 GMT</pubDate>
    <description>DZone Snippets: junit 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>
    <item>
      <title>JUnit: Convenient use of TestSuite based on parameterized TestCases</title>
      <link>http://snippets.dzone.com/posts/show/752</link>
      <description>Here's a frequently (at least for me) occurring problem: let's say&lt;br /&gt;you have a TestCase where each testXXX() method really does mostly &lt;br /&gt;the same thing, parameterized by some instance variables. For instance,&lt;br /&gt;I have a file consisting of good data for each parameter, such as:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;goodAAA=foo&lt;br /&gt;goodBBB=bar&lt;br /&gt;goodCCC=baz&lt;br /&gt;...&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You don't want to create tens or hundreds of methods (testAAA(), &lt;br /&gt;testBBB(), etc.) by copying and pasting. So you create a TestCase that &lt;br /&gt;has:&lt;br /&gt;&lt;br /&gt;- An field called, say, key&lt;br /&gt;&lt;br /&gt;- A single testKey() method that, based on the &lt;br /&gt;key, gets the good data, gets the test data and compares them.&lt;br /&gt;&lt;br /&gt;- static suite() method that, in a loop, creates instances of your &lt;br /&gt;TestCase and sets their keys appropriately.&lt;br /&gt;&lt;br /&gt;This is all well and good. But when you run it, a failure will always &lt;br /&gt;show up in testKey() method in JUnit's UI. (Sure, you can add a message&lt;br /&gt;that shows the key for which it failed, but it does not give&lt;br /&gt;you an at-a-glance feedback.)&lt;br /&gt;&lt;br /&gt;So for convenience, you override getName() to return key. Now&lt;br /&gt;it's very easy to tell, by merely glancing at that JUnit bar, what &lt;br /&gt;failed. &lt;br /&gt;&lt;br /&gt;But if you run it within IDE, say, Eclipse, you want to click on the &lt;br /&gt;failed test so that Failure Trace appears, where you can click on any &lt;br /&gt;Stack Trace Element and get to the exact line of failure in your code. &lt;br /&gt;But because the failed test's name is based on the key field, it does &lt;br /&gt;not correspond to a real testXXX() method in your TestCase. So your&lt;br /&gt;IDE can't take you to the code that failed.&lt;br /&gt;&lt;br /&gt;Here's how to override getName() to get the best of both worlds:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public String getName() {&lt;br /&gt;    StackTraceElement[] st = new Throwable().getStackTrace();&lt;br /&gt;    if (st.length &lt; 4 || !st[3].getMethodName().equals("endTest")) {&lt;br /&gt;        return key; 	  // Meaningful name to appear in the bar&lt;br /&gt;    } else {&lt;br /&gt;        return "testKey"; // Real method name to appear in Failure Trace&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 20 Sep 2005 00:28:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/752</guid>
      <author>debedb (http://www.hrum.org)</author>
    </item>
  </channel>
</rss>
