<?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>JAW Speak &#187; testability</title>
	<atom:link href="http://jawspeak.com/category/testability/feed/" rel="self" type="application/rss+xml" />
	<link>http://jawspeak.com</link>
	<description>Jonathan Andrew Wolter</description>
	<lastBuildDate>Fri, 30 Jul 2010 20:39:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fast and Easily Testable GWT JUnit Tests</title>
		<link>http://jawspeak.com/2009/07/30/fast-and-easily-testable-gwt-junit-tests/</link>
		<comments>http://jawspeak.com/2009/07/30/fast-and-easily-testable-gwt-junit-tests/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 06:05:14 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testability]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=38</guid>
		<description><![CDATA[Reading time: 4 &#8211; 6 minutes

			
				
			
		
The principle we were trying to follow last November during a short Google Web Toolkit project was to test our controllers as much as possible outside of GWTTestCase. (It&#8217;s too slow). So that means never instantiate or reference a GWT widget in the controller. They must always be decoupled by [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 4 &#8211; 6 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2009%2F07%2F30%2Ffast-and-easily-testable-gwt-junit-tests%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2009%2F07%2F30%2Ffast-and-easily-testable-gwt-junit-tests%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<div>The principle <a href="http://ThoughtWorks.com">we</a> were trying to follow last November during a short Google Web Toolkit project was to test our controllers as much as possible outside of <code>GWTTestCase</code>. (It&#8217;s too slow). So that means never instantiate or reference a GWT widget in the controller. They must always be decoupled by interfaces. Here&#8217;s some of what we learned. Formerly my controller had an async event handler in it that showed a pop up message on a failure.</div>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> AsyncCallback asyncCallback <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AsyncCallback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onFailure<span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> caught<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Window</span>.<span style="color: #006633;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;An error occured, please try again. <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">+</span> caught.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSuccess<span style="color: #009900;">&#40;</span>PackageDto pkg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    firePackageUpdate<span style="color: #009900;">&#40;</span>pkg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// throw an event in here that is handled by a</span>
                            <span style="color: #666666; font-style: italic;">// listener, which is the gui code and which has no logic</span>
                            <span style="color: #666666; font-style: italic;">// (logic is in the controller!)</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<div>But since I want to run my controllers with regular JUnit, I can&#8217;t have the <tt>Window.alert</tt> call in there. If I did and ran a junit test, it would get this stack trace:</div>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">ExceptionInInitializerError</span>
Caused by<span style="color: #339933;">:</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">UnsupportedOperationException</span><span style="color: #339933;">:</span> ERROR<span style="color: #339933;">:</span> GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> is only usable in client code<span style="color: #339933;">!</span>
   It cannot be called, <span style="color: #000000; font-weight: bold;">for</span> example, from server code.  <span style="color: #000000; font-weight: bold;">If</span> you are running a unit test, check that your test
   <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #000000; font-weight: bold;">extends</span> GWTTestCase and that GWT.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> is not called from within an initializer or constructor.
<span style="color: #006633;">at</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">gwt</span>.<span style="color: #006633;">core</span>.<span style="color: #006633;">client</span>.<span style="color: #006633;">GWT</span>.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>GWT.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">91</span><span style="color: #009900;">&#41;</span>
at com.<span style="color: #006633;">google</span>.<span style="color: #006633;">gwt</span>.<span style="color: #006633;">user</span>.<span style="color: #006633;">client</span>.<span style="color: #003399;">Window</span>.<span style="color: #009900;">&#40;</span><span style="color: #003399;">Window</span>.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">230</span><span style="color: #009900;">&#41;</span>
... <span style="color: #cc66cc;">28</span> more</pre></div></div>

<div>I saw two initial solutions to removing the Window.alert(), and then the third recommended solution:</div>
<ol>
<li>Implement the AsyncCallback&lt;T&gt; interface with <tt>BaseAsyncCallback&lt;T&gt; </tt>, and implement the <tt>onFailure()</tt> to call <tt>Window.alert()</tt>. In tests, I would subclass and override <tt>onFailure()</tt> in tests, preventing the <tt>Window.alert()</tt> from executing. I generally don&#8217;t like subclassing for tests, so I wasn&#8217;t too keen on this.</li>
<li>Implement the <tt>AsyncCallback&lt;T&gt;</tt> interface with a class <tt>BaseAsyncCallback&lt;T&gt;</tt>, but this time take in a constructor parameter, FailureHandler (which would be an interface or class of my own.) Then in <tt>onFailure()</tt>, delegate to the FailureHandler field. In tests, pass a different FailureHandler implementation or subclass, to avoid calling the GWT widget code.</li>
<li><strong>The recommended solution is</strong> to treat this the same way I deal with <tt>onSuccess()</tt>. Tell a listener that a failure event occurred, and let it handle it.</li>
</ol>
<div>What is key is the <tt>notifyAboutFailure()</tt> method takes a String of the message to the user (possibly using internationalization along the way).<strong> So anything interesting (error processing) still happens on the Controller side</strong>. Right now our view can handle the error by calling <tt>Window.alert()</tt>, but later when we implement the nicer &#8220;flash or gmail yellow bar&#8221; style UI, that can be dropped in (and tested with <code>GWTTestCase</code> or Selenium).</div>
<div><strong>Principle to remember:</strong></div>
<div>
<ul>
<li><strong><em>Separate the Controllers from all references to Views (GWT UI objects) by using event listener / notifier interfaces.</em></strong></li>
</ul>
</div>
<div>You also should read:</div>
<div>
<ul>
<li>Martin Fowler&#8217;s <a href="http://martinfowler.com/eaaDev/uiArchs.html">Humble View</a> and <a href="http://martinfowler.com/eaaDev/SupervisingPresenter.html">Supervising Controller</a></li>
<li><a href="http://googletesting.blogspot.com/2009/02/with-all-sport-drug-scandals-of-late.html">MVP</a> post from google code testing blog</li>
<li><a href="http://code.google.com/events/io/sessions/GoogleWebToolkitBestPractices.html">GWT architecture talk</a> by Ray Ryan at google IO this year (pending a sample app using the event bus he describes. See <a href="http://groups.google.com/group/Google-Web-Toolkit/browse_frm/thread/38448a50c5707986/b00a99f2464ee08f?lnk=gst&amp;q=MVP#b00a99f2464ee08f">discussion group</a> and watch video below).</li>
</ul>
</div>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/PDuhR18-EdM&amp;hl=en&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/PDuhR18-EdM&amp;hl=en&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2009/07/30/fast-and-easily-testable-gwt-junit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Driven log4j Logging Code Example</title>
		<link>http://jawspeak.com/2009/06/21/test-driven-log4j-logging-code-example/</link>
		<comments>http://jawspeak.com/2009/06/21/test-driven-log4j-logging-code-example/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 01:17:37 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testability]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[operations]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=96</guid>
		<description><![CDATA[Reading time: 3 &#8211; 4 minutes

			
				
			
		
Update: Added a snippet so that you can easily assert a message was contained in the logged messages.
Frequently logging is not viewed as &#8220;important&#8221; by engineers as, say, the logic for refunding a customer&#8217;s purchase. The customer&#8217;s won&#8217;t mind, after all, if we forget to log something right? Maybe so, [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 3 &#8211; 4 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2009%2F06%2F21%2Ftest-driven-log4j-logging-code-example%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2009%2F06%2F21%2Ftest-driven-log4j-logging-code-example%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p><strong>Update</strong>: Added a snippet so that you can easily assert a message was contained in the logged messages.</p>
<p>Frequently logging is not viewed as &#8220;important&#8221; by engineers as, say, the logic for refunding a customer&#8217;s purchase. The customer&#8217;s won&#8217;t mind, after all, if we forget to log something right? Maybe so, until there are problems and the operational team supporting your application has very little ability to diagnose problems.</p>
<p><strong>I propose logging should be viewed as another user interface</strong>, and thus needs its&#8217; own set of acceptance and unit tests. This makes for more tedious and up front work for development teams &#8212; however I want to survive in production without a pager going off, and with fewer late night scrambles to ship patches.</p>
<p>Imagine a class that has logic associated with logging. <strong>Below you will see a unit test verifying logging statements are correct. </strong>Often logging uses statics, and as Misko has said, <a href="http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability/">statics are a death to testabilty</a>. With Guice you can easily inject loggers automatically for the class under construction. <em>This method gets around most of the issues with static loggers, although I still despise statics everywhere.</em></p>
<p>This is a basic example, but the point is to understand how to hook into appenders and add/change one to use a test appender.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jawspeak.common.testing</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.common.collect.Lists</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.AppenderSkeleton</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.spi.LoggingEvent</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
* Use me for spying on test logs and making asserts against them.
*
* Example how to use:
*        final SpyLoggerAppenderForTesting spyAppender = new SpyAppenderForTesting();
*        final Logger logger = Logger.getLogger(MyClass.class.getName());
*        try {
*            logger.addAppender(spyAppender);
*            // do something
*            assertEquals(Lists.newArrayList(&quot;Attempted to read such-and-such, but could not: oops&quot;), spyAppender.getMessagesLogged());
*        } finally {
*            logger.removeAppender(spyAppender); // clean up global state
*        }
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SpyLoggingAppenderForTesting <span style="color: #000000; font-weight: bold;">extends</span> AppenderSkeleton <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> messages <span style="color: #339933;">=</span> Lists.<span style="color: #006633;">newArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> append<span style="color: #009900;">&#40;</span>LoggingEvent loggingEvent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    messages.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>loggingEvent.<span style="color: #006633;">getRenderedMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> getMessagesLogged<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> messages<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> doMessagesContain<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> snippet<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">boolean</span> isFound <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message <span style="color: #339933;">:</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>message.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span>snippet<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              isFound <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> isFound<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> requiresLayout<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here&#8217;s an example test where we make use of it. Note that we didn&#8217;t assert the lists are exactly the same, because in this project we were also using aspects to do logging, and depending on if you ran with or without the aspects enabled, a different number of logging messages would be created.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jawspeak.common.utility</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.common.collect.Maps</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.jawspeak.common.testing.SpyLoggingAppenderForTesting</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Level</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.After</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #339933;">*;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.LinkedHashMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SessionSizeLoggingTest <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">private</span> SpyLoggingAppenderForTesting spyAppender <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SpyLoggingAppenderForTesting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> Logger logger <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>SessionSizeLoggingTest.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> Level oldLoggingLevel <span style="color: #339933;">=</span> logger.<span style="color: #006633;">getLevel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> SessionSizeLogging sessionSizeLogging <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SessionSizeLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @Before
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    logger.<span style="color: #006633;">addAppender</span><span style="color: #009900;">&#40;</span>spyAppender<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    logger.<span style="color: #006633;">setLevel</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">TRACE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// set the level in case a log4j.properties or log4j.xml disables this level of logging</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @After
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> tearDown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    logger.<span style="color: #006633;">setLevel</span><span style="color: #009900;">&#40;</span>oldLoggingLevel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    logger.<span style="color: #006633;">removeAppender</span><span style="color: #009900;">&#40;</span>spyAppender<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> logBytesWhenUnprintable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    LinkedHashMap map <span style="color: #339933;">=</span> Maps.<span style="color: #006633;">newLinkedHashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;key&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">31</span>, <span style="color: #cc66cc;">127</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">97</span><span style="color: #009900;">&#125;</span>, <span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> sessionId <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;abc123&quot;</span><span style="color: #339933;">;</span>
    sessionSizeLogging.<span style="color: #006633;">logSessionSize</span><span style="color: #009900;">&#40;</span>logger, sessionId, map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">List</span> messagesLogged <span style="color: #339933;">=</span> spyAppender.<span style="color: #006633;">getMessagesLogged</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertTrue<span style="color: #009900;">&#40;</span>spyAppender.<span style="color: #006633;">doMessagesContain</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Session Size for session id: &quot;</span> <span style="color: #339933;">+</span> sessionId <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (approx. total &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">4</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; bytes)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #666666; font-style: italic;">// or use</span>
    assertTrue<span style="color: #009900;">&#40;</span>messagesLogged.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;  key[   3 bytes]                            key  --&gt;  value[   4 bytes] '<span style="color: #000099; font-weight: bold;">\\</span>u0001<span style="color: #000099; font-weight: bold;">\\</span>u0031<span style="color: #000099; font-weight: bold;">\\</span>u0127<span style="color: #000099; font-weight: bold;">\\</span>u0159'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// .. many more tests</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a trick that I don&#8217;t see written about too much, but I recommend it often. And, I&#8217;d love to have some readers post enhancements, links, and take testability further with respect to logging.</p>
]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2009/06/21/test-driven-log4j-logging-code-example/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Wrestling the Untestable Into Testable Code: Example with Test Driven SimpleTagSupport for custom JSP Tag</title>
		<link>http://jawspeak.com/2009/03/24/wrestling-the-untestable-into-testable-code-example-with-test-driven-simpletagsupport-for-custom-jsp-tag/</link>
		<comments>http://jawspeak.com/2009/03/24/wrestling-the-untestable-into-testable-code-example-with-test-driven-simpletagsupport-for-custom-jsp-tag/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 02:06:41 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testability]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=89</guid>
		<description><![CDATA[Reading time: 3 &#8211; 5 minutes

			
				
			
		
Let&#8217;s say app servers must be stateless. There is no affinity for a user pinned to a particular app server, so two requests will likely be handled by different Tomcats, Jetty&#8217;s, etc. This means nothing can go in HttpSession (we also can&#8217;t use clustering). And when it&#8217;s ecommerce, we can&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 3 &#8211; 5 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2009%2F03%2F24%2Fwrestling-the-untestable-into-testable-code-example-with-test-driven-simpletagsupport-for-custom-jsp-tag%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2009%2F03%2F24%2Fwrestling-the-untestable-into-testable-code-example-with-test-driven-simpletagsupport-for-custom-jsp-tag%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>Let&#8217;s say app servers must be stateless. There is no affinity for a user pinned to a particular app server, so two requests will likely be handled by different Tomcats, Jetty&#8217;s, etc. This means nothing can go in HttpSession (we also can&#8217;t use clustering). And when it&#8217;s ecommerce, we can&#8217;t require cookies or that might cost millions in lost revenue. Given these constraints, you have one valid option left: custom url rewriting over all our links (GET&#8217;s) and with hidden form fields (POST&#8217;s).</p>
<p>You can edit every link and every form to pass some unique id around. But what if you have two or three id&#8217;s that need to pass around for legacy reasons? Sounds like a custom tag would be nice. Something you can put in your jsp&#8217;s (oh yeah, let&#8217;s say we&#8217;re using jsp&#8217;s too).</p>
<p>This introduces me to the testability challenge. Write your own tag extending SimpleTagSupport that would ensure parameters were always in a link, and write another tag to write out the three hidden input fields inside every form.</p>
<p>But, SimpleTagSupport is part of the good old servlet and jsp API, so it&#8217;s very bloated with context objects, deep inheritance, callbacks, and cruft that makes for difficult unit testing. Just look at the public interface, one method: <tt>public void doTag() throws JspException, IOException</tt>.</p>
<p>Here&#8217;s my test driven test case (with 100% coverage, through the public API), which takes advantage of Spring MVC&#8217;s MockHttpServletRequest and MockHttpServletResponse objects (which are <a href="http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html">really fakes</a>). I&#8217;m also using <a href="http://mockito.org/">Mockito</a>, which I prefer over <a href="http://easymock.org/">EasyMock</a> and <a href="http://www.jmock.org/">JMock</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jawspeak.dotcom.tag</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #339933;">*;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #339933;">*;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.invocation.InvocationOnMock</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.mockito.stubbing.Answer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.mock.web.MockHttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.mock.web.MockHttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.mock.web.MockPageContext</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.tagext.JspFragment</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Writer</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StatefulLinkTagTest_SettingSessionId <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> StatefulLinkTag statefulLinkTag <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StatefulLinkTag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> MockHttpServletRequest request <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockHttpServletRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> MockHttpServletResponse response <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockHttpServletResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> SESSION_ID <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ADFHE13&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	@Before
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> given<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sid&quot;</span>, SESSION_ID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		statefulLinkTag.<span style="color: #006633;">setJspContext</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MockPageContext<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, request, response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		JspFragment jspBodyFragment <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>JspFragment.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		doAnswer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> FakeJspBodyAnswerer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">when</span><span style="color: #009900;">&#40;</span>jspBodyFragment<span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Writer</span><span style="color: #009900;">&#41;</span> anyObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		statefulLinkTag.<span style="color: #006633;">setJspBody</span><span style="color: #009900;">&#40;</span>jspBodyFragment<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldRenderLinkWhenThereIsNoParameter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		statefulLinkTag.<span style="color: #006633;">setHref</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/myPage.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		statefulLinkTag.<span style="color: #006633;">doTag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> contentAsString <span style="color: #339933;">=</span> response.<span style="color: #006633;">getContentAsString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>/myPage.html?sid=&quot;</span> <span style="color: #339933;">+</span> 
                     SESSION_ID <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&amp;gt;link text body&amp;lt;/a&amp;gt;&quot;</span>, contentAsString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldRenderLinkWhenThereIsAlreadyAParameter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		statefulLinkTag.<span style="color: #006633;">setHref</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/myPage.html?my_param=foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		statefulLinkTag.<span style="color: #006633;">doTag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> contentAsString <span style="color: #339933;">=</span> response.<span style="color: #006633;">getContentAsString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>/myPage.html?my_param=foo&amp;amp;sid=&quot;</span> <span style="color: #339933;">+</span> 
                    SESSION_ID <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&amp;gt;link text body&amp;lt;/a&amp;gt;&quot;</span>, contentAsString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** Mockito Answer implementation to manipulate the parameters we pass 
	 * into JspFragment.invoke() due to the ugly servlet API. */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> FakeJspBodyAnswerer <span style="color: #000000; font-weight: bold;">implements</span> Answer <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> answer<span style="color: #009900;">&#40;</span>InvocationOnMock invocationOnMock<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">StringWriter</span> writer <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#41;</span> invocationOnMock.<span style="color: #006633;">getArguments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;link text body&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As for the implementation, it&#8217;s pretty straightforward after writing the tests. It doesn&#8217;t show covering multiple ID&#8217;s appended to the link, but it is now easy to test drive implementing them.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jawspeak.dotcom.tag</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.JspException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.PageContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.tagext.SimpleTagSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.StringWriter</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StatefulLinkTag <span style="color: #000000; font-weight: bold;">extends</span> SimpleTagSupport <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> href<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setHref<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> href<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">href</span> <span style="color: #339933;">=</span> href<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doTag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JspException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		PageContext pageContext <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PageContext<span style="color: #009900;">&#41;</span> getJspContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getJspContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>createTagText<span style="color: #009900;">&#40;</span>
                      <span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> pageContext.<span style="color: #006633;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> createTagText<span style="color: #009900;">&#40;</span>HttpServletRequest request<span style="color: #009900;">&#41;</span>
                       <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, JspException <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">StringWriter</span> stringWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>href<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>href.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;?&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			stringWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;sid=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			stringWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;?sid=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		stringWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>request.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		getJspBody<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>stringWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		stringWriter.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/a&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> stringWriter.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Questions, comments? Want to bash Java and tell me why I should be using Ruby, Erlang or Scala? For more about the SimpleTagSupport interface check <a href="http://www.sitepoint.com/article/jsp-2-simple-tags/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2009/03/24/wrestling-the-untestable-into-testable-code-example-with-test-driven-simpletagsupport-for-custom-jsp-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using (These) Anonymous Inner Classes is Probably Too Clever for Your Own Good</title>
		<link>http://jawspeak.com/2009/03/10/using-these-anonymous-inner-classes-is-probably-too-clever-for-your-own-good/</link>
		<comments>http://jawspeak.com/2009/03/10/using-these-anonymous-inner-classes-is-probably-too-clever-for-your-own-good/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 04:33:09 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testability]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=81</guid>
		<description><![CDATA[Reading time: 3 &#8211; 5 minutes

			
				
			
		
Sometimes it is tempting to have less verbose Java code and be clever. Say you have an object you need to build and set several properties on it, then use it. This way looks clever, but is a bad idea &#8211; especially in production code. Read on for why.

  [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 3 &#8211; 5 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2009%2F03%2F10%2Fusing-these-anonymous-inner-classes-is-probably-too-clever-for-your-own-good%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2009%2F03%2F10%2Fusing-these-anonymous-inner-classes-is-probably-too-clever-for-your-own-good%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>Sometimes it is tempting to have less verbose Java code and be clever. Say you have an object you need to build and set several properties on it, then use it. This way looks clever, but is a bad idea &#8211; especially in production code. Read on for why.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  	TripChoice myTrip <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TripChoice<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">// look at these clever initialization blocks!</span>
			setDestination<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;San Francisco&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			setAvailableRoutes<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LAX-SFO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BUR-SFO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ONT-SFO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is a huge problem because it can cause a <tt>NotSerializableException</tt> or create a memory leak. How? Because with that clever anonymous class and initialization block, there is an implicit reference to the outer class&#8217; instance. That outer class will not be garbage collected so long as there is one reference to the anonymous class. Worse, if we ever</p>
<p>Here&#8217;s the typical way to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">                <span style="color: #666666; font-style: italic;">// blah blah, this is typical, and a rather boring looking great big block of text.</span>
		<span style="color: #666666; font-style: italic;">// (That's also probably scattered over several places in your code when you build these objects).</span>
		TripChoice myTrip <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TripChoice<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		myTrip.<span style="color: #006633;">setDestination</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;San Francisco&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Set</span> availableRoutes <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		availableRoutes.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LAX-SFO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		availableRoutes.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;BUR-SFO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		availableRoutes.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ONT-SFO&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		myTrip.<span style="color: #006633;">setAvailableRoutes</span><span style="color: #009900;">&#40;</span>availabileRoutes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here&#8217;s why this improved version is even not the best:</p>
<ul>
<li>It uses setters. Setters allow your object to be constructed in an inconsistent state. There are some situations setters are fine (i.e. form backing objects) but often your code is more clear without setters. In this <em>particular</em> case the setters aren&#8217;t egregious, but I still consider them a smell and much more verbose way that constructor injection. Read more about the problem with setter injection <a href="http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/">here</a></li>
<li>It is verbose.</li>
<li>It is also mutable (often a negative), again a problem due to the use of setters.</li>
</ul>
<p>So what&#8217;s the best way to do it? I have found constructor injection and in our particular example use of <a href="http://code.google.com/p/google-collections/">Google Collections</a>. As for seeing an implementation of the best solution &#8211; just leave a comment.</p>
<p>Below is a full example you can run that  illustrates the hard to catch serialization bug.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jawspeak</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.common.collect.Lists</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #339933;">*;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ByteArrayOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.NotSerializableException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Understands the danger of anonymous subclasses for lists.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TooCleverForOurOwnGoodTest <span style="color: #009900;">&#123;</span>
&nbsp;
        @Test<span style="color: #009900;">&#40;</span>expected <span style="color: #339933;">=</span> <span style="color: #003399;">NotSerializableException</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>  <span style="color: #666666; font-style: italic;">// UPDATE: Thanks for the reminder from Dennis, below</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> serializableDangerBugFromPatricksCleverness<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// you think a List would be serializable, which it is. And so are Strings.</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> myString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">List</span> strings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			add<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;two&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// However, because there is an anonymous subclass of ArrayList created,</span>
			<span style="color: #666666; font-style: italic;">// there is an implicit reference to the outer class, which is **not serializable**.</span>
			<span style="color: #666666; font-style: italic;">// This is a nice feature, which lets us have closure-ish syntax in java, such as</span>
			<span style="color: #666666; font-style: italic;">// the following reference to the outer myString.</span>
			add<span style="color: #009900;">&#40;</span>myString<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// It will throw the exception even without this reference.</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// call your method that requires args to be serializable</span>
		<span style="color: #003399;">ByteArrayOutputStream</span> baos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">ObjectOutputStream</span> oos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span>baos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oos.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>strings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// The particular bug I had was even worse, because we were creating leaking a reference</span>
		<span style="color: #666666; font-style: italic;">// through that List's implicit reference to the outer object.</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> serializableDangerBugSafeButNotClever<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// A List is serialiazable. And so are Strings.</span>
		<span style="color: #003399;">List</span> strings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		strings.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		strings.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;two&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// No longer is there a reference to the outer class.</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// call your method that requires args to be serializable</span>
		<span style="color: #003399;">ByteArrayOutputStream</span> baos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">ObjectOutputStream</span> oos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span>baos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oos.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>strings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> serializableDangerBugSafeAndNiceLooking<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// A List is serialiazable. And so are Strings.</span>
		<span style="color: #003399;">List</span> strings <span style="color: #339933;">=</span> Lists.<span style="color: #006633;">newArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #0000ff;">&quot;two&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// call your method that requires args to be serializable</span>
		<span style="color: #003399;">ByteArrayOutputStream</span> baos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">ObjectOutputStream</span> oos <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span>baos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		oos.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>strings<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2009/03/10/using-these-anonymous-inner-classes-is-probably-too-clever-for-your-own-good/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>One Unit Test should have Prevented Google from Categorizing the Entire Internet as Malware</title>
		<link>http://jawspeak.com/2009/01/31/one-unit-test-should-have-prevented-google-from-categorizing-the-entire-internet-as-malware/</link>
		<comments>http://jawspeak.com/2009/01/31/one-unit-test-should-have-prevented-google-from-categorizing-the-entire-internet-as-malware/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 21:30:40 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[testability]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=69</guid>
		<description><![CDATA[Reading time: 2 &#8211; 3 minutes

			
				
			
		
The google wide massive glitch which this morning, categorized nearly every search result as malware could have been prevented by unit testing. This is a wonderful example why even &#8220;silly little scripts&#8221; should be test driven.

This is what happened. A file was checked in with a &#8216;/&#8217; in it. This [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 2 &#8211; 3 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2009%2F01%2F31%2Fone-unit-test-should-have-prevented-google-from-categorizing-the-entire-internet-as-malware%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2009%2F01%2F31%2Fone-unit-test-should-have-prevented-google-from-categorizing-the-entire-internet-as-malware%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>The google wide massive glitch which this morning, <strong>categorized nearly every search result as malware</strong> could have been prevented by unit testing. This is a wonderful example why <strong>even &#8220;silly little scripts&#8221; should be test driven</strong>.</p>
<p><a href="http://jawspeak.com/wp-content/uploads/2009/01/googlemalware.jpg"><img class="alignnone size-full wp-image-70" title="googlemalware" src="http://jawspeak.com/wp-content/uploads/2009/01/googlemalware.jpg" alt="" width="500" height="405" /></a></p>
<p><strong>This is what happened. A file was checked in with a &#8216;/&#8217; in it. This file listed some or all of the sites Google warns to be dangerous malware sites.</strong> When it rolled out to different data centers it it caused search results to be rendered with warning messages next to almost every search result.</p>
<p><strong>One could have written unit tests to assert that typos (such as &#8216;/&#8217;) are not parsed, accidentally causing the entire web to be miscategorized.</strong> I really hope to read more about this on the <a href="http://googletesting.blogspot.com/">Google Testing Blog</a>, it&#8217;s a prime example for this company to take leadership and further promote Unit Testing, and even the opportunity for Test Driven Development. <a href="http://misko.hevery.com/2008/11/24/guide-to-writing-testable-code/">Miško</a>, care to take that post up?</p>
<p>They handled it well, and <span style="background-color: white;"><span style="color: #0b5394;"><span style="border-collapse: collapse;"><span style="font-weight: bold;"><span style="font-style: italic;"><span style="color: #0b5394;"><span style="color: black;"><span style="font-weight: normal;"><span style="font-style: normal;">Marissa Mayer </span></span></span></span></span></span></span></span></span>made a post on the <a href="http://googleblog.blogspot.com/2009/01/this-site-may-harm-your-computer-on.html">Official Google Blog</a>:</p>
<blockquote><p><span style="background-color: white;"><span style="color: #0b5394;"><span style="border-collapse: collapse;"><span style="color: #0b5394;">We periodically update that list and released one such update to the site this morning.<span style="color: #0b5394;"> </span></span><span style="color: black;">Unfortunately (and here&#8217;s the human error), the URL of &#8216;/&#8217; was mistakenly checked in as a value to the file and &#8216;/&#8217; expands to all URLs.</span></span></span></span></p></blockquote>
<p><strong>I say let&#8217;s use this as a publicity moment for testing</strong>. I am certain that testing has literally prevented many disasters in complex systems, however because the tests did their work &#8212; they got no publicity. Let&#8217;s see promotion of &#8220;lessons learned,&#8221; Google!</p>
<p>Where have you been saved by tests? Or, where was your big blunder that a test could have fixed?</p>
]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2009/01/31/one-unit-test-should-have-prevented-google-from-categorizing-the-entire-internet-as-malware/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Linking to Miško&#8217;s, Russ&#8217; and my Testability Guide</title>
		<link>http://jawspeak.com/2008/11/29/linking-to-miskos-russ-and-my-testability-guide/</link>
		<comments>http://jawspeak.com/2008/11/29/linking-to-miskos-russ-and-my-testability-guide/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 22:26:26 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testability]]></category>
		<category><![CDATA[thoughtworks]]></category>

		<guid isPermaLink="false">http://jawspeak.com/?p=60</guid>
		<description><![CDATA[Reading time: 4 &#8211; 6 minutes

			
				
			
		
I had the great pleasure of collaborating with Miško Hevery and Russ Rufer in creating the Guide for Writing Testable Code. Please feel free to check it out, and leave comments here or on his blog.
Here&#8217;s a peek at the different flaws to watch out for, and warn your co-workers [...]]]></description>
			<content:encoded><![CDATA[<p>Reading time: 4 &#8211; 6 minutes</p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjawspeak.com%2F2008%2F11%2F29%2Flinking-to-miskos-russ-and-my-testability-guide%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fjawspeak.com%2F2008%2F11%2F29%2Flinking-to-miskos-russ-and-my-testability-guide%2F&amp;style=normal&amp;service=bit.ly" height="61" width="51" /><br />
			</a>
		</div>
<p>I had the great pleasure of collaborating with <a href="http://misko.hevery.com/about/">Miško Hevery</a> and <a href="http://groups.yahoo.com/group/siliconvalleypatterns/">Russ Rufer</a> in creating the <a href="http://misko.hevery.com/code-reviewers-guide/">Guide for Writing Testable Code</a>. Please feel free to check it out, and leave comments here or on his blog.</p>
<p>Here&#8217;s a peek at the different flaws to watch out for, and warn your co-workers about. If you see this in code that is getting checked in, bring it to the attention to your team. See Miško&#8217;s <a href="http://misko.hevery.com/2008/11/24/guide-to-writing-testable-code/">post for the full list</a>.</p>
<p><a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/">Flaw #1: Constructor does Real Work (link)<br />
</a></p>
<ul>
<li>If you have a constructor that is doing &#8220;work&#8221; you&#8217;ve established a contract that everyone who wants to create this object also is forced to wait for that &#8220;work&#8221; to happen. This becomes a huge problem in small unit tests. Frequently unit tests create many, many instances of the object under test &#8211; and any work you do in the constructor slows down tests.</li>
<li>The solution? Break up the responsibility into two objects: a builder of factory to do all the heavy lifting to construct the object, and whatever the original object was that had the heavy constructor. When you split the responsibilities up you will have a better object oriented design, and make it easy for more flexible construction. If you&#8217;re tempted to create an <em>init()</em> method and put the work in there &#8211; avoid this siren song. It&#8217;s a smell of mixed responsibilities and a poorly behaving object that isn&#8217;t really ready when the constructor completes.</li>
<li>Warning signs are:
<ul>
<li><span style="font-family: Courier New,monospace;">new</span> keyword in a constructor or at field declaration</li>
<li> Static method calls in a constructor or at 	field declaration</li>
<li> Anything more than field assignment in 	constructors</li>
<li> Object not fully initialized after the 	constructor finishes (watch out for <span style="font-family: Courier New,monospace;">initialize</span> methods)</li>
<li> Control flow (conditional or looping logic) 	in a constructor</li>
<li> Code does complex object graph construction 	inside a constructor rather than using a factory or builder</li>
<li> Adding or using an initialization block</li>
</ul>
</li>
</ul>
<p><a href="http://misko.hevery.com/code-reviewers-guide/flaw-digging-into-collaborators/">Flaw #2: Digging into Collaborators (link)<br />
</a></p>
<ul>
<li>Jumping from one object to another to another to get what you want makes for hard to test and hard to refactor code. This is commonly viewed as the Law of Demeter violation, however you can dig into collaborators without breaking the letter of the law. This is hard to work with in tests, because your tests need to do a great deal of setup stuffing objects in objects into other objects, or else you&#8217;ll get null pointers.</li>
<li>Warning signs are:
<ul>
<li>Objects are passed in but never used directly 	(only used to get access to other objects)</li>
<li>Law of Demeter violation: method call chain 	walks an object graph with more than one dot (<span style="font-family: Courier New,monospace;">.</span>)</li>
<li>Suspicious names: <span style="font-family: Courier New,monospace;">context</span>, 	<span style="font-family: Courier New,monospace;">environment</span>, <span style="font-family: Courier New,monospace;">principal</span>, 	<span style="font-family: Courier New,monospace;">container</span>, or manager</li>
</ul>
</li>
</ul>
<p><a href="http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/">Flaw #3: Brittle Global State &amp; Singletons (link)<br />
</a></p>
<ul>
<li>Global state (usually made possible through the <em>static</em> keyword in Java) creates hard to test and modify code. Parallelizing tests is often impossible, and tests that forget to clean up the global state after running interact undesirably with other tests (causing unexpected failures).</li>
<li>Warning signs are:
<ul>
<li>Adding or using singletons</li>
<li>Adding or using static fields or static 	methods</li>
<li>Adding or using static initialization blocks</li>
<li>Adding or using registries</li>
<li>Adding or using service locators</li>
</ul>
</li>
</ul>
<p><a href="http://misko.hevery.com/code-reviewers-guide/flaw-class-does-too-much/">Flaw #4: Class Does Too Much (link)<br />
</a></p>
<ul>
<li>Object oriented design allows you to split responsibilities into individually tested objects. Using an object oriented language does not prevent people from writing procedural code. In fact, most times when someone does not want to write tests it is because their code is hard to test.</li>
<li>Warning Signs:
<ul>
<li>Summing up what the class does includes the 	word “and”</li>
<li>Class would be challenging for new team 	members to read and quickly “get it”</li>
<li>Class has fields that are only used in some 	methods</li>
<li>Class has static methods that only operate on 	parameters</li>
</ul>
</li>
</ul>
<p>There&#8217;s typically no magic wand to wave and make hard to test code instantly testable. Engineers need to have an open mind and learn how to write code designed for testability. (In my experience 90% of the time this involves test driven design and test driven development.) These ideas above, and many of Miško&#8217;s other posts are a fantastic starting point for adopting that new way of thinking.</p>
<p>Please link it up with your favorite testability mindset readings.</p>
]]></content:encoded>
			<wfw:commentRss>http://jawspeak.com/2008/11/29/linking-to-miskos-russ-and-my-testability-guide/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
