<?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>Carbon Five Community &#187; Testing</title>
	<atom:link href="http://blog.carbonfive.com/category/testing/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.carbonfive.com</link>
	<description></description>
	<lastBuildDate>Fri, 27 Aug 2010 00:24:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Testing iPhone View Controllers</title>
		<link>http://blog.carbonfive.com/2010/03/testing/testing-view-controllers</link>
		<comments>http://blog.carbonfive.com/2010/03/testing/testing-view-controllers#comments</comments>
		<pubDate>Wed, 10 Mar 2010 18:01:08 +0000</pubDate>
		<dc:creator>jonah</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=864</guid>
		<description><![CDATA[I have been writing tests around my iPhone apps&#8217; view controllers in order to follow the same TDD practices we use in other environments. Writing tests first has changed the way I structure my code in a couple of ways which I think offer immediate and emergent benefits for my applications. Most of an iPhone [...]]]></description>
			<content:encoded><![CDATA[<p>I have been writing tests around my iPhone apps&#8217; view controllers in order to follow the same TDD practices we use in other environments. Writing tests first has changed the way I structure my code in a couple of ways which I think offer immediate and emergent benefits for my applications. Most of an iPhone application&#8217;s business logic is implemented in its view controllers. Testing those controllers is therefore a priority if I want to have a well tested application.<br />
Below are some examples of the sort of tests I have written for my view controllers using GTM, Hamcrest, and OCMock (our <a href="http://blog.carbonfive.com/2009/02/testing/iphone-unit-testing-toolkit">iPhone Unit Testing Toolkit</a>). Hopefully this can serve as a starting point for the tests you could be writing for your own projects.<br />
<span id="more-864"></span><br />
<strong>Testing Interface Builder Bindings</strong><br />
Broken nib bindings appear to be a common cause of application bugs during development. It is certainly easy enough to accidentally break or forget to create a binding while editing a nib file so let&#8217;s write some simple tests to assert that our actions and outlets are actually connected to objects in a nib file.<br />
These are really tests of the nib file itself. If the goal was to test the view controller&#8217;s use of these bound view objects I would replace the views with mock objects which could verify the controller&#8217;s behavior. (Erik Dörnenburg has provided a nice example of doing just that: <a href="http://erik.doernenburg.com/2008/07/testing-cocoa-controllers-with-ocmock/">Testing Cocoa Controllers with OCMock</a>)</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> testViewBinding <span style="color: #002200;">&#123;</span>
	TestableSimpleViewController <span style="color: #002200;">*</span>viewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestableSimpleViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;TestableSimpleViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>viewController loadView<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//It is not strictly necessary to call loadView for this test as we access the view property which will call loadView if view is nil</span>
	assertThat<span style="color: #002200;">&#40;</span>viewController.view, isNot<span style="color: #002200;">&#40;</span>nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
	assertThat<span style="color: #002200;">&#40;</span>viewController.button, isNot<span style="color: #002200;">&#40;</span>nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> testUIButtonActionBinding <span style="color: #002200;">&#123;</span>
	TestableSimpleViewController <span style="color: #002200;">*</span>viewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestableSimpleViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;TestableSimpleViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>viewController loadView<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//Here we must call loadView since we have not accessed the controller's view property to trigger view loading from the nib</span>
	assertThat<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>viewController.button actionsForTarget<span style="color: #002200;">:</span>viewController forControlEvent<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>, onlyContains<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;testAction&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Testing View Reloading</strong><br />
View controllers should be able to unload their views if the application receives a memory warning while the view is not visible and then reload the view when it is needed again. We can reproduce that same sequence of messages in a test. For a non-trivial controller we would add additional assertions to test that any other view dependent properties were correctly released and recreated as the view was unloaded and reloaded.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> testViewUnloading <span style="color: #002200;">&#123;</span>
	TestableSimpleViewController <span style="color: #002200;">*</span>viewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestableSimpleViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;TestableSimpleViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>viewController loadView<span style="color: #002200;">&#93;</span>;
	assertThat<span style="color: #002200;">&#40;</span>viewController.view, isNot<span style="color: #002200;">&#40;</span>nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>viewController didReceiveMemoryWarning<span style="color: #002200;">&#93;</span>;
	assertThat<span style="color: #002200;">&#40;</span>viewController.button, is<span style="color: #002200;">&#40;</span>nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">//Note that while viewController.view is nil here we cannot test that directly as accessing view will trigger a call to loadView. Instead we can only test that our outlets have been released as expected.</span>
	<span style="color: #002200;">&#91;</span>viewController loadView<span style="color: #002200;">&#93;</span>;
	assertThat<span style="color: #002200;">&#40;</span>viewController.view, isNot<span style="color: #002200;">&#40;</span>nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Testing Switching Between View Controllers</strong><br />
View controllers frequently have dependencies on other view controllers. If those dependencies are tightly coupled it becomes very difficult to test the behavior of a single view controller. Instead we can expose those dependencies and replace them with mock objects in order to isolate the controller we are currently testing. Here we have a simple table view controller which presents a detail view controller when a table cell is selected. By mocking both the navigation controller used to present that detail view and the detail view itself we can capture and verify the table view controller&#8217;s behavior.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> testSelectingACellPushesADetailView <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">id</span> detailViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>OCMockObject niceMockForClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>GenericDetailViewController class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	UINavigationController <span style="color: #002200;">*</span>navController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">id</span> navigationController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>OCMockObject partialMockForObject<span style="color: #002200;">:</span>navController<span style="color: #002200;">&#93;</span>;
&nbsp;
	TestableTableViewController <span style="color: #002200;">*</span>viewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestableTableViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;TestableTableViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	viewController.detailViewController <span style="color: #002200;">=</span> detailViewController;
	viewController.modelData <span style="color: #002200;">=</span> data;
&nbsp;
	<span style="color: #002200;">&#91;</span>navigationController pushViewController<span style="color: #002200;">:</span>viewController animated<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
	assertThat<span style="color: #002200;">&#40;</span>viewController.navigationController, isNot<span style="color: #002200;">&#40;</span>nilValue<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>viewController loadView<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>detailViewController expect<span style="color: #002200;">&#93;</span> setModelData<span style="color: #002200;">:</span>data<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//expect that the detail view will be given some data to display</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>navigationController expect<span style="color: #002200;">&#93;</span> pushViewController<span style="color: #002200;">:</span>detailViewController animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">//expect that the detail view will be pushed onto the nave controller</span>
	<span style="color: #002200;">&#91;</span>viewController tableView<span style="color: #002200;">:</span>viewController.tableView didSelectRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSIndexPath</span> indexPathForRow<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> inSection<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>detailViewController verify<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>navigationController verify<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>So what makes test-driven controllers different?</strong><br />
I certainly could have written these example controllers without any tests around them. However by writing tests I was encouraged to make a couple of changes to the way I structure my code.</p>
<ul>
<li>My controllers expose dependencies that might otherwise remain buried in their implementations. In the example above I exposed the detail view controller my controller used so that I could replace it with a mock object. That made it possible to test that the expected data was passed to the detail view but it will also make it easy to switch to a different detail view controller in the future.</li>
<li>By focusing on testing just one class at a time I am encouraged to create well defined interfaces and a clear set of responsibilities for each class. That in turn leads to more modular code which is easier to maintain and reuse.</li>
<li>I write bugs but if I write a test to reproduce them and then prove that they are fixed I shouldn&#8217;t ever see the same bug twice. It is much easier to catch a missing nib binding because a test failed than it is to see the app crash when displaying some view and have to track down that exc_bad_access error in the debugger.</li>
<li>Most importantly by writing tests I can have confidence that my code works as I expected, not only when I first write it but also every time I run my tests (which happens every time I build the app). With that feedback I can change one controller without fearing that I might have broken something somewhere else in the app.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/03/testing/testing-view-controllers/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript Testing Talk in Oakland</title>
		<link>http://blog.carbonfive.com/2010/03/software-design/javascript-testing-talk-in-oakland</link>
		<comments>http://blog.carbonfive.com/2010/03/software-design/javascript-testing-talk-in-oakland#comments</comments>
		<pubDate>Mon, 08 Mar 2010 17:40:13 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[Speaking]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=903</guid>
		<description><![CDATA[Next week at EBig Jonah and I are wrapping up our world tour of talking about Javascript testing. March 17th in Oakland: &#8221;Recent evolutions in Javascript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex Javascript, have confidence in the implementation, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ebig.org/index.cfm?fuseaction=Calendar.eventDetail&amp;eventID=158" target="_blank">Next week at EBig</a> Jonah and I are wrapping up our world tour of talking about Javascript testing. March 17th in Oakland: &#8221;Recent evolutions in Javascript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex Javascript, have confidence in the implementation, and push more of the logic from the server into the browser, reducing the load on the server.&#8221; The focus of the talk is walking through a suite of tests we build for a real-world example.</p>
<p>For those of you who caught it last week at the SDForum, here are the links people requested:</p>
<ul>
<li><a href="http://code.google.com/p/javascript-test-maven-plugin/">Maven plugin for Javascript Unit Testing with Rhino/ScrewUnit</a> (from C5)</li>
<li><a href="http://github.com/ndp/wizardize">The current code for the &#8220;wizardize&#8221; example we walked through</a>.</li>
<li><a href="http://github.com/relevance/blue-ridge">Blue Ridge (Rails integration tool)</a></li>
<li><a href="http://pivotallabs.com/users/nick/blog/articles/455-better-javascript-testing-through-screwunit">ScrewUnit</a></li>
<li><a href="http://www.slideshare.net/jeresig/understanding-javascript-testing">JResig&#8217;s Presentation on JS Testing</a></li>
<li><a href="http://dl.dropbox.com/u/1399422/Javascript%20Unit%20Testing%2C%20Yes%20We%20Can%20Slides.pdf">The slides we used</a></li>
</ul>
<p>To sign up for next Wednesday, go to the <a href="http://www.ebig.org/index.cfm?fuseaction=Calendar.eventDetail&amp;eventID=158">EBig site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/03/software-design/javascript-testing-talk-in-oakland/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test-Driven JavaScript with ScrewUnit and BlueRidge</title>
		<link>http://blog.carbonfive.com/2010/02/software-design/test-driven-javascript-with-screwunit-and-blueridge</link>
		<comments>http://blog.carbonfive.com/2010/02/software-design/test-driven-javascript-with-screwunit-and-blueridge#comments</comments>
		<pubDate>Fri, 26 Feb 2010 01:33:04 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Ruby (on Rails)]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=893</guid>
		<description><![CDATA[Jonah and I are taking our presentation about Javascript Testing on the road next Tuesday at 6:30 in Palo Alto, at the SDForum The teaser for it&#8230; Recent evolutions in JavaScript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex JavaScript, [...]]]></description>
			<content:encoded><![CDATA[<p>Jonah and I are taking our presentation about Javascript Testing on the road <a href="http://www.sdforum.org/index.cfm?fuseaction=Calendar.eventDetail&amp;eventID=13629&amp;pageId=471">next Tuesday at 6:30 in Palo Alto, at the SDForum</a></p>
<p>The teaser for it&#8230; Recent evolutions in JavaScript testing frameworks now allow creating test suites, test-driving development, and running tests on a continuous integration server. This allows us to support more complex JavaScript, have confidence in the implementation, and push more of the logic from the server into the browser, reducing the load on the server&#8230;</p>
<p>Hope to see you there.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/02/software-design/test-driven-javascript-with-screwunit-and-blueridge/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Screw.Unit JS Testing in Maven: javascript-test-maven-plugin</title>
		<link>http://blog.carbonfive.com/2010/02/testing/screw-unit-js-testing-in-maven-javascript-test-maven-plugin</link>
		<comments>http://blog.carbonfive.com/2010/02/testing/screw-unit-js-testing-in-maven-javascript-test-maven-plugin#comments</comments>
		<pubDate>Thu, 25 Feb 2010 03:29:31 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=878</guid>
		<description><![CDATA[I&#8217;ve written a maven plugin to integrate Screw.Unit javascript tests into a maven build. The project is inspired by the Blue Ridge testing framework for Rails, but it&#8217;s a bit more light-weight by design. To use the plugin you should start by downloading Screw.Unit and implement your tests according to their instructions. Once you have [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a <a href="http://code.google.com/p/javascript-test-maven-plugin/">maven plugin</a> to integrate <a href="http://github.com/nkallen/screw-unit">Screw.Unit</a> javascript tests into a maven build.  The project is inspired by the <a href="http://blog.thinkrelevance.com/2009/5/12/blue-ridge-1-0-javascript-unit-testing-for-rails-scandalous">Blue Ridge</a> testing framework for Rails, but it&#8217;s a bit more light-weight by design.<br />
<span id="more-878"></span><br />
To use the plugin you should start by downloading Screw.Unit and implement your tests according to their instructions.  Once you have some tests implemented in html files, add the plugin to your maven pom file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javascript-test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.carbonfive.javascript-test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javascript-test-maven-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0-beta1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>src/test/javascript/suite*.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/include<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/includes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You will probably also need to add Carbon Five&#8217;s public repository so that maven will know where to grab the plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>c5-public-repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Carbon Five Public Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://mvn.carbonfive.com/public<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The &lt;configuration&gt; section contains <a href="http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html">standard</a> maven &lt;include&gt; and &lt;exclude&gt; tags to allow you to specify any Screw.Unit test suite html files you would like to include in your build.</p>
<p>Now when maven executes its test phase, it will include your javascript tests as well. Any test that fails will halt the build. When the test phase is complete the target directory will contain a &#8216;screw-unit&#8217; directory that contains the test reports. There will be two files for every file included in the test execution The first is an XML file named TEST-dir1.dir2.dir3.filename.html.xml that contains the test results for that file in a reasonable approximation of JUnit&#8217;s xml format. The other file will be named dir1.dir2.dir3.filename.html and will contain a sort of snapshot of the test suite&#8217;s html at the time the tests completed (the javascript will be removed and the css will be baked in).</p>
<p>Like Blue Ridge, the javascript-test-maven-plugin relies on the excellent <a href="http://ejohn.org/blog/bringing-the-browser-to-the-server/">env.js</a> project and the less-excellent <a href="http://www.mozilla.org/rhino/">Rhino</a> project to emulate a browser in Java.   Unfortunately this combination creates the possibility of discrepancies between the browser and build results, and the probability of relatively slow test execution.  To keep the simulation as realistic as possible, javascript-test-maven-plugin recreates the test environment for each Screw.Unit html file, so the more tests you can cram into a single file the faster your builds will run.</p>
<p>There are a few directions for future work.  I&#8217;d like for the plugin to handle other browser-based javascript testing frameworks.  Screw.Unit is popular, but seems suboptimal in many respects.  The only limiting factor here is the plugin&#8217;s ability to interpret the results.  If env.js could run in javascript interpreters other than Rhino, I&#8217;d like to give the the plugin the capability to fork out to <a href="http://www.mozilla.org/js/spidermonkey/">spidermonkey</a> for better compatibility and performance.  Modifying the plugin would be relatively easy, but env.js appears to have some significant dependencies on Java.</p>
<p>Check it out if you have a need for this kind of thing and let us know if you have any problems or suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2010/02/testing/screw-unit-js-testing-in-maven-javascript-test-maven-plugin/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unit testing JavaScript with Blue Ridge</title>
		<link>http://blog.carbonfive.com/2009/11/testing/unit-testing-javascript-with-blue-ridge</link>
		<comments>http://blog.carbonfive.com/2009/11/testing/unit-testing-javascript-with-blue-ridge#comments</comments>
		<pubDate>Sat, 14 Nov 2009 04:19:19 +0000</pubDate>
		<dc:creator>jonah</dc:creator>
				<category><![CDATA[JavaScript / AJAX]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=696</guid>
		<description><![CDATA[My current rails project is using Blue Ridge and the tool set it bundles together (Rhino, env.js, Screw.Unit, and Smoke) to write test driven JavaScript. The ability to write tests of my JavaScript, in JavaScript, as efficiently as I can test other languages has forced me to rethink how I structure my work and I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>My current rails project is using <a href="http://github.com/relevance/blue-ridge">Blue Ridge</a> and the tool set it bundles together (<a href="http://www.mozilla.org/rhino/">Rhino</a>, <a href="http://github.com/thatcher/env-js">env.js</a>, <a href="http://github.com/nkallen/screw-unit">Screw.Unit</a>, and <a href="http://github.com/andykent/smoke">Smoke</a>) to write test driven JavaScript. The ability to write tests of my JavaScript, in JavaScript, as efficiently as I can test other languages has forced me to rethink how I structure my work and I&#8217;m writing better code because of it.</p>
<p>There are a couple of different options for JS testing available at this point but Blue Ridge&#8217;s plugin is the first I have seen which made writing test driven code as easy as I have come to expect while still providing all the functionality I need. In particular;</p>
<ul>
<li>Running my tests in a browser is fast. Refreshing fixture pages to rerun tests is dramatically more efficient than waiting for selenium tests to cycle.</li>
<li>I can debug my tests as they run against their fixtures pages and inspect or manipulate the DOM at any point.</li>
<li>I have mock and fake objects available to test behavior and stub out dependencies.</li>
<li>My JS tests run in continuous integration, just like all the other tests.</li>
</ul>
<p>By writing fixtures pages and tests first I am immediately using all of my code in two locations and running it against slightly different DOMs. That has pushed me to encapsulate most of my code as jQuery plugins and think carefully about how much I depend on the page&#8217;s structure. I end up with terse selectors instead of using whatever attributes are readily available and a clear configuration point for my page&#8217;s behavior as I chain together plugin calls in $(document).ready().</p>
<p>We&#8217;d like to see what other developers have done and share what we have learned so far so Carbon Five is hosting an open discussion of JavaScript testing in our office this week.</p>
<p>Topic: Javascript Testing<br />
When: Tuesday Nov 17<br />
6pm: arrive/network<br />
6:30 &#8211; 8 presentation and discussion<br />
Where: Carbon Five office: <a href="http://maps.google.com/maps?f=q&#038;hl=en&#038;geocode=&#038;q=171+2nd+St+%23+4,+San+Francisco,+CA%E2%80%8E&#038;sll=36.738884,-118.432617&#038;sspn=13.579326,18.632813&#038;ie=UTF8&#038;hq=&#038;hnear=171+2nd+St,+San+Francisco,+California+94105&#038;z=14&#038;iwloc=r0">171 2nd Street, 4th Floor</a></p>
<p>Please <a href="https://spreadsheets.google.com/viewform?formkey=dDg5dl9zbHlhZXNpdzBxdVIzblhjN3c6MA">RSVP</a> if you would like to attend.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/11/testing/unit-testing-javascript-with-blue-ridge/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C5 Test Support new addition: FunctionalTestRunner</title>
		<link>http://blog.carbonfive.com/2009/10/testing/c5-test-support-new-addition-functionaltestrunner</link>
		<comments>http://blog.carbonfive.com/2009/10/testing/c5-test-support-new-addition-functionaltestrunner#comments</comments>
		<pubDate>Thu, 29 Oct 2009 01:54:03 +0000</pubDate>
		<dc:creator>christian</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=702</guid>
		<description><![CDATA[We&#8217;re always looking for new ways to test our applications and we&#8217;ve been trying a few new things on our projects. One of the recent additions is a JUnit test runner designed to help make writing and running functional tests easier. In Javaland, we use Selenium and/or HtmlUnit for our functional tests. These are the [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re always looking for new ways to test our applications and we&#8217;ve been trying a few new things on our projects.  One of the recent additions is a JUnit test runner designed to help make writing and running functional tests easier.  In Javaland, we use Selenium and/or HtmlUnit for our functional tests.  These are the tests that run against a deployed application over the wire using a real or simulated browser.  Most of our functional tests work the application in the same way a real user would, testing sequences of realistic activity and often touching a number of pages.  Since our functional tests use either a real browser or a simulated one, Javascript is executed and assertions made on the results.  This gives us greater confidence that our app is really working, end to end.</p>
<p>Here&#8217;s the high-level flow that the functional test runner provides:</p>
<ol>
<li>Load fixture data from a DBUnit dataset.</li>
<li>Download and install the application server (if necessary).</li>
<li>Start the application server (using Cargo).</li>
<li>Deploy the application, waiting until it&#8217;s completely started.</li>
<li>Run one or more functional tests (using your preferred testing framework &#8211; Selenium, HTMLUnit, etc)&#8230;<br />
If a test dirties the database in a manner that must be reset, the test class can be marked with the @DirtiesDatabase annotation.  This will reload the database fixture and optionally restart the application.</li>
<li>Shutdown the application server.</li>
</ol>
<p><span id="more-702"></span><br />
It&#8217;s easy to use&#8230;  First add the maven dependency:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>c5-public-repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Carbon Five Public Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://mvn.carbonfive.com/public<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  ...
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.carbonfive.test-support<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test-support<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.9.2-m1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  ...</pre></div></div>

<p>And then write your first functional test:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>FunctionalTestRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionalTestRunnerTest
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// This field is automatically injected by the test runner and references</span>
    <span style="color: #666666; font-style: italic;">// the functional test properties (see below).</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Properties</span> properties<span style="color: #339933;">;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> propertiesShouldBeSetByRunner<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        assertThat<span style="color: #009900;">&#40;</span>properties, not<span style="color: #009900;">&#40;</span>nullValue<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>
        assertThat<span style="color: #009900;">&#40;</span>properties.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, greaterThan<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</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;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> applicationShouldBeDeployed<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; font-weight: bold;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Test using your preferred framework... just using URL as an example.</span>
        <span style="color: #003399; font-weight: bold;">URL</span> root = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">URL</span><span style="color: #009900;">&#40;</span>format<span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;http://localhost:%s/&quot;</span>, properties.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;appserver.port&quot;</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: #003399; font-weight: bold;">String</span> content = IOUtils.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>root.<span style="color: #006633;">openStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>content, containsString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;Orange&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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Lastly, create the  configuration file that goes along with it.  By default, it&#8217;s called functional-test.properties and lives at the root of the classpath (src/test/resources/ for maven users).  Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="properties" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Application server</span>
<span style="color: #000080; font-weight:bold;">appserver.container</span>         <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> tomcat6x</span>
<span style="color: #000080; font-weight:bold;">appserver.installer</span>         <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> http://www.apache.org/dist/tomcat/tomcat-<span style="">6</span>/v6.0.20/bin/apache-tomcat-6.0.20.zip</span>
<span style="color: #000080; font-weight:bold;">appserver.port</span>              <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> 8080</span>
<span style="color: #000080; font-weight:bold;">appserver.logging</span>           <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> false</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Application configuration</span>
<span style="color: #000080; font-weight:bold;">app.war</span>                     <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> ./gearlist-webapp/target/gearlist-webapp-<span style="">1.0</span>-SNAPSHOT.war</span>
<span style="color: #000080; font-weight:bold;">app.context</span>                 <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> ROOT</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Database fixtures</span>
<span style="color: #000080; font-weight:bold;">fixture.restart_application</span> <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> false</span>
<span style="color: #000080; font-weight:bold;">fixture.file</span>                <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> classpath:/database-fixture.xml</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;"># Application configuration (not used by the functional test runner)</span>
<span style="color: #000080; font-weight:bold;">db.driver</span>                   <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> com.mysql.jdbc.Driver</span>
<span style="color: #000080; font-weight:bold;">db.host</span>                     <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> localhost</span>
<span style="color: #000080; font-weight:bold;">db.name</span>                     <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> gearlist_func_test</span>
<span style="color: #000080; font-weight:bold;">db.url</span>                      <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> jdbc:mysql://$<span style="">&#123;</span>db.host<span style="">&#125;</span>/$<span style="">&#123;</span>db.name<span style="">&#125;</span></span>
<span style="color: #000080; font-weight:bold;">db.username</span>                 <span style="color: #000000;">=</span><span style="color: #008000; font-weight:bold;"> root</span>
<span style="color: #000080; font-weight:bold;">db.password</span>                 <span style="color: #000000;">=</span></pre></div></div>

<p>Property values can reference other properties (e.g. db.url) and they&#8217;re all set as system properties so it&#8217;s easy to override your application properties for functional testing.</p>
<p>Both the database fixture loading and the application server steps can be customized by plugging in a new <a href="http://code.google.com/p/c5-test-support/source/browse/trunk/src/main/java/com/carbonfive/test/functional/DatabaseFixtureLoader.java">DatabaseFixtureLoader</a> or <a href="http://code.google.com/p/c5-test-support/source/browse/trunk/src/main/java/com/carbonfive/test/functional/ApplicationServerManager.java">ApplicationServerManager</a>, respectively.</p>
<p>Since it&#8217;s a JUnit extension, functional tests run exactly the same whether they&#8217;re triggered from the IDE or the command line.  This approach is a response to how clunky it felt using Maven to launch the application server before running the tests.  Also, with only one master orchestrator, it&#8217;s easier to handle redeploying the application if necessary.</p>
<p>Want to see a real example?  I&#8217;ve written a sample application called &#8220;gearlist&#8221; that&#8217;s available via <a href="http://svn.carbonfive.com/public/christian/gearlist/trunk">public svn</a> (press cancel if asked for authentication credentials).  It includes several HtmlUnit and Selenium functional tests, as well as other types of tests using some of the other test utils I&#8217;ve written.  Look for the readme for details.</p>
<p><a href="http://code.google.com/p/c5-test-support/">Carbon Five Test Support</a> is hosted on google code, where you can read what documentation there is and peruse the source.</p>
<p>There are a few things to work on, including better exception handling during initialization and some flexibility around the Cargo configuration, however, it&#8217;s pretty stable and there&#8217;s no reason not to use it now.  Give it it try and give me your feedback.  The plan is to tighten it up and write some documentation for the 0.9.2 release.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/10/testing/c5-test-support-new-addition-functionaltestrunner/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Web Application Testing @ SDForum October 6th</title>
		<link>http://blog.carbonfive.com/2009/09/testing/web-application-testing-sdforum-october-6th</link>
		<comments>http://blog.carbonfive.com/2009/09/testing/web-application-testing-sdforum-october-6th#comments</comments>
		<pubDate>Sat, 26 Sep 2009 18:20:07 +0000</pubDate>
		<dc:creator>christian</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=675</guid>
		<description><![CDATA[Updated: Added links to presentation and source code. I&#8217;ll be down in Palo Alto speaking about Automated Web Application Testing Tuesday October 6th. If you&#8217;re interest in getting a peek at the typical Carbon Five Java web architecture along with a variety of strategies and techniques for testing, c&#8217;mon down and join us. The session [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Updated: Added links to presentation and source code.</strong></p>
<p>I&#8217;ll be down in Palo Alto speaking about Automated Web Application Testing Tuesday October 6th.  If you&#8217;re interest in getting a peek at the typical Carbon Five Java web architecture along with a variety of strategies and techniques for testing, c&#8217;mon down and join us.  The session will be primarily code-driven.  I&#8217;ll implement a few features along with unit, integration and functional tests and show off some of the techniques and custom tools that help keep things simple and easy during development.  Some of the topics include:</p>
<ul>
<li>Brief overview of our typical web architecture and tools stack</li>
<li>Differences between Unit, Integration and Functional Tests</li>
<li>Dealing with the database (schema and fixtures)</li>
<li>Where does test coverage pay off the most?</li>
<li>In-browser and out-of-browser functional testing</li>
<li>Carbon Five best practices, custom tools and techniques</li>
<li>And more&#8230;</li>
</ul>
<p>You can find the gritty details on the <a href="http://www.sdforum.org/index.cfm?fuseaction=Calendar.eventDetail&#038;eventID=13525">SDForum site</a>.</p>
<p>Doors at 6:30 and the show begins at 7:00.  Hope to see you there!</p>
<h3>Here are the artifacts from the presentation:</h3>
<ul>
<li><a href="http://svn.carbonfive.com/public/christian/gearlist/branches/sdforum/docs/Web%20Application%20Testing.pdf">Presentation (.pdf)</a></li>
<li><a href="http://svn.carbonfive.com/public/christian/gearlist/branches/sdforum/">Source Code (svn)</a></li>
<li><a href="http://svn.carbonfive.com/public/christian/gearlist/branches/sdforum/docs/readme.txt">Build and Run Instructions (readme.txt)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/09/testing/web-application-testing-sdforum-october-6th/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Custom Xcode Templates for iPhone Development</title>
		<link>http://blog.carbonfive.com/2009/05/testing/custom-xcode-template-for-iphone-development</link>
		<comments>http://blog.carbonfive.com/2009/05/testing/custom-xcode-template-for-iphone-development#comments</comments>
		<pubDate>Wed, 20 May 2009 00:25:16 +0000</pubDate>
		<dc:creator>jonah</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=534</guid>
		<description><![CDATA[Previously we discussed building an iPhone Unit Testing Toolkit. I wanted to reuse that work without having to repeat those configuration steps every time I started a new Xcode project. Xcode provides a number of templates or starting iPhone applications so I made my own versions which include all of the unit testing tools we rely [...]]]></description>
			<content:encoded><![CDATA[<p>Previously we discussed building an <a title="iPhone Unit Testing Toolkit" rel="bookmark" href="http://blog.carbonfive.com/2009/02/testing/iphone-unit-testing-toolkit" target="_self">iPhone Unit Testing Toolkit</a>. I wanted to reuse that work without having to repeat those configuration steps every time I started a new Xcode project. Xcode provides a number of templates or starting iPhone applications so I made my own versions which include all of the unit testing tools we rely on.</p>
<p><span id="more-534"></span></p>
<p>To reuse my templates checkout a copy from <a href="http://svn.carbonfive.com/public/jonah/Xcode-templates/Xcode/">http://svn.carbonfive.com/public/jonah/Xcode-templates/Xcode/</a> to &#8220;/Library/Application Support/Developer/Shared/Xcode/&#8221; and restart Xcode.</p>
<h2>Installing the template files:</h2>
<p>I installed Apple&#8217;s developer tools using the default path so my copy of Apple&#8217;s Xcode templates is located in &#8220;/Developer/Library/Xcode/File Templates&#8221; and &#8220;/Developer/Library/Xcode/Project Templates&#8221;. Installing the iPhone SDK added additional templates in &#8220;/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/&#8221;. Those were useful examples to base my own templates on but I needed to save my new templates separate from Apple&#8217;s default configuration files.</p>
<p>The <a title="Xcode Release Notes" href="http://developer.apple.com/releasenotes/developertools/RN-Xcode/index.html" target="_blank">Xcode release notes</a> state that custom support files including project and file templates belong under &#8220;/Library/Application Support/Developer/&#8221; or &#8220;~/Library/Application Support/Developer/&#8221;. I didn&#8217;t need to restrict the visibility of these templates to a specific version of Xcode and I wanted them to be available to any user account so I put them in &#8220;/Library/Application Support/Developer/Shared/Xcode/Project Templates&#8221; and &#8220;/Library/Application Support/Developer/Shared/Xcode/File Templates&#8221;.</p>
<h2>Building a template project:</h2>
<div>
<p>I started by adding a &#8220;test&#8221; build target to Apple&#8217;s default &#8220;View-Based Application&#8221; template. First I copied &#8220;/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Application/View-Based Application&#8221; to &#8220;/Library/Application Support/Developer/Shared/Xcode/Project Templates/Carbon Five/Testable View-Based Application&#8221;. Then I opened &#8220;Testable View-Based Application/___PROJECTNAME___.xcodeproj&#8221; in Xcode and changed the customized the project structure by adding a &#8220;Unit Tests&#8221; target, the hamcrest and OCMock frameworks, GTM classes, an example test class, and a couple of groups in the overview list.</p>
<div>
<p><a href="http://blog.carbonfive.com/wp-content/uploads/2009/03/customized_project.png"><img class="size-medium wp-image-543 alignnone" title="customized_project" src="http://blog.carbonfive.com/wp-content/uploads/2009/03/customized_project.png" alt="Custom project settings" width="300" height="275" /></a></div>
<div>While setting up the project and testing its configuration Xcode created a bunch of files I did not want as part of my template so when I finished with the project I deleted both the build products directory and the user specific settings files inside the &#8221;___PROJECTNAME___.xcodeproj&#8221; package. I also editted the template description in &#8220;TemplateInfo.plist&#8221;.</div>
<div>I restarted Xcode and could start a new project using the template by selecting it from the User Templates section of the New Project dialog.</div>
<p><a href="http://blog.carbonfive.com/wp-content/uploads/2009/03/selecting_user_template.png"><img class="alignnone size-medium wp-image-545" title="selecting_user_template" src="http://blog.carbonfive.com/wp-content/uploads/2009/03/selecting_user_template.png" alt="" width="300" height="230" /></a><a href="http://blog.carbonfive.com/wp-content/uploads/2009/03/project_from_template.png"><img class="alignnone size-medium wp-image-546" title="project_from_template" src="http://blog.carbonfive.com/wp-content/uploads/2009/03/project_from_template.png" alt="" width="300" height="253" /></a></p>
<h2>Creating a file template:</h2>
<p>Creating a project from my template was handy but I wanted a faster way to build individual test classes as well so I wrote a file template.</p>
<p>First I copied &#8220;/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/File Templates/Cocoa Touch Classes/NSObject subclass.pbfiletemplate&#8221; to &#8220;/Library/Application Support/Developer/Shared/Xcode/File Templates/Carbon Five/GTM test template.pbfiletemplate&#8221;. Then in the class.m file I added hamcrest and OCMock imports and an example test method. This class did not require a separate header file so I deleted class.h.</p>
<p><a href="http://blog.carbonfive.com/wp-content/uploads/2009/03/class_m.png"><img class="alignnone size-medium wp-image-553" title="class_m" src="http://blog.carbonfive.com/wp-content/uploads/2009/03/class_m.png" alt="" width="300" height="158" /></a></p>
<p>In TemplateInfo.plist I updated the template&#8217;s description and since I removed the header file I removed the CounterpartTemplateFile line.</p>
<p><a href="http://blog.carbonfive.com/wp-content/uploads/2009/03/templateinfo_plist.png"><img class="alignnone size-medium wp-image-549" title="templateinfo_plist" src="http://blog.carbonfive.com/wp-content/uploads/2009/03/templateinfo_plist.png" alt="" width="300" height="163" /></a></p>
<p>After restart Xcode again I could create test classes based on the template using the &#8220;New File&#8230;&#8221; menu.</p>
<p><a href="http://blog.carbonfive.com/wp-content/uploads/2009/03/test_from_template.png"></a><a href="http://blog.carbonfive.com/wp-content/uploads/2009/03/selecting_file_template.png"><img class="alignnone size-medium wp-image-555" title="selecting_file_template" src="http://blog.carbonfive.com/wp-content/uploads/2009/03/selecting_file_template.png" alt="" width="300" height="230" /></a><img class="alignnone size-medium wp-image-550" title="test_from_template" src="http://blog.carbonfive.com/wp-content/uploads/2009/03/test_from_template.png" alt="" width="317" height="200" /></p>
<p>A couple dozen unit tests later I&#8217;m happy not to be repeating even this minimal amount of configuration each time I start a new test.</p>
<p>If you&#8217;re developing in Xcode I&#8217;d like to hear what sort of project setup you use. Are there libraries or frameworks you always include? Class files you have, or wish you had, created templates for? Do you use Apple&#8217;s default resource groups or have you established your own conventions for organizing your projects?</p></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/05/testing/custom-xcode-template-for-iphone-development/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>assert_changes and assert_no_changes in Ruby</title>
		<link>http://blog.carbonfive.com/2009/04/testing/assert_changes-and-assert_no_changes-in-ruby</link>
		<comments>http://blog.carbonfive.com/2009/04/testing/assert_changes-and-assert_no_changes-in-ruby#comments</comments>
		<pubDate>Sat, 04 Apr 2009 00:26:51 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Ruby (on Rails)]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Agile]]></category>
		<category><![CDATA[fixtures]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=567</guid>
		<description><![CDATA[Update: This code and documentation is now available on github: http://github.com/ndp/assert_changes/tree/master The Problem On our work on gobalto.com, we spend time to have good fixture data for our tests&#8211; data that can represent all the important application states that our tests require. As a result, our tests are very dependent on the data. It&#8217;s important [...]]]></description>
			<content:encoded><![CDATA[<h3><span style="color: #000080;">Update:</span><span style="color: #000080;"> This code and documentation is now available on github:<a href="http://github.com/ndp/assert_changes/tree/master" target="_blank"> http://github.com/ndp/assert_changes/tree/master</a></span></h3>
<h2>The Problem</h2>
<p>On our work on <a href="http://www.gobalto.com">gobalto.com</a>, we spend time to have good fixture data for our tests&#8211; data that can represent all the important application states that our tests require. As a result, our tests are very dependent on the data. It&#8217;s important that someone doesn&#8217;t inadvertantly change it in subtle ways. This has led us to write not only asserts at the end of tests, but pre-conditions as well. For example,</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    ...
    <span style="color:#9900CC;">inotech</span> = companies<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:inotech</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert inotech.<span style="color:#9900CC;">services</span>.<span style="color:#9900CC;">public</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert inotech.<span style="color:#9900CC;">services</span>.<span style="color:#9900CC;">public</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:b</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert inotech.<span style="color:#9900CC;">services</span>.<span style="color:#9900CC;">public</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:c</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    post <span style="color:#ff3333; font-weight:bold;">:edit_services_dialog</span>, <span style="color:#ff3333; font-weight:bold;">:id</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;inotech.<span style="color:#9900CC;">id</span>,
                                          <span style="color:#ff3333; font-weight:bold;">:service_category_id</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">id</span>
    inotech.<span style="color:#9900CC;">reload</span>
&nbsp;
    assert inotech.<span style="color:#9900CC;">services</span>.<span style="color:#9900CC;">public</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert !inotech.<span style="color:#9900CC;">services</span>.<span style="color:#9900CC;">public</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:b</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert !inotech.<span style="color:#9900CC;">services</span>.<span style="color:#9900CC;">public</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:c</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Although the pre-conditions were introduced to guard against accidentally changing fixture data (or just to figure out what&#8217;s going on), we don&#8217;t necessarily delete them. They provide stronger tests. And in some ways, leaving in pre-conditions make the code more readable by providing documentation of your assumptions to the readers. The code above would be hard to follow without the clarifying pre-conditions. What do we expect to change and what stays the same?</p>
<p>Unfortunately all these asserts make the tests twice as long. And there&#8217;s a subtle readability problem: there&#8217;s no relationship between the corresponding pre- and post-conditions. In the example above, you have to scan carefully to see that the three assertions are repeated, but negated (b and c only). The reader must mentally put pieces together. And it&#8217;s not DRY. Using local variables doesn&#8217;t help much.</p>
<h2>The Solution</h2>
<p>I was inspired by a nice little test helper called assert_difference. It takes a string to evaluate and a block to execute. It&#8217;s useful for checking on state changes&#8211; especially database changes&#8211; during a test:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">assert_difference<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;color: blue;">'Company.count'</span>, <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    Company.<span style="color:#9900CC;">delete_one</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>(Without this method, we rely on a count of all database records, or merely look for specific ones. The former approach leads to brittle tests, and the latter to incomplete assertions.)</p>
<p>A limitation of assert_difference is that it only deals with integers. What if it were generalized? Here goes:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    i = <span style="color:#0000FF; font-weight:bold;">true</span>
    assert_changes <span style="color:#996600;color: blue;">'i'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">do</span>   <span style="color:#008000; font-style:italic;"># read as: i changes to false</span>
      i = <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><span id="more-567"></span>The string passed to assert changes is evaluated in the block context, both before and after the block is run. So this block asserts that i becomes false (and by deduction, starts out as true). It executes asserts on both ends, just like we want.</p>
<p>Of course sometimes you want to be explicit about a state change, so you can specify both the starting and ending values using an array:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    o.<span style="color:#9900CC;">answer</span> = <span style="color:#996600;color: blue;">'yes'</span>
    assert_changes <span style="color:#996600;color: blue;">'o.answer'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;color: blue;">'yes'</span>,<span style="color:#996600;color: blue;">'no'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      o.<span style="color:#9900CC;">answer</span> = <span style="color:#996600;color: blue;">'no'</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>To handle the original example, you can pass multiple pre/post conditions of arbitrary complexity and they are all evaluated before and after the block is executed:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">      assert_changes <span style="color:#996600;color: blue;">'post(:a).status'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:preview</span>, <span style="color:#ff3333; font-weight:bold;">:published</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
                            <span style="color:#996600;color: blue;">'comment(:c).status'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:preview</span>, <span style="color:#ff3333; font-weight:bold;">:deleted</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        ...
      <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Finally, I added support for a :no_change symbol. Now I can re-write my original problem in a clearer form:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    assert_changes
        <span style="color:#996600;color: blue;">'inotech.services.public.include?(categories(:a))'</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:no_change</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
            <span style="color:#996600;color: blue;">'inotech.services.public.include?(categories(:b))'</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;false,
            <span style="color:#996600;color: blue;">'inotech.services.public.include?(categories(:c))'</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;false <span style="color:#9966CC; font-weight:bold;">do</span>
      post <span style="color:#ff3333; font-weight:bold;">:edit_services_dialog</span>, <span style="color:#ff3333; font-weight:bold;">:id</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;inotech.<span style="color:#9900CC;">id</span>, <span style="color:#ff3333; font-weight:bold;">:service_category_id</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;categories<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:a</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">id</span>
      inotech.<span style="color:#9900CC;">reload</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>For completeness, I added assert_no_changes, with slightly extended parameter possibilities:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    i,j = <span style="color:#996600;color: blue;">'hello'</span>,<span style="color:#996600;color: blue;">'hi'</span>
    assert_no_changes <span style="color:#996600;color: blue;">'i'</span> <span style="color:#9966CC; font-weight:bold;">do</span> ...  <span style="color:#008000; font-style:italic;"># i (before) == i (after)</span>
    assert_no_changes <span style="color:#996600;color: blue;">'i'</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;color: blue;">'hello'</span> <span style="color:#9966CC; font-weight:bold;">do</span> ... <span style="color:#008000; font-style:italic;"># i == 'hello' before and after</span>
    assert_no_changes <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;color: blue;">'i'</span>,<span style="color:#996600;color: blue;">'j'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span> ... <span style="color:#008000; font-style:italic;"># neither i or j change</span>
    assert_no_changes <span style="color:#996600;color: blue;">'i'</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;color: blue;">'hello'</span>,<span style="color:#996600;color: blue;">'j'</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;color: blue;">'hi'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#008000; font-style:italic;"># or be explicit</span></pre></div></div>

<h3><span style="color: #000080;">Update:</span><span style="color: #000080;"> This code and documentation is now available on github:<a href="http://github.com/ndp/assert_changes/tree/master" target="_blank"> http://github.com/ndp/assert_changes/tree/master</a></span></h3>
<p>Thoughts? Comments?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/04/testing/assert_changes-and-assert_no_changes-in-ruby/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Constraints for OCMock</title>
		<link>http://blog.carbonfive.com/2009/02/testing/custom-constraints-for-ocmock</link>
		<comments>http://blog.carbonfive.com/2009/02/testing/custom-constraints-for-ocmock#comments</comments>
		<pubDate>Tue, 17 Feb 2009 20:28:43 +0000</pubDate>
		<dc:creator>alon</dc:creator>
				<category><![CDATA[Rescue Mission]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=287</guid>
		<description><![CDATA[In my last post on unit testing iPhone development I introduced a couple helpful tools including OCMock, a mock objects implementation for Objective-C. I recently came across a scenario where I needed to make an assertion on an argument passed to an expected method invocation but OCMock did not provide the constraint I needed. I [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://blog.carbonfive.com/2009/02/testing/iphone-unit-testing-toolkit">last post on unit testing iPhone development</a> I introduced a couple helpful tools including <a href="http://www.mulle-kybernetik.com/software/OCMock/">OCMock</a>, a mock objects implementation for Objective-C.</p>
<p>I recently came across a scenario where I needed to make an assertion on an argument passed to an expected method invocation but OCMock did not provide the constraint I needed. I was testing a FileDetailsViewController that pushes a new FileContentsViewController on to the view stack when a button is pressed. I mocked the UINavigationController so that I could add an assertion that <code>pushViewController:animated:</code> is called with the right arguments.</p>
<p>The meat of my test is:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> FileDetailsViewControllerTest <span style="color: #002200;">:</span> SenTestCase <span style="color: #002200;">&#123;</span><span style="color: #002200;">&#125;</span>
    FileDetailsViewController <span style="color: #002200;">*</span>controller;
    <span style="color: #a61390;">id</span> navigationController;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> FileDetailsViewControllerTest
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setUp <span style="color: #002200;">&#123;</span>
    controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FileDetailsViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;FileDetailsView&quot;</span> 
                                                             bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    navigationController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>OCMockObject mockForClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UINavigationController class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    controller.navigationController <span style="color: #002200;">=</span> navigationController;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> testShowContentLoadsView <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>navigationController expect<span style="color: #002200;">&#93;</span> 
         pushViewController<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>OCMConstraint isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>FileContentsViewController class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>
                   animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>controller showContentsButtonWasPressed<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> tearDown <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>navigationController verify<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Note that in this example, I some fanciness is required to assign <code>controller.navigationController = navigationController</code> since it is a readonly property in the UIViewController API. I&#8217;ll explain what I did in a later post.</p>
<p>The OCMock feature I was missing is the <code>OCMock.isKindOfClass</code> constraint. So I wrote my own.</p>
<p>Following is the header file that declares the isKindOfClass assertion as a static method in an Objective-C category for OCMock. Categories are kind of like Ruby mixins for Objective-C. You can add static or instance methods to an existing class definition. With this approach I can follow the OCMock pattern of providing static factory methods on the OCMock class for the range of constraints available.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//File: OCMockConstraint+Extensions.h</span>
<span style="color: #6e371a;">#import &lt;OCMock/OCMConstraint.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> OCMConstraint <span style="color: #002200;">&#40;</span>Extensions<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Following is the implementation, both of my new constraint and the factory method to create it.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//File: OCMockConstraint+Extensions.m</span>
<span style="color: #6e371a;">#import &quot;OCMockConstraint+Extensions.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> OCMKindOfClassConstraint <span style="color: #002200;">:</span> OCMConstraint
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@public</span>
    <span style="color: #a61390;">id</span> testClass;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> OCMKindOfClassConstraint
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>evaluate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>value isKindOfClass<span style="color: #002200;">:</span>testClass<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Static factory method</span>
<span style="color: #a61390;">@implementation</span> OCMConstraint <span style="color: #002200;">&#40;</span>Extensions<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>isKindOfClass<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>value <span style="color: #002200;">&#123;</span>
	OCMKindOfClassConstraint <span style="color: #002200;">*</span>constraint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>OCMKindOfClassConstraint constraint<span style="color: #002200;">&#93;</span>;
	constraint<span style="color: #002200;">-</span>&gt;testClass <span style="color: #002200;">=</span> value;
	<span style="color: #a61390;">return</span> constraint;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>This implementation shows both how to write a custom constraint for OCMock and a use of Objective-C categories to provide a nice API for using it.</p>
<p>In many cases, you&#8217;ll want to write a custom constraint that is specific to your test scenario and not appropriate to be shared globally in this manner. In this case you&#8217;ll probably follow a similar strategy but implemented in your test class instead of mixed in to OCMockConstraint.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/02/testing/custom-constraints-for-ocmock/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>iPhone Unit Testing Toolkit</title>
		<link>http://blog.carbonfive.com/2009/02/testing/iphone-unit-testing-toolkit</link>
		<comments>http://blog.carbonfive.com/2009/02/testing/iphone-unit-testing-toolkit#comments</comments>
		<pubDate>Fri, 13 Feb 2009 21:36:20 +0000</pubDate>
		<dc:creator>alon</dc:creator>
				<category><![CDATA[Rescue Mission]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://blog.carbonfive.com/?p=267</guid>
		<description><![CDATA[As we get in to developing iPhone/Touch applications at Carbon Five, we obviously want to bring over all our best agile practices including test driven development (TDD). I&#8217;ve been bootstrapping with the beta version of the Pragmatic iPhone SDK Development book but was disappointed to find no discussion of unit testing for the iPhone. So [...]]]></description>
			<content:encoded><![CDATA[<p>As we get in to developing iPhone/Touch applications at Carbon Five, we obviously want to bring over all our best agile practices including test driven development (TDD).</p>
<p>I&#8217;ve been bootstrapping with the beta version of the <a href="http://www.pragprog.com/titles/amiphd/iphone-sdk-development">Pragmatic iPhone SDK Development</a> book but was disappointed to find no discussion of unit testing for the iPhone. So I&#8217;ve been piecing it together as I go.</p>
<h2>GTM</h2>
<p>Unit testing in Objective-C is provided by the SenTestingKit framework that installs with XCode in /Developer/Library/Frameworks/. However it is not compatible with the iPhone SDK. The Google Toolbox for Mac provides <a href="http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting">an implementation of SenTestCase for the iPhone</a> that I am using instead. The instructions provided on that page worked for me with one exception that I included as a comment on that page:</p>
<blockquote><p>
I followed the instructions above but was unable to see any messages in the console. I did not see the behavior described above as:</p>
<p>Your target should now build cleanly, and if you check the build log you should see something like: &#8220;Executed 0 tests, with 0 failures (0 unexpected) in 0.001 (0.001) seconds&#8221; at the end.</p>
<p>It turned out that I needed to set the base SDK in the inspector for my target to an iPhone simulator even though the Overview showed I was building for simulator debugging.
</p></blockquote>
<p>I created a &#8216;Unit Tests&#8217; target per the GTM directions to run my tests. Now I am happily running my tests with command-B. One point that could be a problem when getting into continuous integration is that my Unit Tests target reports &#8216;Build succeeded (1 error)&#8217; when a unit test fails instead of having the build actually fail.</p>
<p>I have not gotten into the &#8220;Advanced Stuff&#8221; described on that page.</p>
<h2>Hamcrest</h2>
<p>The SenTestingKit assertion methods are pretty lame. They&#8217;re verbose and require that you provide a string message for your expectation (e.g. STAssertEqualStrings(a1, a2, description, &#8230;)). We&#8217;re using and like <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> on many of our Java projects. I was psyched to see there is <a href="http://code.google.com/p/hamcrest/wiki/TutorialObjectiveC2">an Objective-C implementation</a>.</p>
<p>There is no download available for Hamcrest Objective-C so I <a href="http://code.google.com/p/hamcrest/source/browse/#svn/trunk/hamcrest-objectivec2/hamcrest">checked it out from SVN</a> and built the framework product in Xcode for OS X 10.5/i386.</p>
<p>I struggled for a while to get the dependency on the hamcrest.framework build product into my project. I ended up with this solution:</p>
<ol>
<li>Create a &#8216;Frameworks&#8217; folder in my project root
<li>Copy hamcrest.framework to Frameworks/
<li>In Xcode, right click on &#8216;Frameworks&#8217; and &#8216;Add existing framework&#8230;&#8221;
<li>Browse to the hamcrest.framework folder and add it, making sure to add it to my Unit Tests target only
<li>Right click on the Unit Tests target to add a new Copy Files Build Phase that copies to the Products directory and put that build phase early in the target steps
<li>Add hamcrest.framework to that build phase by dragging from Frameworks
<li>Include Hamcrest in your test class and you&#8217;re good to go
</ol>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;GTMSenTestCase.h&quot;</span>
<span style="color: #6e371a;">#define HC_SHORTHAND</span>
<span style="color: #6e371a;">#import &lt;hamcrest/hamcrest.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ExampleWithAssertThat <span style="color: #002200;">:</span> SenTestCase
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> ExampleWithAssertThat
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> testUsingAssertThat
<span style="color: #002200;">&#123;</span>
    assertThat<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;xx&quot;</span>, is<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;xx&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    assertThat<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;yy&quot;</span>, isNot<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;xx&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    assertThat<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;i like cheese&quot;</span>, containsString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;cheese&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h2>OCMock</h2>
<p>iPhone development requires a lot of mucking around with UI framework classes like UITableView, UIApplicationDelegate and so on. These classes have a lot of dependencies and are depended upon all over the place which can make it hard to get them under test. </p>
<p><a href="http://en.wikipedia.org/wiki/Mock_object">Mock objects</a> are a great solution to this problem and <a href="http://www.mulle-kybernetik.com/software/OCMock/">OCMock</a> provides an implementation for Objective-C. In this case, the OCMock.framework provided in the OCMock binary release worked for me when installed in the same manner as described for Hamcrest above. </p>
<p>This example of <a href="http://www.pathf.com/blogs/2009/01/testing-delegate-ocmock/">testing [UIApplicationDelegate applicationDidFinishLaunching]</a> illustrates its use pretty well. I have a simple test that creates a mock UITableView to asserts a cell is correctly created in [UITableViewController tableView:cellForRowAtIndexPath:]. It looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;GTMSenTestCase.h&quot;</span>
<span style="color: #6e371a;">#define HC_SHORTHAND</span>
<span style="color: #6e371a;">#import &lt;hamcrest/hamcrest.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OCMock/OCMock.h&gt;</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;SortedStateViewController.h&quot;</span>
<span style="color: #6e371a;">#import &quot;Country.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> SortedStateViewControllerTest <span style="color: #002200;">:</span> SenTestCase <span style="color: #002200;">&#123;</span><span style="color: #002200;">&#125;</span>
&nbsp;
SortedStateViewController <span style="color: #002200;">*</span>controller;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> SortedStateViewControllerTest
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setUp <span style="color: #002200;">&#123;</span>
    controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SortedStateViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    Country <span style="color: #002200;">*</span>country <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Country alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>country addState<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;New York&quot;</span> withPopulation<span style="color: #002200;">:</span><span style="color: #2400d9;">19306183</span> andArea<span style="color: #002200;">:</span><span style="color: #2400d9;">47213</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>country addState<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;California&quot;</span> withPopulation<span style="color: #002200;">:</span><span style="color: #2400d9;">36457549</span>     andArea<span style="color: #002200;">:</span><span style="color: #2400d9;">155959</span><span style="color: #002200;">&#93;</span>;
    controller.country <span style="color: #002200;">=</span> country;
    controller.key <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;population&quot;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> testPopulatesCell <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> tableView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>OCMockObject mockForClass<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UITableView class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>tableView stub<span style="color: #002200;">&#93;</span> dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>OCMConstraint any<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span>indexPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSIndexPath</span> indexPathForRow<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span><span style="color: #2400d9;">0</span> 
                                                inSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSUInteger<span style="color: #002200;">&#41;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>controller tableView<span style="color: #002200;">:</span>tableView 
                            cellForRowAtIndexPath<span style="color: #002200;">:</span>indexPath<span style="color: #002200;">&#93;</span>;
    assertThat<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;color: blue;">&quot;California&quot;</span>, equalTo<span style="color: #002200;">&#40;</span>cell.text<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h2>Continuous Integration</h2>
<p>So I&#8217;m pretty happy with where this is now. I&#8217;m working through the Pragmatic book TDD as I go. It&#8217;s also have an ObjectiveCTest that I add assertions to as I play with features of the language.</p>
<p>Missing from this toolkit is a good continuous integration solution. </p>
<p>There is now a <a href="http://blog.jeffreyfredrick.com/2008/11/27/continuous-integration-for-iphonexcode-projects/">recently released Xcode builder for CruiseControl</a>. However, we use <a href="http://www.atlassian.com/software/bamboo/">Bamboo</a> for CI for our Java and Ruby projects and are considering moving to <a href="http://www.jetbrains.com/teamcity/">TeamCity</a>. It looks like there is an <a href="http://github.com/orj/teamcity-xcode/tree/master">Xcode plugin for TeamCity</a> and some <a href="http://forums.atlassian.com/thread.jspa?messageID=257299176&#038;tstart=0">attempts with Bamboo</a>.</p>
<p>More on that later.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2009/02/testing/iphone-unit-testing-toolkit/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Database Testing with Spring 2.5 and DBUnit</title>
		<link>http://blog.carbonfive.com/2008/07/testing/database-testing-with-spring-25-and-dbunit</link>
		<comments>http://blog.carbonfive.com/2008/07/testing/database-testing-with-spring-25-and-dbunit#comments</comments>
		<pubDate>Thu, 31 Jul 2008 22:31:18 +0000</pubDate>
		<dc:creator>christian</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog/?p=30</guid>
		<description><![CDATA[Note: Version 0.9.1 of c5-test-support has been released. We&#8217;ve been using DB Unit on our Java projects for years and the mechanics of how it&#8217;s used has evolved over time. I&#8217;ve recently spent some time making it work a little nicer for how we typically write database tests. What I&#8217;ve created makes using DBUnit on [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: Version 0.9.1 of c5-test-support has been released.</em></p>
<p>We&#8217;ve been using <a href="http://www.dbunit.org">DB Unit</a> on our Java projects for years and the mechanics of how it&#8217;s used has evolved over time.  I&#8217;ve recently spent some time making it work a little nicer for how we typically write database tests.  What I&#8217;ve created makes using DBUnit on a project that is already using Spring and the testing support added in Spring 2.5 just a little easier through the application of convention and annotations.</p>
<p>In general, we&#8217;ve adopted the convention of loading data off the classpath from a flat dataset file named after the test located next to the test on the classpath.  For example (in the maven standard directory structure):</p>
<ul>
<li>src/test/java/com/acme/TripRepositoryTest.java &#8211; Java Test Code</li>
<li>src/test/resources/com/acme/TripRepositoryTest.xml &#8211; DB Unit Data Set for TripRepositoryTest</li>
</ul>
<p>For most tests, the data set is loaded inside the test&#8217;s transaction and rolled back when the test completes so that nothing needs to be cleaned up (see <a href="http://www.springframework.net/doc-latest/reference/html/testing.html#testing-tx">Spring&#8217;s reference</a>).  For other tests &#8212; service or integration tests &#8212; the data is loaded outside of a transaction and must be cleared out manually.  Most projects have a mix of both strategies and both should be easily supported.</p>
<p>When Spring 2.5 came out with its <a href="http://static.springframework.org/spring/docs/2.5.x/reference/new-in-2.html#new-in-2-other-testing">new testing framework</a>, I threw together a custom TestExecutionListener that looks for test methods that are annotated with @DataSet, and when found, loads the data using DB Unit.  Here&#8217;s a transaction-per-test example:</p>
<h4>TripRepositoryImplTest.java &#8211; Example transaction-per-test Test Case</h4>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">@ContextConfiguration<span style="color: #009900;">&#40;</span>locations = <span style="color: #009900;">&#123;</span><span style="color: #0000ff;color: blue;">&quot;classpath:applicationContext.xml&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TripRepositoryImplTest <span style="color: #000000; font-weight: bold;">extends</span> AbstractTransactionalDataSetTestCase <span style="color: #009900;">&#123;</span>
    @Autowired TripRepository repository<span style="color: #339933;">;</span>
&nbsp;
    @Test
    @DataSet
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> forIdShouldFindTrip<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; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        Trip trip = repository.<span style="color: #006633;">forId</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertThat<span style="color: #009900;">&#40;</span>trip, not<span style="color: #009900;">&#40;</span>nullValue<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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The high-level execution path for this example looks like:</p>
<ol>
<li>Inject dependencies (DependencyInjectionTestExecutionListener)</li>
<li>Start transaction (TransactionalTestExecutionListener)</li>
<li>Load dbunit data set from TripRepositoryImplTest.xml (DataSetTestExecutionListener) using the setup operation (default is CLEAN_INSERT)</li>
<li>Execute test</li>
<li>Optionally cleanup dbunit data using the tear down operation (default is NONE)</li>
<li>Rollback transaction (TransactionalTestExecutionListener)</li>
</ol>
<p>Here&#8217;s the trimmed down log output for this test:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">INFO: Began transaction (<span style="color: #ff0000;">1</span>): transaction manager; rollback [true] (TransactionalTestExecutionListener.java:<span style="color: #ff0000;">259</span>)
INFO: Loading dataset <span style="color: #00007f;">from</span> <span style="color: #00007f;">location</span> <span style="color: #7f007f;color: blue;">'classpath:/eg/domain/TripRepositoryImplTest.xml'</span> using operation <span style="color: #7f007f;color: blue;">'CLEAN_INSERT'</span>. (DataSetTestExecutionListener.java:<span style="color: #ff0000;">152</span>)
INFO: Tearing down dataset using operation <span style="color: #7f007f;color: blue;">'NONE'</span>, leaving database connection open. (DataSetTestExecutionListener.java:<span style="color: #ff0000;">67</span>)
INFO: Rolled back transaction after test execution for test context (TransactionalTestExecutionListener.java:<span style="color: #ff0000;">279</span>)</pre></div></div>

</p>
<p>For this to work in its current incarnation, a single datasource must be available for lookup in the application context.  One of the interesting details is what to do with the connection used to load the data.  The framework assumes that if it&#8217;s a transactional connection it should be left open because whatever started the transaction should do the closing.  When it&#8217;s non-transactional it&#8217;s closed after the dataset is loaded.  This convention works well for how I typically write my database tests.</p>
<p>In addition to the @DataSet annotation, we must add the DataSetTestExecutionListener to the set of listeners that are applied to the test class.  As in the above example, you can extend AbstractTransactionalDataSetTestCase which does this for you or you can specify the listener using the class-level annotation @TestExecutionListeners (see <a href="http://svn.carbonfive.com/public/christian/spring-dbunit-test-extension/trunk/src/test/java/eg/domain/TripServiceImplTest.java">example</a>).  It&#8217;s important that the listener is triggered after the TransactionalTestExecutionListener.</p>
<p>If all test methods use the dataset, then the test class (or super class) can be annotated and every test will load the dataset.  Also, if a different dataset should be loaded, the name of the resource can be specified in the annotation (e.g. @DataSet(&#8220;TripRepositoryImplTest-foo.xml&#8221;) or @DataSet(&#8220;classpath:/db/trips.xml&#8221;)).  Lastly, the setup and teardown database operations can be overriden (e.g. @DataSet(setupOperation = &#8220;INSERT&#8221;, teardownOperation=&#8221;DELETE&#8221;)).</p>
<p>This functionality is part of the C5 Test Support package and is available in our maven repository.  To use it, first add the C5 Public Maven repository to your pom.xml, and then add the necessary dependencies:</p>
<h4>pom.xml</h4>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>c5-public-repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://mvn.carbonfive.com/public<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;snapshots<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;updatePolicy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>always<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/updatePolicy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/snapshots<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.dbunit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>dbunit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.2.3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.carbonfive<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test-support<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

</p>
<p>Check out the <a href="http://svn.carbonfive.com/public/christian/spring-dbunit-test-extension/trunk">sample application</a> for details.  It&#8217;s mavenized and utilizes an in-memory database.  Just check it out of subversion, look over the code, and give it a run using your IDE or from the command-line (mvn install).  I&#8217;d be psyched to hear what you think and of course, welcome comments and suggestions.</p>
<h4>Resources:</h4>
<ul>
<li>C5 Test Support Source: <a href="http://svn.carbonfive.com/public/carbonfive/test-support/trunk">http://svn.carbonfive.com/public/carbonfive/test-support/trunk</a></li>
<li>C5 Test Support Maven Home: <a href="http://mvn.carbonfive.com/public/com/carbonfive/test-support/0.6/">http://mvn.carbonfive.com/public/com/carbonfive/test-support/0.6/</a></li>
<li>Sample Application: <a href="http://svn.carbonfive.com/public/christian/spring-dbunit-test-extension/trunk">http://svn.carbonfive.com/public/christian/spring-dbunit-test-extension/trunk</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2008/07/testing/database-testing-with-spring-25-and-dbunit/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Multithreaded Testing</title>
		<link>http://blog.carbonfive.com/2008/05/testing/multithreaded-testing</link>
		<comments>http://blog.carbonfive.com/2008/05/testing/multithreaded-testing#comments</comments>
		<pubDate>Mon, 05 May 2008 22:06:42 +0000</pubDate>
		<dc:creator>christian</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog/?p=29</guid>
		<description><![CDATA[Every now and then you&#8217;ll work on something that needs to handle requests from multiple concurrent threads in a special way. I say &#8220;special way&#8221; because in a web application, everything needs to handle being executed concurrently and there are a slew of techniques used to handle this (prototypes, thread locals, stateless services, etc). Here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then you&#8217;ll work on something that needs to handle requests from multiple concurrent threads in a special way.  I say &#8220;special way&#8221; because in a web application, <i>everything</i> needs to handle being executed concurrently and there are a slew of techniques used to handle this (prototypes, thread locals, stateless services, etc).  Here&#8217;s an example of what I mean by &#8220;special&#8221;&#8230;</p>
<p>On my current project, we have a queue of articles that need human-user attention (i.e. editorial moderation).  Each article must be doled out to only one moderator and there are multiple instances of the web application servicing requests in the cluster.  Imagine tens of thousands of articles per day and a team of moderators churning through them.  We can&#8217;t rely on Java synchronization because it only works within the JVM instance, not across server instances.</p>
<p>The simplified version of the service interface we&#8217;re working on looks like this:</p>
<h4>ArticleService.java &#8211; Example service interface</h4>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ArticleService
<span style="color: #009900;">&#123;</span>
    Article findNextArticleForModeration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What makes this interesting is that we must ensure that the service doesn&#8217;t hand out the same Article to more than one user.  This is impossible to assert using a single thread.  We&#8217;ve all been told that multiple threads and automated testing don&#8217;t mix.  It&#8217;s generally true and should be avoided if at all possible, but in some cases it&#8217;s the only way we can truly assert specific behavior.  I&#8217;ve found a pretty simple way to do this type of testing in a reliable, consistent, and non-disruptive manner.  Despite the fact that the technique leverages Java 1.5 built-in concurrency utilities, most of the engineers who have seen it are surprised and weren&#8217;t aware that such testing was so easy to implement.</p>
<p>Given the above service interface, here&#8217;s a test that will assert that no single article is given out to more than one invoker of the method findNextArticleForModeration().  The scenario we&#8217;re simulating is 10 users feverishly moderating a queue of 250 articles as quickly as possible.</p>
<h4>ArticleServiceImplTest.java &#8211; Test to invoke service concurrently</h4>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">...
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> findNextArticleForModerationStressTest<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; font-weight: bold;">Exception</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #006600; font-weight: bold;">int</span> ARTICLE_COUNT = <span style="color: #cc66cc;">250</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #006600; font-weight: bold;">int</span> THREAD_COUNT = <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Create test data and callable tasks</span>
    <span style="color: #666666; font-style: italic;">//</span>
    Set<span style="color: #339933;">&lt;</span>Article<span style="color: #339933;">&gt;</span> testArticles = <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Article<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Collection<span style="color: #339933;">&lt;</span>Callable<span style="color: #339933;">&lt;</span>Article<span style="color: #339933;">&gt;&gt;</span> tasks = <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Callable<span style="color: #339933;">&lt;</span>Article<span style="color: #339933;">&gt;&gt;</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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> ARTICLE_COUNT<span style="color: #339933;">;</span> i++<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Test data</span>
        testArticles.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Article<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Tasks - each task makes exactly one service invocation.</span>
        tasks.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Callable<span style="color: #339933;">&lt;</span>Article<span style="color: #339933;">&gt;</span><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> Article call<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; font-weight: bold;">Exception</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> articleService.<span style="color: #006633;">findNextArticleForModeration</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><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    articleService.<span style="color: #006633;">createArticles</span><span style="color: #009900;">&#40;</span>testArticles<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Execute tasks</span>
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #003399; font-weight: bold;">ExecutorService</span> executorService = <span style="color: #003399; font-weight: bold;">Executors</span>.<span style="color: #006633;">newFixedThreadPool</span><span style="color: #009900;">&#40;</span>THREAD_COUNT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// invokeAll() blocks until all tasks have run...</span>
    List<span style="color: #339933;">&lt;</span>Future<span style="color: #339933;">&lt;</span>Article<span style="color: #339933;">&gt;&gt;</span> futures = executorService.<span style="color: #006633;">invokeAll</span><span style="color: #009900;">&#40;</span>tasks<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertThat<span style="color: #009900;">&#40;</span>futures.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, is<span style="color: #009900;">&#40;</span>ARTICLE_COUNT<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Assertions</span>
    <span style="color: #666666; font-style: italic;">//</span>
    Set<span style="color: #339933;">&lt;</span>Long<span style="color: #339933;">&gt;</span> articleIds = <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Long<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>ARTICLE_COUNT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Future<span style="color: #339933;">&lt;</span>Article<span style="color: #339933;">&gt;</span> future : futures<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// get() will throw an exception if an exception was thrown by the service.</span>
        Article article = future.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Did we get an article?</span>
        assertThat<span style="color: #009900;">&#40;</span>article, not<span style="color: #009900;">&#40;</span>nullValue<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: #666666; font-style: italic;">// Did the service lock the article before returning?</span>
        assertThat<span style="color: #009900;">&#40;</span>article.<span style="color: #006633;">isLocked</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, is<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</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;">// Is the article id unique (see Set.add() javadoc)?</span>
        assertThat<span style="color: #009900;">&#40;</span>articleIds.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>article.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>, is<span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</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: #666666; font-style: italic;">// Did we get the right number of article ids?</span>
    assertThat<span style="color: #009900;">&#40;</span>articleIds.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, is<span style="color: #009900;">&#40;</span>ARTICLE_COUNT<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
...</pre></div></div>

<p>The test starts off by creating 250 test articles to be moderated.  It also creates 250 &#8216;tasks&#8217;, each designed to make a single service invocation of findNextArticleForModeration().  The real magic happens in <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int)">Executors.newFixedThreadPool()</a> and <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)">executorService.invokeAll()</a>.  The first creates a new <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html">ExecutorService</a> backed by a thread pool of the specified size.  This is a generic ExecutorService that is designed to churn through tasks using all of the threads in the pool.  invokeAll blocks until every task has finished executing.  In this test, 10 threads will rip through 250 tasks, each making a single call to our service and capturing the result of that call.  Each task execution results in a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html">Future</a>, which is a handle to the results of the task (and more).</p>
<p>Iterating over each resulting future, we make several assertions.  The most important one is the last, where we assert that every task is given a unique Article.  Thanks to the natural semantics of Set, this is easy to do in an elegant way.  Another useful, though unexpected, feature is that if an exception occurs during the task execution, an <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutionException.html">ExecutionException</a> will be thrown when <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html#get()">get()</a> is called on the corresponding Future.  If our service fails for some reason, the test will fail because no exceptions are expected.</p>
<p>This technique makes simulating a multi-threaded environment in a test easy and readable.  It&#8217;s important to only use this technique when it&#8217;s <em>really</em> necessary.  The resulting test is more of an integration test than a unit test, and its run time is orders of magnitude more than a unit test, so overuse of the technique will artificially inflate the time it takes to runs the tests.  After I&#8217;ve finished working on the component under test, I will reduce the test-data size and thread count to a level that the test still provides value, but is no longer a stress test (e.g. 10 articles and 2 threads).  The next time the component is being worked on, the developer can crank up the values and run the tests to be confident that the behavior isn&#8217;t broken.</p>
<p>The complete source for a working example of this technique is available <a href="http://svn.carbonfive.com/public/christian/multi-threaded-testing/trunk">here</a>.  You&#8217;ll need Maven (or IntelliJ IDEA 7.x) to build and run the test.  By default, the tests run against an in-memory H2Database instance, but if you look at application.properties you&#8217;ll see configurations for PostgreSQL and MySQL as well.</p>
<p>Happy testing!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2008/05/testing/multithreaded-testing/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>DBUnit 2.2, Spring, and Testing</title>
		<link>http://blog.carbonfive.com/2007/05/testing/dbunit-22-spring-and-testing</link>
		<comments>http://blog.carbonfive.com/2007/05/testing/dbunit-22-spring-and-testing#comments</comments>
		<pubDate>Tue, 15 May 2007 16:30:20 +0000</pubDate>
		<dc:creator>christian</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog/?p=12</guid>
		<description><![CDATA[You may have noticed that DBUnit changed its connection closing behavior in v2.2. We noticed it when tests deriving from our custom DatabaseTestCase implementation (which uses DBUnit) starting failing. Initially we just reverted back to version 2.1. Since then I&#8217;ve discovered what the problem was and found a workaround, and while I was at it, [...]]]></description>
			<content:encoded><![CDATA[<p>You may have noticed that <a href="http://www.dbunit.org/">DBUnit</a> changed its connection closing behavior in v2.2.  We noticed it when tests deriving from our custom DatabaseTestCase implementation (which uses DBUnit) starting failing.  Initially we just reverted back to version 2.1.  Since then I&#8217;ve discovered what the problem was and found a workaround, and while I was at it, I created some nice utilities to be used in tests which need data fixtures.</p>
<p><strong>Problem</strong></p>
<p>In v2.2 DBUnit introduces some new abstractions, one of which is the <a href="http://www.dbunit.org/apidocs/org/dbunit/IDatabaseTester.html">IDatabaseTester</a>.  Whether it’s by design or by accident, the default implementation closes connections after executing its operations (<a href="http://sourceforge.net/tracker/?func=detail&#038;aid=1473744&#038;group_id=47439&#038;atid=449494">more info</a>).  The end result is that in our unit tests, the database connection is closed before the test can run.</p>
<p><strong>Solution</strong></p>
<p>I’ve created two simple util classes for loading test data into a database without closing the connection:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** A helper for loading data sets into unit tests. */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DatabaseUtils <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> loadDataSet<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Class</span> clazz, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">DataSource</span> dataSource<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        IDataSet dataSet = <span style="color: #000000; font-weight: bold;">new</span> FlatXmlDataSet<span style="color: #009900;">&#40;</span>TestUtils.<span style="color: #006633;">datasetInputStream</span><span style="color: #009900;">&#40;</span>clazz<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        IDatabaseTester tester = <span style="color: #000000; font-weight: bold;">new</span> ExistingConnectionDatabaseTester<span style="color: #009900;">&#40;</span>dataSource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tester.<span style="color: #006633;">setDataSet</span><span style="color: #009900;">&#40;</span>dataSet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tester.<span style="color: #006633;">onSetup</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>


<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** A special DatabaseTester that doesn’t close the connection when its done. */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExistingConnectionDatabaseTester <span style="color: #000000; font-weight: bold;">extends</span> AbstractDatabaseTester <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">DataSource</span> dataSource<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ExistingConnectionDatabaseTester<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">DataSource</span> dataSource<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</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;">this</span>.<span style="color: #006633;">dataSource</span> = dataSource<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> IDatabaseConnection getConnection<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; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> DatabaseConnection<span style="color: #009900;">&#40;</span>DataSourceUtils.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>dataSource<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: #006600; font-weight: bold;">void</span> closeConnection<span style="color: #009900;">&#40;</span>IDatabaseConnection connection<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Don't close that connection!</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first of these depends on another test-related utility, TestUtils.java (see below), which converts a class to a path.  The second depends on Spring’s <a href="http://www.springframework.org/docs/api/org/springframework/jdbc/datasource/DataSourceUtils.html">DataSourceUtils</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * A set of utilities that generate paths from classnames.  This is useful when
 * making reference to resources used by test cases, when the resources are
 * located in a directory path matching the class' package.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestUtils <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399; font-weight: bold;">String</span> TEST_PREFIX = <span style="color: #0000ff;color: blue;">&quot;src/test/resources/&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Maven2 default</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399; font-weight: bold;">String</span> pathString<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Class</span> clazz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> pathString<span style="color: #009900;">&#40;</span>TEST_PREFIX, clazz, <span style="color: #0000ff;color: blue;">&quot;&quot;</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: #000000; font-weight: bold;">static</span> <span style="color: #003399; font-weight: bold;">String</span> pathString<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Class</span> clazz, <span style="color: #003399; font-weight: bold;">String</span> resource<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> pathString<span style="color: #009900;">&#40;</span>TEST_PREFIX, clazz, resource<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: #000000; font-weight: bold;">static</span> <span style="color: #003399; font-weight: bold;">String</span> pathString<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> prefix, <span style="color: #003399; font-weight: bold;">Class</span> clazz, <span style="color: #003399; font-weight: bold;">String</span> resource<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        prefix = <span style="color: #009900;">&#40;</span>prefix <span style="color: #339933;">!</span>= <span style="color: #006600; font-weight: bold;">null</span> <span style="color: #339933;">?</span> prefix : <span style="color: #0000ff;color: blue;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399; font-weight: bold;">StringBuffer</span> sb = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>prefix<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>prefix.<span style="color: #006633;">endsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>ClassUtils.<span style="color: #006633;">classPackageAsResourcePath</span><span style="color: #009900;">&#40;</span>clazz<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>resource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> sb.<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>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399; font-weight: bold;">InputStream</span> pathInputStream<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Class</span> clazz, <span style="color: #003399; font-weight: bold;">String</span> resource<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">FileNotFoundException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> pathInputStream<span style="color: #009900;">&#40;</span>TEST_PREFIX, clazz, resource<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: #000000; font-weight: bold;">static</span> <span style="color: #003399; font-weight: bold;">InputStream</span> pathInputStream<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> prefix, <span style="color: #003399; font-weight: bold;">Class</span> clazz, <span style="color: #003399; font-weight: bold;">String</span> resource<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">FileNotFoundException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">FileInputStream</span><span style="color: #009900;">&#40;</span>pathString<span style="color: #009900;">&#40;</span>prefix, clazz, resource<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: #000000; font-weight: bold;">static</span> <span style="color: #003399; font-weight: bold;">InputStream</span> datasetInputStream<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Class</span> clazz<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">FileNotFoundException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> pathInputStream<span style="color: #009900;">&#40;</span>TEST_PREFIX, clazz, ClassUtils.<span style="color: #006633;">getShortName</span><span style="color: #009900;">&#40;</span>clazz<span style="color: #009900;">&#41;</span> + <span style="color: #0000ff;color: blue;">&quot;.xml&quot;</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>These three classes give us everything we need to load data into our test database using DBUnit.  And TestUtils can be used to load other test resources from the classpath as well, like images, documents, CSVs, etc.</p>
<p>Here&#8217;s an example of how it can be used in a DAO test based on Spring&#8217;s test classes.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArtistHibernateDaoTest <span style="color: #000000; font-weight: bold;">extends</span> AbstractAnnotationAwareTransactionalTests <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> ArtistHibernateDao artistDao<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> setArtistDao<span style="color: #009900;">&#40;</span>ArtistHibernateDao artistDao<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;">artistDao</span> = artistDao<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> getConfigLocations<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: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;color: blue;">&quot;applicationContext-database.xml&quot;</span>, <span style="color: #0000ff;color: blue;">&quot;applicationContext-hibernate.xml&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #006600; font-weight: bold;">void</span> onSetUpInTransaction<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; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        DatabaseUtils.<span style="color: #006633;">loadDataSet</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, getJdbcTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getDataSource</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: #006600; font-weight: bold;">void</span> testFindAllNames<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> names = artistDao.<span style="color: #006633;">findAllNames</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: #cc66cc;">3</span>, names.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertTrue<span style="color: #009900;">&#40;</span>names.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;The Cure&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertTrue<span style="color: #009900;">&#40;</span>names.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;Depeche Mode&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertTrue<span style="color: #009900;">&#40;</span>names.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;New Order&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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>onSetUpInTransaction() loads the data set using the convention of (packagename).(ClassName).xml before each test (e.g. com.c5.PizzaTest loads the file com/c5/PizzaText.xml on the classpath).  In this case the data is loaded in the same transaction as the test so that no clean-up is necessary when the test is completed.  These tests run fast!</p>
<p>We&#8217;ve been talking about moving away from our custom DatabaseTestCase class hierarchy.  The above functionality in conjunction with <a href="http://www.springframework.org/docs/api/org/springframework/test/annotation/AbstractAnnotationAwareTransactionalTests.html">Spring&#8217;s test hierarchy</a> could be a good start.</p>
<p>On a somewhat related note, there is a new unit testing framework called <a href="http://unitils.sourceforge.net">Unitils </a>which does this sort of thing and much more.  I haven’t used it but it sounds interesting.</p>
<p>Christian</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2007/05/testing/dbunit-22-spring-and-testing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic Custom Argument Matching in EasyMock</title>
		<link>http://blog.carbonfive.com/2007/05/testing/generic-custom-argument-matching-in-easymock</link>
		<comments>http://blog.carbonfive.com/2007/05/testing/generic-custom-argument-matching-in-easymock#comments</comments>
		<pubDate>Sun, 06 May 2007 19:05:01 +0000</pubDate>
		<dc:creator>randy</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[easymock java testing]]></category>

		<guid isPermaLink="false">http://blog/?p=11</guid>
		<description><![CDATA[EasyMock is a clean, simple library for creating mock objects. It provides a whole range of facilities for declaring our expectations of the method call interactions on the mock including call count, call order, return values, exceptions, and argument matchers. Unfortunately, the argument matchers provide a limited set of argument expectations and a somewhat cumbersome [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.easymock.org/">EasyMock</a> is a clean, simple library for creating mock objects. It provides a whole range of facilities for declaring our expectations of the method call interactions on the mock including call count, call order, return values, exceptions, and argument matchers. Unfortunately, the argument matchers provide a limited set of argument expectations and a somewhat cumbersome process for expanding that set. Using java generics, it is possible to create a custom argument matcher allowing simple expression of unlimited assertions about Object arguments.</p>
<p><span id="more-11"></span></p>
<p>Imagine we have a test fixture, BlogEntryController, which depends on a single service, BlogService.  Because we want to focus our testing on the fixture, we&#8217;ll inject a mock object as the service.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//follow EasyMock's convention of static imports</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">easymock</span>.<span style="color: #006633;">EasyMock</span>.<span style="color: #339933;">*</span>
...
<span style="color: #006633;">BlogEntryController</span> controller = <span style="color: #000000; font-weight: bold;">new</span> BlogEntryController<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
BlogService mockBlogService = createMock<span style="color: #009900;">&#40;</span>BlogService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
controller.<span style="color: #006633;">setBlogService</span><span style="color: #009900;">&#40;</span>mockBlogService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The next step is to record the expectations about the interactions between the fixture and its collaborator. In the record phase, we simply interact with the mock to record these expectations:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//record phase</span>
BlogEntry blogEntry = <span style="color: #000000; font-weight: bold;">new</span> BlogEntry<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mockBlogService.<span style="color: #006633;">saveBlogEntry</span><span style="color: #009900;">&#40;</span>blogEntry<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//replay phase</span>
replay<span style="color: #009900;">&#40;</span>mockBlogService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//method under test</span>
controller.<span style="color: #006633;">handleRequest</span><span style="color: #009900;">&#40;</span>request, response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//verify</span>
verify<span style="color: #009900;">&#40;</span>mockBlogService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By default, argument expectations are verified using the equals() method. For the previous expectation, when the controller code is run, EasyMock will assert that the BlogEntry passed in by the controller equals() that passed in during the record phase. If, for instance, the controller creates its own BlogEntry and BlogEntry has the default implementation of equals(), this expectation would fail. Fortunately, EasyMock provides other argument matchers which can verify arguments based on different criteria:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">mockBlogService.<span style="color: #006633;">saveBlogEntry</span><span style="color: #009900;">&#40;</span>isA<span style="color: #009900;">&#40;</span>BlogEntry.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By using the isA(Class clazz) argument matcher, one of several predefined in EasyMock, the code will verify that the argument passed from the controller to the mock service is an instance of the BlogEntry class.  By relaxing our expectations, we&#8217;ve come up with a test case which would pass.  However, generally speaking, we&#8217;ll probably want to test some more details about the BlogEntry. For instance, it&#8217;s common to want to verify the argument&#8217;s properties.</p>
<p>EasyMock has the facility to create custom argument matchers. It&#8217;s a somewhat heavy weight solution requiring the implementation of 3 methods: 2 for the IArgumentMatcher interface and one static method used during the record phase. This facility seems better suited to creating reusable argument matchers (akin to the predefined ones which come with EasyMock) rather than singular matchers relevant only to a particular test case.</p>
<p>To bridge the gap, I have created a generic argument matcher, EasyMockUtils.argAssert(Assertion&lt;E&gt; assertion), which abstracts away the required EasyMock interaction code, allowing one to more simply express, inline if desired, the argument expectations:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">common.test.easymock.EasyMockUtils</span><span style="color: #339933;">;</span>
...
<span style="color: #006633;">mockBlogService</span>.<span style="color: #006633;">saveBlogEntry</span><span style="color: #009900;">&#40;</span>argAssert<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Assertion<span style="color: #339933;">&lt;</span>BlogEntry<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-weight: bold;">void</span> check<span style="color: #009900;">&#40;</span>BlogEntry blogEntry<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;submitted blog entry body&quot;</span>, blogEntry.<span style="color: #006633;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertNull<span style="color: #009900;">&#40;</span>blogEntry.<span style="color: #006633;">getDateCreated</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: #009900;">&#41;</span></pre></div></div>

<p>The argAssert static method takes a genericized instance of the Assertion interface in which one can declare any number of assertions against the argument passed in during the replay phase. As an added benefit, the Assertion implementation could also be leveraged to modify the passed in argument.  For instance, imagine the controller depends on the service setting the dateCreated property on the passed in bean. We can ensure our mock replicates that behavior:</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;">mockBlogService.<span style="color: #006633;">saveBlogEntry</span><span style="color: #009900;">&#40;</span>argAssert<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Assertion<span style="color: #339933;">&lt;</span>BlogEntry<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-weight: bold;">void</span> check<span style="color: #009900;">&#40;</span>BlogEntry blogEntry<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;submitted blog entry body&quot;</span>, blogEntry.<span style="color: #006633;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        assertNull<span style="color: #009900;">&#40;</span>blogEntry.<span style="color: #006633;">getDateCreated</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: #666666; font-style: italic;">//mimic the behavior of the true service</span>
        blogEntry.<span style="color: #006633;">setDateCreated</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Date</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: #009900;">&#41;</span></pre></div></div>

<p>Mocking to this detail could trigger a warning regarding the level of coupling between the test, the fixture, and the collaborator; however, I have seen situations where it can make the difference between being able to use a mock object for testing or not. Mockist testers in general must be wary of unnecessarily deep coupling where, for instance, a refactor of the fixture/collaborator interactions could result in as much time spent updating test code even if the test&#8217;s high level assertions remain unchanged. However, used attentively during test driven development, mock objects can help a programmer focus on keeping those interactions simple and clearly defined.  Furthermore, testing with mock objects helps one to focus on unit testing the fixture at hand. In that situation, the generic custom argument matcher provides a simplified tool for expressing assertions about that code.</p>
<p />
<p>And here&#8217;s the code. The generic interface which allows one to specify argument assertions:</p>
<p><b>Assertion.java</b></p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">common.test.easymock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Assertion<span style="color: #339933;">&lt;</span>E<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Allows specifying varying assertions about an argument.
    * @param argument the argument being check
    * @throws Error throw an exception in the case of an assertion failure
    */</span>
    <span style="color: #006600; font-weight: bold;">void</span> check<span style="color: #009900;">&#40;</span>E argument<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Error</span>, <span style="color: #003399; font-weight: bold;">Exception</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A simple container for the static method used to declare the argument matching assertion during the mock record phase.</p>
<p><b>EasyMockUtils.java</b></p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">common.test.easymock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.easymock.EasyMock</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> EasyMockUtils
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>E<span style="color: #339933;">&gt;</span> E argAssert<span style="color: #009900;">&#40;</span>Assertion<span style="color: #339933;">&lt;</span>E<span style="color: #339933;">&gt;</span> assertion<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        EasyMock.<span style="color: #006633;">reportMatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ArgumentAssertion<span style="color: #009900;">&#40;</span>assertion<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #006600; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the IArgumentMatcher implementation which ties the pieces together:</p>
<p><b>ArgumentAssertion.java</b></p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">common.test.easymock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.easymock.IArgumentMatcher</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> ArgumentAssertion <span style="color: #000000; font-weight: bold;">implements</span> IArgumentMatcher <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Assertion assertion<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Error</span> assertionError<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ArgumentAssertion<span style="color: #009900;">&#40;</span>Assertion assertion<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;">assertion</span> = assertion<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">boolean</span> matches<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Object</span> actual<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            assertion.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>actual<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #006600; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Error</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            assertionError = e<span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #006600; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Exception</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            assertionError = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Error</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #006600; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> appendTo<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">StringBuffer</span> buffer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        buffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;argumentAssertion(exception &quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>assertionError<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;)&quot;</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>And of course a test:</p>
<p><b>ArgumentAssertionTest.java</b></p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">common.test.easymock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> common.<span style="color: #006633;">test</span>.<span style="color: #006633;">easymock</span>.<span style="color: #006633;">EasyMockUtils</span>.<span style="color: #006633;">argAssert</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">junit.framework.TestCase</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;">easymock</span>.<span style="color: #006633;">EasyMock</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> ArgumentAssertionTest <span style="color: #000000; font-weight: bold;">extends</span> TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> TestInterface
    <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-weight: bold;">void</span> testMethod<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    TestInterface mock<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #006600; font-weight: bold;">void</span> setUp<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; font-weight: bold;">Exception</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mock = createMock<span style="color: #009900;">&#40;</span>TestInterface.<span style="color: #000000; font-weight: bold;">class</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: #006600; font-weight: bold;">void</span> testArgumentAssertionSucess<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//record expected behavior</span>
        mock.<span style="color: #006633;">testMethod</span><span style="color: #009900;">&#40;</span>argAssert<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Assertion<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><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: #006600; font-weight: bold;">void</span> check<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> argument<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//do nothing - should pass</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//should get no errors</span>
        replay<span style="color: #009900;">&#40;</span>mock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        mock.<span style="color: #006633;">testMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        verify<span style="color: #009900;">&#40;</span>mock<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: #006600; font-weight: bold;">void</span> testArgumentAssertionFailure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//record expected behavior</span>
        mock.<span style="color: #006633;">testMethod</span><span style="color: #009900;">&#40;</span>argAssert<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Assertion<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><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: #006600; font-weight: bold;">void</span> check<span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">String</span> argument<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                fail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;explicitly fail assertion&quot;</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: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        replay<span style="color: #009900;">&#40;</span>mock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-weight: bold;">boolean</span> testSucceeded = <span style="color: #006600; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            mock.<span style="color: #006633;">testMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            verify<span style="color: #009900;">&#40;</span>mock<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            testSucceeded = <span style="color: #006600; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">AssertionError</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//should get this error</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000;  font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>testSucceeded<span style="color: #009900;">&#41;</span>
            fail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;color: blue;">&quot;should have gotten a failure&quot;</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>

]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2007/05/testing/generic-custom-argument-matching-in-easymock/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DBUnit, HSQL and the BOOLEAN data type</title>
		<link>http://blog.carbonfive.com/2005/07/testing/dbunit-hsql-and-the-boolean-data-type</link>
		<comments>http://blog.carbonfive.com/2005/07/testing/dbunit-hsql-and-the-boolean-data-type#comments</comments>
		<pubDate>Wed, 20 Jul 2005 17:40:25 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://blog/?p=7</guid>
		<description><![CDATA[We have been using HSQL in-memory along with DBUnit for unit testing lately, and I found an issue using the most recent version of each. Basically, HSQL has added a new data type, BOOLEAN, which replaces BIT. But DBUnit is not updated to support this, and an error is throw when you attempt to insert [...]]]></description>
			<content:encoded><![CDATA[<p>We have been using HSQL in-memory along with DBUnit for unit testing lately, and I found an issue using the most recent version of each.  Basically, HSQL has added a new data type, BOOLEAN, which replaces BIT.  But DBUnit is not updated to support this, and an error is throw when you attempt to insert some BOOLEAN data using DBUnit.</p>
<p/>
The error looks like this:</p>
<p/>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">WARNING - TABLE.COLUMN data type (16, 'BOOLEAN') not recognized
and will be ignored. See FAQ for more information.</pre></div></div>

<p/>
The solution outlined here is straightforward.  You need to create a new data type factory that extends the DBUnit class <b>DefaultDataTypeFactory</b>.  This new class just handles the SQL type <b>Types.BOOLEAN</b> as a special case.  The code follows:</p>
<p/>
<p><b>HsqlDataTypeFactory.java</b></p>
<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;">company.project.test</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.dbunit.dataset.datatype.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.*</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> HsqlDataTypeFactory
  <span style="color: #000000; font-weight: bold;">extends</span> DefaultDataTypeFactory
<span style="color: #009900;">&#123;</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> Log log <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>HsqlDataTypeFactory.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> DataType createDataType<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> sqlType, <span style="color: #003399;">String</span> sqlTypeName<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> DataTypeException
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sqlType <span style="color: #339933;">==</span> <span style="color: #003399;">Types</span>.<span style="color: #006633;">BOOLEAN</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> DataType.<span style="color: #006633;">BOOLEAN</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">createDataType</span><span style="color: #009900;">&#40;</span>sqlType, sqlTypeName<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>Then, in order to use this data type factory, just set a property on the <b>IDatabaseConnection</b> DBUnit object in your code (here is an example method):</p>
<p/>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">protected</span> IDatabaseConnection getConnection<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>
    IDatabaseConnection connection <span style="color: #339933;">=</span>
      <span style="color: #000000; font-weight: bold;">new</span> DatabaseConnection<span style="color: #009900;">&#40;</span>dataSource.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    DatabaseConfig config <span style="color: #339933;">=</span> connection.<span style="color: #006633;">getConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span>DatabaseConfig.<span style="color: #006633;">PROPERTY_DATATYPE_FACTORY</span>,
                       <span style="color: #000000; font-weight: bold;">new</span> HsqlDataTypeFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> connection<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.carbonfive.com/2005/07/testing/dbunit-hsql-and-the-boolean-data-type/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
