<?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>Megatome &#187; junit</title>
	<atom:link href="http://www.megatome.com/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.megatome.com</link>
	<description>Just another idiot&#039;s ramblings</description>
	<lastBuildDate>Thu, 12 Jan 2012 00:45:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Known Defects and FindBugs</title>
		<link>http://www.megatome.com/2008/05/30/known-defects-and-findbugs/</link>
		<comments>http://www.megatome.com/2008/05/30/known-defects-and-findbugs/#comments</comments>
		<pubDate>Fri, 30 May 2008 16:41:16 +0000</pubDate>
		<dc:creator>iamthechad</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Making]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[characterization test]]></category>
		<category><![CDATA[findbugs]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[knowndefect]]></category>
		<category><![CDATA[legacy]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.megatome.com/2008/05/30/known-defects-and-findbugs/</guid>
		<description><![CDATA[(Download KnownDefects) I ran into a bit of a quandary the other day. I had to modify a piece of legacy code in our system &#8211; one that has no tests written against it. My first task was to write as many JUnit tests as I could to document the behavior of the component to [...]]]></description>
			<content:encoded><![CDATA[<p>(<a href="http://subversion.megatome.com/projects/KnownDefects/trunk/KnownDefect.zip">Download KnownDefects</a>)  </p>
<p>I ran into a bit of a quandary the other day. I had to modify a piece of legacy code in our system &#8211; one that has no tests written against it. My first task was to write as many <a href="http://www.junit.org/">JUnit</a> tests as I could to document the behavior of the component to help ensure that I didn&#8217;t break existing functionality with my changes.  </p>
<p>After a few tests, however, I began to notice something troubling. The component I was writing tests against had some bugs in it, and in order to make my unit tests pass, I had to code to those bugs. This in itself did not bother me too much, since I had no expectation that the component I was testing was error-free.  </p>
<p>What bothered me is that unit tests are often the best documentation of how a system works, and I would be creating misleading documentation. So, I did what anyone in this situation would do: I emailed <a href="http://langrsoft.com/">someone a lot smarter than me</a>.&nbsp; </p>
<p>The reply email contained several suggestions, all of which I plan to implement. The first suggestion is to carefully name the test methods to make it clear which ones work because of defects. The second suggestion is to create an annotation that can be added to a method to indicate that it works because of a defect, coupled with some way to report on all uses of the annotation.  </p>
<p>After a few hours of coding, I&#8217;m happy to announce that I&#8217;ve created a Java annotation named <code>KnownDefect</code> as well as a detector plugin for <a href="http://findbugs.sourceforge.net/">FindBugs</a> to collect all of the annotation instances.  </p>
<p>In the parlance of &#8220;<a href="http://www.amazon.com/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1212164658&amp;sr=1-1">Working Effectively with Legacy Code</a>&#8220;, I have been creating &#8220;<a href="http://www.artima.com/weblogs/viewpost.jsp?thread=198296">characterization tests</a>&#8220;. These tests look like normal unit tests, but they document the current state of the system, warts and all. Using the <code>KnownDefect</code> annotation helps make the warts stick out better.  </p>
<p>Here&#8217;s an example:  </p>
<p>
<pre name="code" class="brush: java">public void testValidateNullResponse() {
   try {
      ProtocolParser.validateResponse(null);
      fail();
   } catch (InvalidRequestException expected) {
      // Caught expected exception
   }
}</pre>
</p>
<p>This test documents the system as it currently exists, but the behavior is obviously not correct. The <code>ProtocolParser</code> class should be throwing an <code>InvalidResponseException</code>, not an <code>InvalidRequestException</code>. To document this defect, I change the method name and add the <code>KnownDefect</code> annotation to the test method, resulting in the following:
</p>
<p>
<pre name="code" class="brush: java">@KnownDefect("Should throw InvalidResponseException")
public void testValidateNullResponseShowsKnownDefect() {
   try {
      ProtocolParser.validateResponse(null);
      fail();
   } catch (InvalidRequestException expected) {
      // Caught expected exception
   }
}</pre>
</p>
<p>The correct behavior is now documented, and the test still passes.
</p>
<p>Instead of writing a new tool to manage the <code>KnownDefect</code> instances, I ended up writing a new plugin for FindBugs. The FindBugs detector will collect all instances of the annotation and group them under the &#8220;Correctness&#8221; heading so they&#8217;re all in one place. Bug reports can then be created and the defects fixed or not as business needs dictate. (Sadly, the FindBugs plugin for Eclipse does not find the annotation in Groovy code. The standalone version of FindBugs works fine, so I assume there&#8217;s a resource filtering issue with the Eclipse plugin.)</p>
<p>Instructions for using the annotation and the detector are included in the download file. Try it out &#8211; hopefully it will be useful to somebody other than myself.</p>
<p>(<a href="http://subversion.megatome.com/projects/KnownDefects/trunk/KnownDefect.zip">Download KnownDefects</a>) (Subversion: <a href="http://subversion.megatome.com/projects/KnownDefects/trunk">http://subversion.megatome.com/projects/KnownDefects/trunk</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.megatome.com/2008/05/30/known-defects-and-findbugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Approaching the Finish Line</title>
		<link>http://www.megatome.com/2007/08/31/approaching-the-finish-line/</link>
		<comments>http://www.megatome.com/2007/08/31/approaching-the-finish-line/#comments</comments>
		<pubDate>Fri, 31 Aug 2007 20:34:33 +0000</pubDate>
		<dc:creator>iamthechad</dc:creator>
				<category><![CDATA[frame2]]></category>
		<category><![CDATA[general]]></category>
		<category><![CDATA[sourceforge]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[web-application-framework]]></category>

		<guid isPermaLink="false">http://frame2.megatome.com/archives/9</guid>
		<description><![CDATA[Frame2 is almost ready to be released as a new version. I may still make some changes, but it&#8217;s definitely in Release Candidate stage now. There are only a few things that I may add before testing the heck out of it: Upgrade unit tests to JUnit 4. There are a lot of tests, especially [...]]]></description>
			<content:encoded><![CDATA[<p>Frame2 is almost ready to be released as a new version. I may still make some changes, but it&#8217;s definitely in Release Candidate stage now. There are only a few things that I may add before testing the heck out of it:</p>
<ul>
<li>Upgrade unit tests to <a href="http://www.junit.org/index.htm">JUnit 4</a>. There are a lot of tests, especially the ones that mock a servlet container, that do a lot of repetitive work in <code>setUp()</code> . JUnit 4 has finally brought the ability to annotate methods to be run before and after all tests in the class with <code>@BeforeClass</code> and <code>@AfterClass</code>, respectively. Individual test setup activities can still be performed by annotating a method with <code>@Before</code>.</li>
<li>Fix outstanding bugs. There&#8217;s only one right now, and it&#8217;s SOAP specific, so I may or may not fix it. It may take me longer to figure out how to reproduce the problem with unit tests than it&#8217;s worth.</li>
<li>Add outstanding feature requests. Again, there&#8217;s only one, but it&#8217;s (in my opinion) a biggie &#8211; the ability to add parameters to the response when handling an event.</li>
<li>Doc updates. The web services doc needs to be written, and I&#8217;m sure all of the other doc needs a good examination.</li>
</ul>
<p>I&#8217;ll be testing the latest build with a web application that I&#8217;ve been working on for years that I&#8217;ve come to think of as an unofficial reference implementation of Frame2. I haven&#8217;t decided yet if the web app should become part of the project on Sourceforge or not. We&#8217;ll see.</p>
<p>Once the testing is done and I&#8217;ve released a new version of Frame2, I plan on working on the <a href="http://sourceforge.net/project/showfiles.php?group_id=102634&amp;package_id=112600">Eclipse plugin</a>. The poor plugin has pretty much been ignored for several years now, and it needs some attention.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.megatome.com/2007/08/31/approaching-the-finish-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Consistent API is a Very Good Thing</title>
		<link>http://www.megatome.com/2007/08/30/a-consistent-api-is-a-very-good-thing/</link>
		<comments>http://www.megatome.com/2007/08/30/a-consistent-api-is-a-very-good-thing/#comments</comments>
		<pubDate>Thu, 30 Aug 2007 20:24:56 +0000</pubDate>
		<dc:creator>iamthechad</dc:creator>
				<category><![CDATA[general]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[frame2]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[oss]]></category>

		<guid isPermaLink="false">http://frame2.megatome.com/archives/8</guid>
		<description><![CDATA[I&#8217;ve been mucking around in the codebase, cleaning up pieces of the API that annoy me. Some bright person thought that it was a good idea to have a method that returns an Iterator to access a collection. I&#8217;ve been cleaning up all of these methods, changing them so that they return an actual collection [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been mucking around in the codebase, cleaning up pieces of the API that annoy me. Some bright person thought that it was a good idea to have a method that returns an <code>Iterator</code> to access a collection. I&#8217;ve been cleaning up all of these methods, changing them so that they return an actual collection &#8211; occasionally made unmodifiable through the <code>Collections</code> class.</p>
<p>Naturally, this has led to unit test failures. Almost all of the failures have been easy to fix, but once in a while one pops up that makes me scratch my head. Here&#8217;s an example:</p>
<pre name="code" class="brush: java">
public void testGetIfEmpty() {
    assertTrue(this.errors.isEmpty());
    assertNull(this.errors.get(FOO));
    assertEquals(0, this.errors.get().length);
    assertEquals(0, this.errors.get(FOO).length);
}</pre>
<p>In this instance, <code>errors</code> is a class that aggregates individual <code>Error</code> objects. The <code>get(String)</code> method returns an array of <code>Error</code> objects that have the specified key, while <code>get()</code> retrieves all <code>Error</code>s. Pretty simple, right? After my changes, the test fails on the <code>assertNull()</code> statement, which isn&#8217;t surprising.</p>
<p>The failing statement read <code>assertNull(this.errors.iterator(FOO))</code> before I removed the <code>iterator()</code> method, which tells us a bit about how the API was originally coded. The <code>iterator(String)</code> returned <code>null</code> if there were no matching entries. However, as the unit test clearly shows, <code>get(String)</code> returns an empty array when it doesn&#8217;t find any matching <code>Error</code>s. Quite the difference, and potentially confusing &#8211; the developer has to remember which method may return <code>null</code>.</p>
<p>The API has now been changed to return an empty collection instead of <code>null</code> when no matching errors are found. Now, a user of the API simply has to call the method and loop over the result (zero times if it&#8217;s empty) without having to make <code>null</code> checks.</p>
<p>Whoever wrote this unit test obviously knew that one method returned <code>null</code> while the other returned an empty array for the same input, since the test passed. I only wish that person would have looked at those lines and noticed the inconsistency, instead of leaving it for me to find.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.megatome.com/2007/08/30/a-consistent-api-is-a-very-good-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

