<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jabbering Giraffe &#187; junit</title>
	<atom:link href="http://happygiraffe.net/blog/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://happygiraffe.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 20:49:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Dynamic JUnit</title>
		<link>http://happygiraffe.net/blog/2007/04/27/dynamic-junit/</link>
		<comments>http://happygiraffe.net/blog/2007/04/27/dynamic-junit/#comments</comments>
		<pubDate>Fri, 27 Apr 2007 06:24:09 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2007/04/27/dynamic-junit/</guid>
		<description><![CDATA[Recently, I wanted to do something slightly unusual with JUnit. I&#8217;m working on a cocoon project, so there are squillions of little XML files floating around. These need to be all well-formed. So I want a test that parses each &#8230; <a href="http://happygiraffe.net/blog/2007/04/27/dynamic-junit/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently, I wanted to do something slightly unusual with JUnit.  I&#8217;m working on a cocoon project, so there are squillions of little <span class="caps">XML</span> files floating around.  These need to be all well-formed.  So I want a test that parses each one.  Then, <a href="http://cruisecontrol.sourceforge.net/">CruiseControl</a> can let us know when they get broken.</p>
<p>First, I gave the task to a colleague.  He came up with something that checked each file, and returned a list of the bad ones.  It then asserted that the <code>nonWellFormed</code> list had a zero length.  Which is great and all, but didn&#8217;t tell you which file was broken, nor why.</p>
<p>What I really wanted to do was have a single test per file, so it could display the errors correctly.  This seemed like an easy thing to do&#8230;  Until I tried it.  This is what I eventually arrived at:</p>
<pre>
  class WellFormedTest extends TestCase {
    public static Test suite() {
        final TestSuite suite = new TestSuite(WellFormedTest.class.getCanonicalName());
        // Stupid bloody Java regexes have to match from the beginning of the
        // string.
        Pattern p = Pattern.compile(".*\\.(xml|xslt|xconf|xmap)$");
        FindFiles ff = new FindFiles(p) {
            protected void processFile(final File file) {
                suite.addTest(new WellFormedTest(file));
            }
        };
        ff.search("web");
        return suite;
    }

    private File file;

    public WellFormedTest(File file) {
      super("Well-Formed? " + file.toString());
      this.file = file;
    }

    protected void runTest() throws Throwable {
      XmlValidator validator = new XmlValidator();
      String result = validator.isWellFormed(file);
      assertEquals(file.toString(), null, result);
    }
  }
</pre>
<ul>
<li>FindFiles is a utility class to walk a directory tree.  Tell me again why Java doesn&#8217;t have something this basic in it&#8217;s vast class libraries?</li>
<li>You have to call <code>super("blah")</code> in your constructor to name each test sensibly.</li>
<li>But if you do this, you have to override <code>runTest()</code> in order for things to actually work.  The usual mechanism for determining which tests to run doesn&#8217;t work if you supply a custom name.  This took forever to work out and required delving into the JUnit source.  Halleluljah for Open Source.
<ul>
<li>As part of prodding around in the debugger, I noticed that JUnit creates a new TestCase object for each test in the class.  So it&#8217;s OK to just do one thing in <code>runTest()</code>, as that&#8217;s all that&#8217;s going to happen anyway.</li>
</ul>
</li>
<li>XmlValidator is another custom helper class.  It just parses the file and returns a String containing the error (or null).</li>
<li>Yes, this is JUnit 3.8.  I know I need to migrate to JUnit 4.  That&#8217;s a battle for another day, dependent on upgrading ant first.</li>
</ul>
<p>Originally, I tried to get the test done inside a nested anonymous subclass of TestCase, but there&#8217;s no constructor there, so that doesn&#8217;t work too well.  Plus it bumps the ugliness of the source another level.</p>
<p>The end result works quite well and provides a useful example for doing dynamic tests with JUnit.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2007/04/27/dynamic-junit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Verbosity</title>
		<link>http://happygiraffe.net/blog/2006/07/24/java-verbosity/</link>
		<comments>http://happygiraffe.net/blog/2006/07/24/java-verbosity/#comments</comments>
		<pubDate>Mon, 24 Jul 2006 20:55:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/07/24/java-verbosity/</guid>
		<description><![CDATA[I&#8217;ve been doing quite a bit of Java over the last fortnight. It&#8217;s much easier with eclipse of course, but it&#8217;s still pretty verbose. But I dunno, maybe it&#8217;s just me, a whingy old Perl coder. But then again, look &#8230; <a href="http://happygiraffe.net/blog/2006/07/24/java-verbosity/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing quite a bit of Java over the last fortnight.  It&#8217;s much easier with <a href="http://www.eclipse.org/">eclipse</a> of course, but it&#8217;s still pretty verbose.  But I dunno, maybe it&#8217;s just me, a whingy old Perl coder.  But then again, look what I&#8217;ve just found in the <a href="http://www.oreilly.com/catalog/javacook2/">Java Cookbook</a>:</p>
<pre>
  public class Ls {
    public static void main(String argh_my_aching_fingers[]) {
      // ...
    }
  }
</pre>
<p>Glad to know I&#8217;m not the only one.  <code> <img src='http://happygiraffe.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </code></p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/07/24/java-verbosity/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JUnit 4, the downfall</title>
		<link>http://happygiraffe.net/blog/2006/07/17/junit-4-the-downfall/</link>
		<comments>http://happygiraffe.net/blog/2006/07/17/junit-4-the-downfall/#comments</comments>
		<pubDate>Mon, 17 Jul 2006 22:02:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/07/17/junit-4-the-downfall/</guid>
		<description><![CDATA[Sadly, after my excitement about Junit 4, I&#8217;ve found the downfall: it doesn&#8217;t work with ant yet. However, there does appear to be a workaround. Add this to each class: public static junit.framework.Test suite() { return new junit.framework.JUnit4TestAdapter(SimpleTest.class); } However, &#8230; <a href="http://happygiraffe.net/blog/2006/07/17/junit-4-the-downfall/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sadly, after my <a href="http://happygiraffe.net/blog/archives/2006/07/14/junit-4">excitement</a> about Junit 4, I&#8217;ve found the downfall: it doesn&#8217;t work with ant yet.</p>
<p>However, there does <em>appear</em> to be a workaround.  Add this to each class:</p>
<pre>
  public static junit.framework.Test suite() {
    return new junit.framework.JUnit4TestAdapter(SimpleTest.class);
  }
</pre>
<p>However, that&#8217;s now causing my tests to fail with NullPointerExceptions inside HttpUnit.  Yet they work fine in Eclipse.  Wonder what I&#8217;m doing wrong?</p>
<p><strong>Update</strong>: It&#8217;s entirely my own fault&#8212;my ant task was forgetting to copy over non-java artifacts into the classpath.  So adding <code>suite()</code> makes things work just fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/07/17/junit-4-the-downfall/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Junit 4</title>
		<link>http://happygiraffe.net/blog/2006/07/14/junit-4/</link>
		<comments>http://happygiraffe.net/blog/2006/07/14/junit-4/#comments</comments>
		<pubDate>Fri, 14 Jul 2006 00:27:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/07/14/junit-4/</guid>
		<description><![CDATA[I&#8217;ve got a little project at work that&#8217;s completely new (hurrah!), and using Java 5. So, I thought I&#8217;d give the latest Junit a try. Junit 4 is quite a departure from the older versions. It relies on features only &#8230; <a href="http://happygiraffe.net/blog/2006/07/14/junit-4/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got a little project at work that&#8217;s completely new (hurrah!), and using Java 5.  So, I thought I&#8217;d give the latest <a href="http://www.junit.org/">Junit</a> a try.  Junit 4 is quite a departure from the older versions.  It relies on features only present in Java 5, like annotations.  But this does free up the tests from having to inherit from TestCase.</p>
<p>This is the simplest test case.</p>
<pre>
  import org.junit.Assert;
  import org.junit.Before;
  import org.junit.Test;

  public class MyTest {
    @Before
    public void setUp() throws Exception {
      // ...
    }

    @Test
    public void alwaysTrue() {
      Assert.assertTrue(true);
    }
  }
</pre>
<p>You can make this slightly nicer by using static imports for the assertions.  I didn&#8217;t because I wanted eclipse to complete them and I couldn&#8217;t auto-discover them as easily.  I&#8217;ll probably switch shortly.</p>
<p>The annotations are also used for stating which exceptions you wish to catch.</p>
<p>All in all, I&#8217;m quite impressed.  It&#8217;s about as simple as it gets in Java.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/07/14/junit-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ant &amp; JUnit</title>
		<link>http://happygiraffe.net/blog/2005/09/03/ant-junit/</link>
		<comments>http://happygiraffe.net/blog/2005/09/03/ant-junit/#comments</comments>
		<pubDate>Sat, 03 Sep 2005 08:05:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[junit]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2005/09/03/ant-junit/</guid>
		<description><![CDATA[I&#8217;ve just been tripped up by ant &#38; JUnit. I&#8217;m appalled. All the proposed solutions are both hackish and ugly. I don&#8217;t actually care about the intricacies of ClassLoaders. I just want a simple way to tell ant &#8220;junit is &#8230; <a href="http://happygiraffe.net/blog/2005/09/03/ant-junit/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just been tripped up by <a href="http://ant.apache.org/faq.html#delegating-classloader">ant &amp; JUnit</a>.  I&#8217;m appalled.  All the proposed solutions are both hackish and ugly.  I don&#8217;t actually <em>care</em> about the intricacies of ClassLoaders.  I just want a simple way to tell ant &#8220;junit is over here&#8221; so that my build and tests will work on whatever system I check out on.  I don&#8217;t want to have to install stuff into the ant directory on each system.  I already have <code>junit.jar</code> in my codebase, and it should be able to use it.</p>
<p>I suppose I could get around it by writing a <code>build.sh</code> which sets the <span class="caps">CLASSPATH</span> correctly, but I thought ant was supposed to avoid all that?</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2005/09/03/ant-junit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

