<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Effective Java Testing</title>
	<atom:link href="http://effectivejavatesting.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://effectivejavatesting.wordpress.com</link>
	<description>A series of pragmatic thoughts on agile testing, from one programmer to another.</description>
	<lastBuildDate>Wed, 17 Dec 2008 17:00:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='effectivejavatesting.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Effective Java Testing</title>
		<link>http://effectivejavatesting.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://effectivejavatesting.wordpress.com/osd.xml" title="Effective Java Testing" />
	<atom:link rel='hub' href='http://effectivejavatesting.wordpress.com/?pushpress=hub'/>
		<item>
		<title>this Wish</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/17/this-wish/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/17/this-wish/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 17:00:26 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[builder pattern]]></category>
		<category><![CDATA[color commentary]]></category>
		<category><![CDATA[crazy ideas]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java generics]]></category>
		<category><![CDATA[jcp]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[recursive generics]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=102</guid>
		<description><![CDATA[I want this&#8230; Why can&#8217;t Java have a return type of &#8216;this&#8217;, which guarantees to return the method&#8217;s object. And after a bit of google searching &#8212; as expected &#8212; I find I&#8217;m not the first to put this idea forward. Now that Java permits covariant returns types, we can get nearly the same effect [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=102&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I want this&#8230;<br />
<pre class="brush: java;">
class FooBuilder {
  ...
  public this withBar(Bar bar) {
    this.bar = bar;
  }
}
</pre><br />
Why can&#8217;t Java have a return type of &#8216;this&#8217;, which guarantees to return the method&#8217;s object. And after a bit of google searching &#8212; as expected &#8212; I find I&#8217;m not <a href="http://groups.google.com/group/javaposse/browse_thread/thread/3587eb6460a8a5c5?pli=1">the first</a> to put this idea forward.</p>
<p>Now that Java permits covariant returns types, we can get nearly the same effect by having <em>Bar extends Foo</em> override any builder-type method that returns a Foo to return a Bar. But what a hassle!</p>
<p>We can also get the same effect using recursive generics, via<br />
<pre class="brush: java;">
class FooBuilder&lt;T extends FooBuilder&lt;T&gt;&gt; {
  T someBuilderMethod(...) { ... }
}

class BarBuilder extends FooBuilder&lt;BarBuilder&gt; {
  ...
}
</pre></p>
<p>My own experience is that recursive generics used for this purpose tend to be clunky. Further, if something else extends Bar, someBuilderMethod can only return a Bar, so we can&#8217;t do something like<br />
<pre class="brush: java;">
class BazBuilder extends BarBuilder {
  BazBuilder someOtherBuilderMethod(...) { ... }
}

class SomeTestThatNeedsABaz {
  public void testSomething() {
    Baz baz = new BazBuilder().someBuilderMethod(...).someOtherBuilderMethod(...).build(); //WRONG
  }
}
</pre></p>
<p>All these issues would be solved very neatly with</p>
<p><pre class="brush: java;">
class FooBuilder&lt;T extends FooBuilder&lt;T&gt;&gt; {
  this someBuilderMethod(...) { ... }
}
</pre><br />
Code that instantiates a BazBuilder knows it still has a BazBuilder.</p>
<p>Oh, and the syntax inside a this method is the same as a void method, permitting naked returns or dropping off the bottom with no return.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/102/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=102&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/17/this-wish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Use Your Eyes as Pattern-Matchers</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/16/use-your-eyes-as-pattern-matchers/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/16/use-your-eyes-as-pattern-matchers/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 17:00:20 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[Musings]]></category>
		<category><![CDATA[color commentary]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[duplication]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=87</guid>
		<description><![CDATA[Every now and then &#8212; especially before you check code in &#8212; squint your eyes and take a look at your test methods. With the individual words fuzzed out, can you see patterns in the shape of your tests? Strong visual patterns can be an indicator that you may need to remove some duplication from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=87&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Every now and then &#8212; especially before you check code in &#8212; squint your eyes and take a look at your test methods. With the individual words fuzzed out, can you see patterns in the shape of your tests?</p>
<p>Strong visual patterns can be an indicator that you may need to remove some duplication from your tests. I say <em>&#8220;may&#8221;</em>, because some amount of duplication in the interest of clarity is acceptable in test code.</p>
<p>Assume you&#8217;re testing a method call, and that your test follows the basic, simple flow</p>
<ul>
<li>set-up</li>
<li>call the method under test</li>
<li>assertions</li>
</ul>
<p>If the duplication is in the set-up, can you use a builder or other means to remove some of that duplication? If it&#8217;s in the assertions, are you sure that you aren&#8217;t testing the same thing in multiple test cases? Even in cases where you need to test different edge cases (&#8220;Does it work with a one-element list? With a zero-element list? With a two-element list?&#8221;) you may not need to repeat the ancillary assertions in each test. If another test is already testing that part, don&#8217;t repeat yourself. At least, don&#8217;t repeat yourself without a good reason.</p>
<p>Squint.</p>
<p>And if you see patterns, take a moment to ponder.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=87&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/16/use-your-eyes-as-pattern-matchers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Consider a Builder for Test Data</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/15/consider-a-builder-for-test-data/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/15/consider-a-builder-for-test-data/#comments</comments>
		<pubDate>Mon, 15 Dec 2008 17:00:03 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[basic]]></category>
		<category><![CDATA[builder pattern]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test data]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=54</guid>
		<description><![CDATA[A specific instance of writing tests to be read is to use a builder to create the test data. In that previous post, we produced the following test code. As well as duplication in the assertions, the test data set-up is similar. We could get rid of this duplication with a helper method But once [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=54&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A specific instance of <a href="http://effectivejavatesting.wordpress.com/2008/12/09/write-tests-to-be-read/">writing tests to be read</a> is to use a <a href="http://en.wikipedia.org/wiki/Builder_pattern">builder</a> to create the test data.</p>
<p>In that previous post, we produced the following test code.</p>
<p><pre class="brush: java;">
   public void testDisplayLines() {
     Account acct = new Account();
     acct.setId(&quot;ID17&quot;);
     acct.setBalance(17);
     acct.setNewCharges(5);

     widget.addDisplayLines(acct);

     DisplayLine line = widget.getDisplayLine(&quot;ID17&quot;);
     assertEquals(&quot;17.0&quot;, line.getPreviousBalance());
     assertEquals(&quot;22.0&quot;, line.getCurrentBalance());
     assertEquals(&quot;Overdue 90 days&quot;, line.getErrorMessage());
   }

   public void testDisplayLinesWithOverdueAccount() {
     Account acct = new Account();
     acct.setOverdueMonths(3);
     acct.setId(&quot;ID17&quot;);
     acct.setBalance(17);
     acct.setNewCharges(5);

     widget.addDisplayLines(acct);

     DisplayLine line = widget.getDisplayLine(&quot;ID17&quot;);
     assertEquals(&quot;17.0&quot;, line.getPreviousBalance());
     assertEquals(&quot;22.0&quot;, line.getCurrentBalance());
     assertEquals(null, line.getErrorMessage());
   }
</pre></p>
<p>As well as duplication in the assertions, the test data set-up is similar. We could get rid of this duplication with a helper method</p>
<p><pre class="brush: java;">
   public void testDisplayLines() {
     Account acct = createAccount(&quot;ID17&quot;, 17, 5);

     widget.addDisplayLines(acct);
     ...

   private void Account createAccount(String id, int balance, int newCharges) {
     Account acct = new Account();
     acct.setId(id);
     acct.setBalance(balance);
     acct.setNewCharges(newCharges);
     return acct;
   }
</pre></p>
<p>But once we have 8-10 tests with different set-up needs, we end up with telescoping helper methods, and the reader cannot tell solely by inspection of the call to the helper method what each parameter means.</p>
<p>While the <a href="http://en.wikipedia.org/wiki/Builder_pattern">builder pattern</a> is usually used to create immutable data, we can use it to chain data creation.</p>
<p><pre class="brush: java;">
   public void testDisplayLines() {
     Account acct = buildAccount()
         .setId(&quot;ID17&quot;).setBalance(17).setNewCharges(5).build();

     widget.addDisplayLines(acct);
     ...
   }

   public void testDisplayLinesWithOverdueAccount() {
     Account acct = buildAccount()
         .setOverdueMonths(3).setId(&quot;ID17&quot;).setBalance(17).setNewCharges(5).build();

     widget.addDisplayLines(acct);
     ...
   }

   private AccountBuilder buildAccount() {
     return new AccountBuilder();
   }

   private static class AccountBuilder {
     private Account acct = new Account();

     AccountBuilder setOverdueMonths(int overdueMonths) {
       acct.setOverdueMonths(overdueMonths);
       return this;
     }

     Account build() {
       Account result = acct;
       acct = null;  //builder is single-use
       return result;
     }

     AccountBuilder setId(String id) {
       acct.setId(id);
       return this;
     }

     AccountBuilder setBalance(int balance) {
       acct.setBalance(balance);
       return this;
     }

     AccountBuilder setNewCharges(int charges) {
       acct.setCharges(charges);
       return this;
     }
   }
</pre></p>
<p>Note that the data set-up in the test methods is as readable as setting the values &#8220;by hand&#8221;. But the declaration of the build class itself is fairly verbose. Some care should be taken to ensure that a builder is only used to instantiate a single object, as untangling the values persisted across multiple re-uses is confusing to read. There&#8217;s also a danger of aliasing if the same object is re-used.</p>
<p>For illustrative purposes, this example is only creating test data of a single type in two test methods. In the real world, this is not enough to justify its construction.</p>
<p>It&#8217;s often the case that we want to use multiple builders for different kinds of data.</p>
<p><pre class="brush: java;">
      Contact contact = buildContact().setNames(&quot;Jack&quot;, &quot;Endino&quot;).build();
      Account acct = buildAccount().setId(&quot;ID32&quot;).addContact(contact).build();
      ...
</pre></p>
<p>The builders&#8217; methods do not need to correspond one-for-one with the objects&#8217; data, but it must be trivially obvious to the most casual observer just what is being set.</p>
<p>The builder can even create collaborative objects, but only when the convenience gained greatly outweighs any loss of readaility. For example, if many accounts need to have a contact as a collaborator, a convenience method can be added to the builder.<br />
<pre class="brush: java;">
      Account acct = buildAccount().addSimpleContact(&quot;Jack&quot;, &quot;Endino&quot;).build();
      ...
</pre><br />
The danger here is drifting back toward telescoping helper methods, so these should be used sparingly.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=54&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/15/consider-a-builder-for-test-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Testing as Titration</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/12/testing-as-titration/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/12/testing-as-titration/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 17:00:01 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[Musings]]></category>
		<category><![CDATA[color commentary]]></category>
		<category><![CDATA[p3]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[weasel]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=72</guid>
		<description><![CDATA[This is the Baby Bear approved method: not too much, not too little, but just right.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=72&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I guess my head&#8217;s still thinking about <a href="http://effectivejavatesting.wordpress.com/2008/12/10/55/">weasel testing</a> and <a href="http://tinyurl.com/pingpongprog">ping ping programming</a>. And <a href="http://effectivejavatesting.wordpress.com/2008/12/08/justify-every-test/">JET</a>.</p>
<p>If you took high school chemistry, you may have had to titrate a hydrochloric acid solution with a sodium hydroxide solution. The idea was to perfectly balance the hydrochloric acid with the sodium hydroxide, so that the <a href="http://en.wikipedia.org/wiki/Phenolphthalein">phenolphthalein</a> is right at the edge between pink and white.</p>
<p>When you use weasel testing or p3 to do <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a>, and alternate rapidly between writing a little test and writing a little production code, you &#8220;titrate&#8221; your unit tests to perfectly cover your production code. You know you have enough tests, because you didn&#8217;t write any production code until you were forced to do so. And you know you didn&#8217;t write any extra tests, because you would have no motivation to write a test that won&#8217;t force a change in the production code. This is the Baby Bear approved method: not too much, not too little, but just right.</p>
<p>I suspect getting this just-enough-and-no-more is much harder to do when you write all your unit tests in advance, or when you write your unit tests after the fact. Maybe I should try this for comparison at some point.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/72/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=72&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/12/testing-as-titration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Use &#8220;Should&#8221; in Assertions</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/11/use-should-in-assertions/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/11/use-should-in-assertions/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 05:29:03 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[basic]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[assertion]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=66</guid>
		<description><![CDATA[Look at the messages in the following two assertions. Which one seems more correct to you? Part of the difficulty is that we read the same message in two different contexts. When we&#8217;re reading them in the code, it&#8217;s natural to read the messages as being the invariant which is needed to pass this test. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=66&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Look at the messages in the following two assertions. Which one seems more correct to you?</p>
<p><pre class="brush: java;">
assertTrue(&quot;list is exhausted&quot;, !iter.hasNext());
assertTrue(&quot;index is out of bounds&quot;, i &lt; 0);
</pre></p>
<p>Part of the difficulty is that we read the same message in two different contexts. When we&#8217;re reading them in the code, it&#8217;s natural to read the messages as being the invariant which is needed to pass this test. That is, when in code, it&#8217;s easy to read the message as <em>what is true when the test passes</em>.</p>
<p>But what do we see on the console when those same two assertions fail?</p>
<p><pre class="brush: java;">
java.lang.AssertionError: list is exhausted
java.lang.AssertionError: index is out of bounds
</pre></p>
<p>When reading the assertion error in console output, it is easy to read it as <em>what is true when the test fails</em>. The &#8220;list is exhausted&#8221; messages makes sense in the code (assuming exhaustion is what passes the test) while &#8220;index is out of bounds&#8221; makes more sense when read in the console output.</p>
<p>But if we turn them into &#8220;should statements&#8221;, they make sense in both contexts.</p>
<p><pre class="brush: java;">
assertTrue(&quot;list should be exhausted&quot;, !iter.hasNext());
assertTrue(&quot;index should not be out of bounds&quot;, i &lt; 0);
</pre></p>
<p><pre class="brush: java;">
java.lang.AssertionError: list should be exhausted
java.lang.AssertionError: index should not be out of bounds
</pre></p>
<div></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=66&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/11/use-should-in-assertions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Weasel Testing</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/10/55/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/10/55/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 17:00:44 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[Process]]></category>
		<category><![CDATA[p3]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[weasel]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=55</guid>
		<description><![CDATA[The better you get at playing Weasel, the better your tests will become.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=55&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://tinyurl.com/pingpongprog">Ping pong pair programming</a> is great.</p>
<p>You can get nearly the same effect, even if you have to develop solo. (But it won&#8217;t be near as much fun.)</p>
<p>The key to doing this effectively is that you need to have two personas. I&#8217;ve tried to come up with a good name for this technique, but so far I&#8217;ve only come up with &#8220;schizophrenic programming&#8221; or &#8220;devil&#8217;s advocate programming&#8221; or &#8220;stubborn testing&#8221; or even &#8220;two hats programming&#8221;. For now, let&#8217;s call it &#8220;weasel testing&#8221;.</p>
<p>You wear two hats, and you alternate between them. One hat belongs to the Good Tester. He&#8217;s only allowed to edit the test code. He&#8217;s always trying to force the Bad Programmer to write the correct production code. While you&#8217;re playing the Good Tester, your job is to write the next incremental piece of an excellent test. And your job is finished &#8212; for the moment &#8212; as soon as you have a correct but failing test.</p>
<p>Once the tests are failing, you change hats, and Weasel takes over. The Weasel isn&#8217;t a bad programmer. &#8220;Lazy&#8221; or &#8220;Contrarian&#8221; might describe Weasel better. Wearing Weasel&#8217;s hat, your job is to make all the tests pass by writing the simplest piece of code that satisfies the test. The simplest. No matter how stupid or wrong. Be stubborn. That Good Tester is trying to make you write the correct code, and you even know what code he&#8217;s trying to force you to write. But you&#8217;ll show him! You&#8217;ll think of some cockeyed but simple way to weasel out of his trap, and make the tests go green. Thereby showing the Good Tester that his tests aren&#8217;t up to snuff yet. Once the tests go green, it&#8217;s the Good Tester&#8217;s turn again. And you cycle back and forth until the Good Tester finally paints Weasel into a corner from which he can&#8217;t escape. At that point, you have a solid chunk of production code with full test coverage.</p>
<p>Do not underestimate how hard this is to do well. Every time you do TDD, <em>you already pretty much know what code you&#8217;re trying to force yourself to write</em>. This makes it very easy to write the &#8220;final, correct&#8221; code, even without sufficient test pressure. As a result, when you transition from Good Tester to Weasel, it&#8217;s worth pausing a moment, shifting contexts, and trying to think carefully about how to cheat and still get the tests to go green.</p>
<p>The better you get at playing Weasel, the better your tests will become.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=55&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/10/55/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Write Tests to be Read</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/09/write-tests-to-be-read/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/09/write-tests-to-be-read/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 17:00:58 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[basic]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=7</guid>
		<description><![CDATA[As well as being useful and correct, an effective test should be readable. An unreadable test is a test that gives no one confidence. Even if it is correct and useful, no one can tell. What are these tests supposed to do? Each of two tests sets values on an account, and adds that account to some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=7&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As well as being <em>useful</em> and <em>correct</em>, an effective test should be <span><em>readable</em></span>. An unreadable test is a test that gives no one confidence. Even if it <span>is</span> correct and useful, no one can tell.</p>
<p>What are these tests supposed to do?</p>
<p><pre class="brush: java;">
   public void testSomething4() {
     dataSetup(5, 17, new Account());
     verifyExpectations(&quot;ID17&quot;, &quot;17.0&quot;, &quot;22.0&quot;, null);
   }

   public void testSomething5() {
     Account acct = new Account();
     acct.setOverdueMonths(3);
     dataSetup(5, 17, acct);
     verifyExpectations(&quot;ID17&quot;, &quot;17.0&quot;, &quot;22.0&quot;, &quot;Overdue 90 days&quot;);
   }

   private void dataSetup(int i, int j, Displayable acct) {
     acct.setId(&quot;ID&quot; + j);
     acct.setBalance(j);
     acct.setNewCharges(i);
     widget.addDisplayLine(acct);
   }

   private void verifyExpectations(String id, String balance, String total, String errors) {
     DisplayLine line = widget.getDisplayLine(id);
     assertEquals(balance, line.getPreviousBalance());
     assertEquals(total, line.getCurrentBalance());
     assertEquals(errors, line.getErrorMessage());
   }
</pre></p>
<p>Each of two tests sets values on an account, and adds that account to some widget. Unfortunately, however, the helper methods dataSetup() and verifyExpectations() obscure the tests rather than clarifying. The reader will need to build an understanding of what these methods do before she can determine whether the test is needed and correct.</p>
<p> </p>
<p>Ideally, a reader should be able to understand <em>what</em> is being tested and <em>how</em> by looking solely at the test method. In tests, we can tolerate a bit more redundancy if it improves clarity.</p>
<p><pre class="brush: java;">
   public void testDisplayLines() {
     Account acct = new Account();
     acct.setId(&quot;ID17&quot;);
     acct.setBalance(17);
     acct.setNewCharges(5);

     widget.addDisplayLines(acct);

     DisplayLine line = widget.getDisplayLine(&quot;ID17&quot;);
     assertEquals(&quot;17.0&quot;, line.getPreviousBalance());
     assertEquals(&quot;22.0&quot;, line.getCurrentBalance());
     assertEquals(&quot;Overdue 90 days&quot;, line.getErrorMessage());
   }

   public void testDisplayLinesWithOverdueAccount() {
     Account acct = new Account();
     acct.setOverdueMonths(3);
     acct.setId(&quot;ID17&quot;);
     acct.setBalance(17);
     acct.setNewCharges(5);

     widget.addDisplayLines(acct);

     DisplayLine line = widget.getDisplayLine(&quot;ID17&quot;);
     assertEquals(&quot;17.0&quot;, line.getPreviousBalance());
     assertEquals(&quot;22.0&quot;, line.getCurrentBalance());
     assertEquals(null, line.getErrorMessage());
   }
</pre></p>
<p>The test methods have been given meaningful names. They have been refactored to expose the basic progression of any good test: set-up, exercise the code under test, and then assertions that test its correctness. In fact, we have formatted each test to visually separate each phase. Now we set the account id with a constant string, rather than &#8220;computing&#8221; it from the balance.</p>
<p>With this change, there is more redundancy, but we&#8217;ve only increased the line count by 3. By formatting the redundant parts identically, it is relatively easy to visually &#8220;diff&#8221; the two tests to understand how the two similar tests differ.</p>
<p>There are ways we can make these tests ever more readable, but that&#8217;s a topic for another post&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=7&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/09/write-tests-to-be-read/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Justify Every Test</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/08/justify-every-test/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/08/justify-every-test/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 17:00:15 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[basic]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[p3]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/?p=19</guid>
		<description><![CDATA[Done right, no production code is created without a corresponding test. But a less obvious advantage is that no tests are created without corresponding production code. <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=19&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Every test should justify its existence. You might think that if tests are good, then more tests are better, but that&#8217;s usually not the case. Extra tests that are truly redundant are dead weight in the test suite. They are a drag on agile development not only in slowing down the test suite, but they can also slow future development &#8212; they are extra tests that have to be changed as the application&#8217;s requirements change. (Integration tests or end-to-end tests are an exception to this rule.)</p>
<p>For example, let&#8217;s imagine that we have an <em>AbstractFlyable</em> and its subclasses, <em>Blimp</em>, <em>Helicopter</em>, and <em>Airplane</em>. To the extent that there is testable functionality in <em>AbstractFlyable</em> &#8211; maybe calculations related to speed or fuel consumption &#8212; we test those in <em>AbstractFlyableTest</em>. There&#8217;s really no good reason to re-test those capabilities in <em>BlimpTest</em>. <em>BlimpTest</em> should be testing the code that is blimp-specific.</p>
<p>This rule is often violated when frameworks are used. For example, it&#8217;s common for a web application to have a persistence framework hiding SQL usage underneath, and to be used repeatedly for storing all kinds of data for our application, whether blimps, repair parts, departure dates, or whatever. For each of these, we want to test the code that is specific to each use of the framework.</p>
<ul>
<li><em>Do</em> test that blimps are something persisted to the database. <em></em></li>
<li><em>Do</em> test specialized queries created just for blimps, such as a complex query to allow blimps to find football stadiums. <em></em></li>
<li><em>Don&#8217;t</em> test behavior that is inherited from the framework<em>, </em>such as the ability to compute air speed from data persisted by all <em>AbstractFlyables</em>.</li>
</ul>
<p>In some of the more rigorous forms of  <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> such as <a href="http://tinyurl.com/pingpongprog">ping pong</a> TDD or devil&#8217;s advocate testing (forthcoming blog post), a natural rhythm emerges:</p>
<ul>
<li>The developers imagine some piece of functionality they want to add to the code.</li>
<li>They write a test that pushes them in the right direction.</li>
<li>They implement as little production code as they can to get the test to pass. </li>
<li>And then they iterate this cycle until done. </li>
</ul>
<p>Done right, no production code is created without a corresponding test. But another, less obvious, advantage is that no tests are created without corresponding production code. No tests is added unless it is needed to drive development. As a result, the test code stays lean. This is harder to do if tests are written en masse before or after the production code.</p>
<p>Like your production code, your tests should be lean. And fast. J-E-T. Justify every test.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=19&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/08/justify-every-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>A Good Test is Useful, Correct, and Readable</title>
		<link>http://effectivejavatesting.wordpress.com/2008/12/03/a-good-test-is-useful-correct-and-readable/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/12/03/a-good-test-is-useful-correct-and-readable/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 16:02:00 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[basic]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/2008/12/03/a-good-test-is-useful-correct-and-readable/</guid>
		<description><![CDATA[A good test is useful because it reflects a requirement in the application. Obviously, this is very application-dependent, as a test for multiplying 200-digit integers makes sense for cryptographic applications, but is silly for a web application storing cake recipes. This is tied to the yagni principle &#8212; don&#8217;t build infrastructure, no matter how well-tested, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=8&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A good test is <span style="font-style:italic;">useful</span> because it reflects a requirement in the application. Obviously, this is very application-dependent, as a test for multiplying 200-digit integers makes sense for cryptographic applications, but is silly for a web application storing cake recipes. This is tied to the <a href="http://www.google.com/search?q=yagni">yagni</a> principle &#8212; don&#8217;t build infrastructure, no matter how well-tested, without clear and present need for it. When features are removed from an application, it&#8217;s imperative to remove associated infrastructure and tests.</p>
<p>There may be another aspect of usefulness &#8212; I&#8217;m still waffling on this one &#8212; in that a good test is a defense against likely failures. That is, we don&#8217;t usually test simple getters and setters, because they are not expected to fail. Should you buy accidental death and dismemberment insurance? Probably not, although the insurance is relatively cheap, as actuarially accidents are far less likely than illness or disease. Likewise, providing test insurance against something that&#8217;s highly unlikely to fail is probably a waste of resources.</p>
<p>A good test is <span style="font-style:italic;">correct</span>. A test that expects incorrect values, actually enforcing errors in the application, is a bad test. Almost as bad is a test that doesn&#8217;t effectively test what it purports to test, giving a false sense of security. Common examples of this are EasyMock tests which fail to verify the mocks, or tests which override methods of the class under test.</p>
<p>A good test is <span style="font-style:italic;">readable</span>. Readability is what gives subsequent readers of the test confidence that the test is useful and correct. Ideally, it should not take someone familiar with your application more than 30 seconds to understand what an individual test is testing and the basic mechanism by which you are testing that feature. It might take longer to ensure the correctness of any mocks, builders, and helpers you use, but the intent of those helpers should be plain in the top-level test.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=8&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/12/03/a-good-test-is-useful-correct-and-readable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
		<item>
		<title>Beware EasyMock&#8217;s Quirks</title>
		<link>http://effectivejavatesting.wordpress.com/2008/11/25/beware-easymock/</link>
		<comments>http://effectivejavatesting.wordpress.com/2008/11/25/beware-easymock/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 17:29:00 +0000</pubDate>
		<dc:creator>esnickell</dc:creator>
				<category><![CDATA[Doubles]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[mocks]]></category>

		<guid isPermaLink="false">http://effectivejavatesting.wordpress.com/2008/11/25/beware-easymock/</guid>
		<description><![CDATA[EasyMock is a framework designed to make construction of mocks straightforward. Essentially, you Create a mock with EasyMock. Set the expectations on the calls to be made on that mock by making those same calls. At this point you can also set return values. Put the mock into replay mode Run your test. Verify the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=6&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.easymock.org">EasyMock</a> is a framework designed to make construction of mocks straightforward. Essentially, you</p>
<ol>
<li>Create a mock with EasyMock.</li>
<li>Set the expectations on the calls to be made on that mock by making those same calls. At this point you can also set return values.</li>
<li>Put the mock into replay mode</li>
<li>Run your test.</li>
<li>Verify the mock.</li>
</ol>
<p>As an example,</p>
<p><pre class="brush: java;">
  Foo foo = EasyMock.createMock(Foo.class);
  Bar myBar = new Bar();
  EasyMock.expect(foo.someMethod(1, myBar, &quot;baz&quot;)).andReturn(17L);
  EasyMock.replay(foo);
  ClassUnderTest underTest = new ClassUnderTest(foo);
  long result = underTest.classUnderTest(&quot;baz&quot;, myBar);
  assertEquals(17L, result);
  EasyMock.verify(foo);
</pre></p>
<p>Before the replay call, the call to foo.someMethod() sets an expectation that someMethod should be called on foo with exactly the parameters 1, myBar, and &#8220;baz&#8221;. EasyMock will throw an exception if any other method is called, or if that method is called with different parameters. EasyMock.verify will throw an exception if any expectations have not been met.</p>
<p>EasyMock can be made more general than this. Let&#8217;s say that our class under test actually instantiates a Bar, and we want to all the call to someMethod no matter which Bar is passed in. We can do this, but it requires that we use EasyMock&#8217;s matchers rather than concrete values when setting up the expectation. Specifically, EasyMock.isA(Bar.class) matches any Bar. We can&#8217;t do</p>
<p><pre class="brush: java;">
  EasyMock.expect(foo.someMethod(    //WRONG!
      EasyMock.isA(Bar.class), &quot;baz&quot;).andReturn(17L);
</pre></p>
<p>because it turns out that when setting expectations on an EasyMock mock, you can&#8217;t mix concrete values and matches. You need to use all one or all the other.</p>
<p><pre class="brush: java;">
  EasyMock.expect(foo.someMethod(
      EasyMock.anyInt(),
      EasyMock.isA(Bar.class),
      EasyMock.eq(&quot;baz&quot;))).andReturn(17L);
</pre></p>
<p>This expectation will allow any single call to someMethod which is passed any int as the first argument, any Bar as the second, and the string &#8220;baz&#8221; as the third. It further turns out that EasyMock provides a mechanism for anyone to create their own argument matchers. We could, for example, create an argument matcher that only matches instances of Bar which satisfy some more specific criterion.</p>
<p>Now, if you think about it, all these matchers &#8212; whether EasyMock&#8217;s or custom &#8212; need to return a value of the correct type to be passed to foo.someMethod. Who knows what constructor&#8217;s Bar might have, and what might happen if EasyMock tries to instantiate one? And how can the fake Bar that is returned from EasyMock.isA() and handed to foo.someMethod() have the information to do the correct matching operation?</p>
<p>The answer is, it can&#8217;t. EasyMock cheats. And that brings us to our point. When using argument matchers, EasyMock expects &#8212; nay, requires &#8212; that each matcher make exactly one call to EasyMock.reportMatcher(&#8230;). And how does it know which matcher goes with which argument? Well, it lines them up. By calling</p>
<p><pre class="brush: java;">
  foo.someMethod(EasyMock.anyInt(), EasyMock.isA(Bar.class),
      EasyMock.eq(&quot;baz&quot;));
</pre></p>
<p>each of the EasyMock matchers does a call to EasyMock.reportMatcher(), which squirrels away a specific matcher in a global list. When the mock foo gets a call to someMethod, it expects to find 3 matchers in that global list. (Well, either 3 or zero, zero being the case where we&#8217;re using concrete values and not matchers.) If it find 1 or 2, instead, it throws a cryptic exception, because you mixed concrete values and matchers, and it has no way to know which argument each of the matchers should go with. It simply can&#8217;t tell.</p>
<p>So &#8230; problem #1 rears its head when we use our IDE&#8217;s automated refactoring tools. Someone at some later time decides that they need to generalize Foo&#8217;s someMethod, so they use their IDE to add another argument, a boolean let&#8217;s say, and they tell their IDE the default value is false. Our call using matchers now looks like</p>
<p><pre class="brush: java;">
  foo.someMethod(EasyMock.anyInt(),
      EasyMock.isA(Bar.class), EasyMock.eq(&quot;baz&quot;), false)
</pre></p>
<p>That someone probably has no idea this test is here. When they finally run the full test suite, the cryptic error pops up, they come snooping around this code. If they&#8217;re not already familiar with EasyMock and this particular quirk, they&#8217;re in for a fun time, until they discover that they need to change this to</p>
<p><pre class="brush: java;">
  foo.someMethod(EasyMock.anyInt(), EasyMock.isA(Bar.class),
      EasyMock.eq(&quot;baz&quot;), EasyMock.anyBoolean())
</pre></p>
<p>The second problem is even more insidious. What happens when we refactor our test method. For whatever reason, let&#8217;s say we extract the Bar matcher into a local variable.</p>
<p><pre class="brush: java;">
  Bar bar = EasyMock.isA(Bar.class);
  foo.someMethod(EasyMock.anyInt(), bar,
      EasyMock.eq(&quot;baz&quot;), EasyMock.anyBoolean());
</pre></p>
<p>And now EasyMock starts failing. Why? Because each argument matcher is responsible to call the global EasyMock.reportMatcher &#8212; in the correct order. By this refactoring, the reportMatcher calls are now out of order, and EasyMock is either going to have class cast exceptions, or the expections on someMethod that previously passed will now fail.</p>
<p>Or worse.</p>
<p>One way to avoid these problems is to decide not to use EasyMock at all, instead creating your own hand-rolled doubles. And if you decide to use EasyMock in your project, &#8230;</p>
<ol>
<li>Make sure at least someone on your team understands how EasyMock works under the hood.</li>
<li>Be aware of EasyMock&#8217;s all-concrete or all-matchers requirement, and that violations of this requirement can be violated by automated refactoring.</li>
<li>If you use EasyMock matchers, make sure that they are produced while marshalling the arguments to call the mock method, and not beforehand.</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/effectivejavatesting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/effectivejavatesting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/effectivejavatesting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/effectivejavatesting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/effectivejavatesting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/effectivejavatesting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/effectivejavatesting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/effectivejavatesting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/effectivejavatesting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/effectivejavatesting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/effectivejavatesting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/effectivejavatesting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/effectivejavatesting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/effectivejavatesting.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=effectivejavatesting.wordpress.com&amp;blog=5775902&amp;post=6&amp;subd=effectivejavatesting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://effectivejavatesting.wordpress.com/2008/11/25/beware-easymock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f61c51a06d3410ee92442bdc0921f28?s=96&#38;d=identicon" medium="image">
			<media:title type="html">esnickell</media:title>
		</media:content>
	</item>
	</channel>
</rss>
