<?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>Code Adept &#187; test first</title>
	<atom:link href="http://blog.code-adept.com/tag/test-first/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.code-adept.com</link>
	<description>Random thoughts on Agile development and other things geeky.</description>
	<lastBuildDate>Thu, 09 Feb 2012 01:15:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.code-adept.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Code Adept &#187; test first</title>
		<link>http://blog.code-adept.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.code-adept.com/osd.xml" title="Code Adept" />
	<atom:link rel='hub' href='http://blog.code-adept.com/?pushpress=hub'/>
		<item>
		<title>Code Retreat GR Recap</title>
		<link>http://blog.code-adept.com/2010/02/17/code-retreat-gr-recap/</link>
		<comments>http://blog.code-adept.com/2010/02/17/code-retreat-gr-recap/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 22:12:01 +0000</pubDate>
		<dc:creator>Jeremy Anderson</dc:creator>
				<category><![CDATA[Conferences]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[code retreat]]></category>
		<category><![CDATA[craftsmanship]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[kata]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test first]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.code-adept.com/?p=154</guid>
		<description><![CDATA[On February 6th I hosted the first Code Retreat to hit West Michigan, and we really couldn&#8217;t have asked for a nicer day for a Code Retreat. Well, maybe a little bit warmer weather, but hey, it&#8217;s February in Michigan, what do you expect? So after a quick stop at Panera Bread to get some bagels, scones [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.code-adept.com&amp;blog=2511053&amp;post=154&amp;subd=codeadept&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On February 6th <a title="Jeremy Anderson" href="http://www.atomicobject.com/pages/Jeremy+Anderson/">I</a> hosted the first <a title="Code Retreat" href="http://coderetreat.ning.com/">Code Retreat</a> to hit West Michigan, and we really couldn&#8217;t have asked for a nicer day for a Code Retreat. Well, maybe a little bit warmer weather, but hey, it&#8217;s February in Michigan, what do you expect? So after a quick stop at Panera Bread to get some bagels, scones and muffins, I made my way down to <a title="Our Historic Building" href="http://www.atomicobject.com/pages/Our+Historic+Building">Atomic Object HQ</a> to start the coffee brewing in preparation for the attendees. Shortly after sunrise, <a title="Agile Shrugged" href="http://agileshrugged.com/blog/">Nayan Hajratwala</a> showed up to help with any last minute preparations before everyone else showed up.<br />
<span id="more-154"></span><br />
<img title="Code Retreaters pairing on Game of Life" src="http://spin.atomicobject.com/assets/2010/2/16/63951982.jpg" alt="Code Retreat GR" /></p>
<p>Soon about 20 people from all parts of the region had showed up to practice TDD and learn with each other, including one guy who came all the way down from Marquette, MI just to attend. He officially got the &#8220;I traveled the furthest&#8221; award for the day. We were also joined by <a title="XProgramming" href="http://xprogramming.com/index.php">Ron Jeffries</a>and <a title="Hendrickson XP" href="http://www.hendricksonxp.com/">Chet Hendrickson</a>, who had agreed to come and be my professional trouble makers for the day. Shortly after 9:00, once everyone had been sufficiently caffeinated, we decided to get started. One of the attendees had mentioned something about <a title="Corey Haines" href="http://coreyhaines.com/">Corey Haines</a> putting together a<a title="coreyhaines / practice_game_of_life @ github" href="http://github.com/coreyhaines/practice_game_of_life">set of Cucumber features</a> at one of the previous Code Retreats in Chicago, so some of the pairs decided to give that a whirl. After some yak shaving we managed to get through the first iteration of the morning and retrospected on what happened and continued on into the second iteration of the day.</p>
<p>My original plan was to just sort of float around, help facilitate, and observe everyone else pairing, however when I noticed Ron Jeffries didn&#8217;t have a pair for the second iteration, I took the opportunity to pair with him. Neither of us knew Cucumber very well, so we decided to give that a whirl. If ever you get the chance to pair with either Ron or Chet, don&#8217;t think twice about it, just do it. Ron had at one point in the day managed to &#8211; as one participant described &#8220;&#8230;[kick his] BDD mindset a bit out of place. . . &#8220;</p>
<p>Before we knew it, lunch was upon us. It turns out that Corey Haines was hosting another Code Retreat in Seattle that day, so we fired up Skype and greeted our fellow Code Retreaters on the west coast as they were just getting ready to start for the day. Then we all proceeded to enjoy the taco bar that had been delivered for lunch and continued to retrospect on the days events so far. When we were all finished stuffing our faces with Qdoba, Mike Sweiton and myself gave our participants a quick tour of Atomic Object HQ, showing off our open space, <a title="Information Radiators" href="http://spin.atomicobject.com/2010/02/08/information-radiators">stoplight</a>, CI server, and our embedded projects workbench.</p>
<p>Now that our food had a chance to settle, it was back to pairing for a few more iterations of Conway&#8217;s Game of Life. After one of the afternoon retrospectives, for a little bit of a distraction, we watched a video of someone implementing <a title="Game of Life in APL" href="http://www.youtube.com/watch?v=a9xAKttWgP4">Conway&#8217;s Game of Life in APL</a>. This was spawned by an email thread that circulated right before the Code Retreat about how to implement the<a title="Game of Life in single line of APL" href="http://www.dyalog.com/dfnsdws/c_life.htm">Game of Life in a single line of APL</a>, which still blows my mind.</p>
<p>Finally, by the time the end of the day had finally arrived, we had lost a few of our fellow coders and we were ready to call it a day. Those of us who were still left standing at the end of the day took a walk around the corner to <a title="The Green Well" href="http://thegreenwell.com/">The Green Well</a>, one of the many local establishments in the Eastown area, for some much needed unwinding. We continued to retrospect on the day&#8217;s happenings over a few local microbrews and some delicious items from the menu. All in all I would have to say this was a successful Code Retreat. Everyone had a great time, we all got to pair program with some great folks we wouldn&#8217;t normally get to pair with, and &#8211; most importantly &#8211; learning happened. Though many of the Code Retreats in the past have used Java as their language of choice, in my opinion I think using Ruby for this Code Retreat was the right choice. It afforded us much less yak shaving than would have probably been necessary had we been using Java. I&#8217;m looking forward to hosting another Code Retreat later this year when the weather is a little warmer, and hopefully attending the upcoming <a title="Code Retreat Philadelphia" href="http://coderetreat.ning.com/xn/detail/2712512:Event:4161?xg_source=activity">Code Retreat being hosted in Philadelphia</a> by <a title="Beards and Keyboards" href="http://sebastianlab.com/">Sebastian Hermida</a>.</p>
<br /> Tagged: <a href='http://blog.code-adept.com/tag/agile/'>agile</a>, <a href='http://blog.code-adept.com/tag/code-retreat/'>code retreat</a>, <a href='http://blog.code-adept.com/tag/craftsmanship/'>craftsmanship</a>, <a href='http://blog.code-adept.com/tag/cucumber/'>cucumber</a>, <a href='http://blog.code-adept.com/tag/kata/'>kata</a>, <a href='http://blog.code-adept.com/tag/ruby/'>Ruby</a>, <a href='http://blog.code-adept.com/tag/tdd/'>tdd</a>, <a href='http://blog.code-adept.com/tag/test-first/'>test first</a>, <a href='http://blog.code-adept.com/tag/testing/'>testing</a>, <a href='http://blog.code-adept.com/tag/unit-testing/'>unit testing</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeadept.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeadept.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeadept.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeadept.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeadept.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeadept.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeadept.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeadept.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeadept.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeadept.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeadept.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeadept.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeadept.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeadept.wordpress.com/154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.code-adept.com&amp;blog=2511053&amp;post=154&amp;subd=codeadept&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.code-adept.com/2010/02/17/code-retreat-gr-recap/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/611c688725819bb23742a714a9c33dfe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jeremy</media:title>
		</media:content>

		<media:content url="http://spin.atomicobject.com/assets/2010/2/16/63951982.jpg" medium="image">
			<media:title type="html">Code Retreaters pairing on Game of Life</media:title>
		</media:content>
	</item>
		<item>
		<title>Unit Testing JSP Custom Tags</title>
		<link>http://blog.code-adept.com/2008/01/25/unit-testing-jsp-custom-tags/</link>
		<comments>http://blog.code-adept.com/2008/01/25/unit-testing-jsp-custom-tags/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 13:43:40 +0000</pubDate>
		<dc:creator>Jeremy Anderson</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[custom tags]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[mock testing]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test first]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.code-adept.com/?p=26</guid>
		<description><![CDATA[Testing J2EE components has always been a difficult task, which is probably why I see so many web projects that have few tests written for the web layer or sometimes none at all. Late last year Spring announced the release of Spring 2.5 , with some nice additions to the suite of mock testing objects [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.code-adept.com&amp;blog=2511053&amp;post=26&amp;subd=codeadept&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Testing J2EE components has always been a difficult task, which is probably why I see so many web projects that have few tests written for the web layer or sometimes none at all. Late last year Spring announced the release of Spring 2.5 , with some nice additions to the suite of mock testing objects for unit testing web components. That&#8217;s right unit testing web components, not in container testing. So like any good agile programmer let&#8217;s start with the test first.<span id="more-26"></span></p>
<p><pre class="brush: java;">

public class SomeCustomTagTest extends TestCase {

    private SomeServiceInterface mockService;
    private SomeCustomTag someCustomTag;
    private MockServletContext mockServletContext;
    private MockPageContext mockPageContext;
    private WebApplicationContext mockWebApplicationContext;

    protected void setUp() throws Exception {
        super.setUp();
        // Create the mock servlet context
        mockServletContext = new MockServletContext();

        // Create the mock Spring Context so that we can mock out the calls to getBean in the custom tag
        // Then add the Spring Context to the Servlet Context
        mockWebApplicationContext = createMock(WebApplicationContext.class);
        mockServletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                                        mockWebApplicationContext);

        // Create the MockPageContext passing in the mock servlet context created above
        mockPageContext = new MockPageContext(mockServletContext);

        // Create the mock service object using it's interface
        mockService = createMock(SomeServiceInterface.class);

        // Create an instance of the custom tag we want to test
        // set it's PageContext to the MockPageContext we created above
        someCustomTag = new SomeCustomTag();
        someCustomTag.setPageContext(mockPageContext);

        // Whenever you make a call to the doStartTag() method on the custom tag it calls getServletContext()
        // on the WebApplicationContext.  So to avoid having to put this expect statement in every test
        // I've included it in the setUp()
        expect(mockWebApplicationContext.getServletContext()).andReturn(mockServletContext).anyTimes();
    }

}</pre><br />
Some things you&#8217;ll notice in the setUp() method are that we&#8217;re creating a mock for the Spring Context. Since I don&#8217;t know of any way to have Spring inject dependencies into the custom tag, we&#8217;ll be getting a WebApplicationContext object from the container to get our beans from Spring. This mock allows us to fake calls to getBean() and return our mock service objects so we can unit test the custom tag without relying on external dependencies.<br />
Now in order to get this test to compile we need to create the custom tag class. You&#8217;ll notice that we&#8217;re extending the RequestContextAwareTag class instead of TagSupport. The RequestContextAwareTag class is a Spring helper class that gives us some convenience methods for getting the WebApplicationContext among other things. You&#8217;ll also see that the method we need to implement is called doStartTagInternal() instead of doStartTag().<br />
<pre class="brush: java;">

public class SomeCustomTag extends RequestContextAwareTag {

    protected int doStartTagInternal() throws Exception {

        // put some business logic here

        return SKIP_BODY;
    }

}</pre><br />
Now let&#8217;s write a failing test. Back in the unit test lets add a test method and a couple of helper methods for the mock objects.<br />
<pre class="brush: java;">

public void testDoStartTag() throws Exception{
    String param = &quot;Jeremy&quot;;
    String expectedOutput = &quot;Hello, &quot; + param;

    replayAllMocks();

    someCustomTag.setUserName(param);
    int tagReturnValue = someCustomTag.doStartTag();
    String output = ((MockHttpServletResponse) mockPageContext.getResponse()).getContentAsString();

    assertEquals(&quot;Tag should return 'SKIP_BODY'&quot;, TagSupport.SKIP_BODY, tagReturnValue);
    assertEquals(&quot;Output should be 'Hello, Jeremy'&quot;, expectedOutput, output);

    verifyAllMocks();
}

private void replayAllMocks() {
	replay(mockWebApplicationContext);
	replay(mockCompanyQuery);
}

private void verifyAllMocks() {
	verify(mockWebApplicationContext);
	verify(mockCompanyQuery);
} 

</pre><br />
In order to get the test to compile, you&#8217;ll need to modify the SomeCustomTag class to have a setter method for a userName parameter.  We would like our custom tag to say &#8220;Hello&#8221; to a username that is passed in, so let&#8217;s implement the behavior we are trying to accomplish.<br />
<pre class="brush: java;">

public class SomeCustomTag extends RequestContextAwareTag {

    private String userName;

    // A setter for an attribute that will be set in the custom tag
    public void userName(String userName) {
        this.userName = userName;
    }

    protected int doStartTagInternal() throws Exception {

        pageContext.getOut().print(&quot;Hello, &quot; + userName);

        return SKIP_BODY;
    }

}

</pre><br />
Next we would like to get the users full name from our Spring service.  So in order to do that we need to tell the mockWebApplicationContext to return a mock of our service so we can unit test this in isolation.  We also need to tell our mock service that we expect a call to getFullName() and what to return from this call.  So modify our test method to resemble the following.<br />
<pre class="brush: java;">

public void testDoStartTag() throws Exception{
    String param = &quot;Jeremy&quot;;
    String fullName = &quot;Jeremy Anderson&quot;;
    String expectedOutput = &quot;Hello, &quot; + fullName;

    expect(mockWebApplicationContext.getBean(&quot;someService&quot;)).andReturn(mockService);
    expect(someService.getFullName(param)).andReturn(fullName);
    replayAllMocks();

    someCustomTag.setUserName(param);
    int tagReturnValue = someCustomTag.doStartTag();
    String output = ((MockHttpServletResponse) mockPageContext.getResponse()).getContentAsString();

    assertEquals(&quot;Tag should return 'SKIP_BODY'&quot;, TagSupport.SKIP_BODY, tagReturnValue);
    assertEquals(&quot;Output should be 'Hello, Jeremy Anderson'&quot;, expectedOutput, output);

    verifyAllMocks();
}</pre><br />
Now when you run this code, you&#8217;ll get an error from EasyMock complaining about methods that were expected to be called, but weren&#8217;t.  So lets fix that now.  Modify the doStartTagInternal() method to look like the following.<br />
<pre class="brush: java;">

protected int doStartTagInternal() throws Exception {
        // Get the Spring Context from the RequestContext
        WebApplicationContext context = getRequestContext().getWebApplicationContext();

        // Get the service we want to call from the Spring Context
        someService = (SomeServiceInterface) context.getBean(&quot;someService&quot;);

        String fullName = someService.getFullName(userName);
        pageContext.getOut().print(&quot;Hello, &quot; + fullName);

        return SKIP_BODY;
    }

</pre><br />
Congratulations, you&#8217;ve now implemented a JSP Custom Tag using &#8220;Test First&#8221;.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/codeadept.wordpress.com/26/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/codeadept.wordpress.com/26/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/codeadept.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/codeadept.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/codeadept.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/codeadept.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/codeadept.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/codeadept.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/codeadept.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/codeadept.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/codeadept.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/codeadept.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/codeadept.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/codeadept.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/codeadept.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/codeadept.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.code-adept.com&amp;blog=2511053&amp;post=26&amp;subd=codeadept&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.code-adept.com/2008/01/25/unit-testing-jsp-custom-tags/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/611c688725819bb23742a714a9c33dfe?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Jeremy</media:title>
		</media:content>
	</item>
	</channel>
</rss>
