<?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>Ted Naleid &#187; unit testing</title>
	<atom:link href="http://naleid.com/blog/category/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<lastBuildDate>Wed, 16 May 2012 00:54:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Upgrading to Grails 2 Unit Testing</title>
		<link>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/</link>
		<comments>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/#comments</comments>
		<pubDate>Wed, 02 May 2012 04:15:28 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=580</guid>
		<description><![CDATA[Grails 2 has a lot of great new unit testing features that make many test scenarios easier. The grails documentation does an OK job of describing some of the new features, but there really wasn&#8217;t anywhere that I could find that had a comprehensive list of changes you should make to your code when migrating [...]]]></description>
			<content:encoded><![CDATA[<p>Grails 2 has a lot of great <a href="http://grails.org/doc/latest/guide/testing.html#9.3 Functional Testing" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_9.3_Functional_Testing?referer=');">new unit testing features</a> that make many test scenarios easier.</p>
<p>The grails documentation does an OK job of describing some of the new features, but there really wasn&#8217;t anywhere that I could find that had a comprehensive list of changes you should make to your code when migrating from grails 1.3.X to 2.0.X.</p>
<p>This blog post is the list of changes that I wished I had when I started to migrate our code.<span id="more-580"></span></p>
<p>I found these pages helpful in figuring things out:</p>
<ul>
<li>The <a href="http://grails.org/doc/latest/guide/testing.html#9.3 Functional Testing" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_9.3_Functional_Testing?referer=');">Grails 2 Testing Documentation</a></li>
<li>Peter Ledbrook&#8217;s <a href="http://pledbrook.github.com/grails-howtos/en/upgradeToGrails2.html" onclick="pageTracker._trackPageview('/outgoing/pledbrook.github.com/grails-howtos/en/upgradeToGrails2.html?referer=');">&#8220;Upgrade to Grails 2&#8243;</a> blog post</li>
<li>The Grails 2 <a href="http://www.grails.org/doc/2.0.x/guide/gettingStarted.html#upgradingFromPreviousVersionsOfGrails" onclick="pageTracker._trackPageview('/outgoing/www.grails.org/doc/2.0.x/guide/gettingStarted.html_upgradingFromPreviousVersionsOfGrails?referer=');">&#8220;Upgrading From Previous Versions of Grails&#8221;</a> section of the user guide.</li>
<li>Rob Fletcher&#8217;s <a href="http://blog.freeside.co/blog/2011/11/29/grails-2-upgrade-part-1/" onclick="pageTracker._trackPageview('/outgoing/blog.freeside.co/blog/2011/11/29/grails-2-upgrade-part-1/?referer=');">Upgrading to Grails 2: Part 1</a></li>
<li>This google plus thread on <a href="https://plus.google.com/117411438136918964913/posts/axyCZk1P5L5" onclick="pageTracker._trackPageview('/outgoing/plus.google.com/117411438136918964913/posts/axyCZk1P5L5?referer=');">people&#8217;s experiences upgrading to grails 2</a>
<li><a href="http://jira.grails.org/browse/GRAILS-7617" onclick="pageTracker._trackPageview('/outgoing/jira.grails.org/browse/GRAILS-7617?referer=');">GRAILS-7617</a> &#8211; TestFor(ControllerClass) annotation doesn&#8217;t provide renderArgs</li>
</ul>
<p>The company I&#8217;m consulting at made a large investment in unit testing in the grails 1.2/1.3 days with 500-600 unit tests that heavily leverage custom extended base classes, `Mixin`s, and the grails 1.3 way of doing unit testing.   This investment helped us keep most of our testing quick in unit tests, but has made this the most painful grails upgrade cycle I&#8217;ve been through (more painful than the grails 1.0->1.1 upgrade, which was the previous worst).</p>
<p>There have been surprisingly few changes to the actual production code to get things going, the vast majority of changes have been in how unit testing works.</p>
<h3>General Test Upgrade Tips</h3>
<ul>
<li>If you use an IDE with a debugger, it&#8217;s <strong>very</strong> useful to have a separate copy of your code that works against grails 1.3.7 so that you can have 2 instances of IntelliJ and can step through line by line in each codebase to see where things diverge.</li>
<li>Methods with <code>@Test</code>, <code>@Before</code>, and <code>@After</code> MUST be declared <code>public void</code> or else they won&#8217;t be picked up</li>
<li>I had real problems with using the new grails annotations (like <code>@Mock</code>) while extending a custom super-class, I haven&#8217;t dug to the bottom of this yet, but the grails annotations seem to have trouble with class hierarchies, avoid them if possible</li>
<li>To that end, if you use the new grails annotations (like <code>@TestFor</code>), you MUST remove the old <code>extends GrailsUnitTestCase</code></li>
<li>Integration tests should also never extend <code>GrailsUnitTestCase</code>, they should instead extend <code>GroovyTestCase</code> and just use <code>assert</code></li>
<li>I&#8217;ve hit a number of test pollution issues when refactoring my code, where one test hasn&#8217;t properly cleaned up after itself affecting other tests downstream.  If your test passes in isolation, you&#8217;ll need to run the tests in the same order as <code>grails test-app</code> does and slowly take out tests till your tests passes.  Then you know which test is causing the pollution.  The grails 2 test runner doesn&#8217;t show you the test order, but you can get it by setting a breakpoint in the <code>JUnit4GrailsTestType.doPrepare</code> method (line 54 as of grails 2.0.3) and executing this in your IDE: <code>testClasses.name.each { println it }</code>.  Then you can take that list, join it together into one line and prepend <code>grails test-app</code> on it to run them in order.</li>
</ul>
<p>Another way to get the in-order list of test classes from the test reports after a failed test using a little grep and sed:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> testsuite target<span style="color: #000000; font-weight: bold;">/</span>test-reports<span style="color: #000000; font-weight: bold;">/</span>TESTS-TestSuites.xml <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> testsuites <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d\  <span style="color: #660033;">-f8-9</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-E</span> <span style="color: #ff0000;">'s/name=&quot;(.*)&quot; package=&quot;(.*)&quot;/\2.\1/'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> .</pre></div></div>

<h3>Mocking Class Methods by mucking with the metaClass</h3>
<p>Groovy is now more stringent on closure arguments matching the type signature of a method it&#8217;s meant to mock.  If the method you&#8217;re mocking types it&#8217;s variables, you need to match that with your closure.</p>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">MyDomain.<span style="color: #006600;">metaClass</span>.<span style="color: #ff0000;">'static'</span>.<span style="color: #993399; font-weight: bold;">executeUpdate</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> theQuery, theParams <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">MyDomain.<span style="color: #006600;">metaClass</span>.<span style="color: #ff0000;">'static'</span>.<span style="color: #993399; font-weight: bold;">executeUpdate</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #aaaadd; font-weight: bold;">String</span> theQuery, <span style="color: #aaaadd; font-weight: bold;">Collection</span> theParams <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Otherwise it won&#8217;t find your closure and will still call the regular method you&#8217;re trying to mock.  Put a breakpoint/<code>println</code> in your mocked methods to ensure that they&#8217;re being called by the code under test.</p>
<h2>Unit Testing Domain Classes in Grails 2</h2>
<h3>mockDomain</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span> 
    ...
    <span style="color: #006600;">mockDomain</span><span style="color: #66cc66;">&#40;</span>Person<span style="color: #66cc66;">&#41;</span>
    mockDomain<span style="color: #66cc66;">&#40;</span>Vehicle<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@Mock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person, Vehicle<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
   ...</pre></div></div>

<p>If you&#8217;ve given <code>mockDomain</code> instances to save, you&#8217;ll need to save them manually now.</p>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockDomain<span style="color: #66cc66;">&#40;</span>Person, <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If you keep the old style of domain mocking through <code>mockDomain</code> you need to ensure that you aren&#8217;t calling it twice on the same class (say in setUp and then again in a test method, or in a superclass and a subclass).  This causes really hard to find test pollution errors downstream with the grails 2.0.3 code.  It appears that it&#8217;s messing with the metaClass again and re-mocking it, but then when the test is done not fully cleaning up after both metaClass modifications.  I consider this a bug as <code>mockDomain</code> should be idempotent; if you&#8217;ve already mocked a class calling <code>mockDomain</code> again shouldn&#8217;t remock the class, but this is what we have now.</p>
<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@TestMixin<span style="color: #66cc66;">&#40;</span>GrailsUnitTestMixin<span style="color: #66cc66;">&#41;</span>
@Mock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
    ...
    @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testPerson<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>failOnError: <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
    ...</pre></div></div>

<p>One caveat is that validation is now enforced on these objects, you can either pass in <code>validate:false</code> on the object, or use the <a href="https://bitbucket.org/tednaleid/grails-test-data/wiki/Home" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/Home?referer=');">grails BuildTestData plugin</a> to create your valid objects:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@TestMixin<span style="color: #66cc66;">&#40;</span>GrailsUnitTestMixin<span style="color: #66cc66;">&#41;</span>
@Build<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
    ...
    @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testPerson<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        Person.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span>
        ...</pre></div></div>

<p>The Build Test Data plugin has recently been enhanced to work with grails unit tests, and I recommend using it&#8217;s <code>@Build</code> annotation in place of <code>@Mock</code> in pretty much every case.  It just makes things so much easier by building fully valid objects automatically for you.</p>
<h2>Controller Unit Tests in Grails 2</h2>
<h3>mockConfig</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockConfig <span style="color: #ff0000;">&quot;&quot;&quot;
    grails.foo.bar='baz'
&quot;&quot;&quot;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grailsApplication.<span style="color: #006600;">config</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">foo</span>.<span style="color: #006600;">bar</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'baz'</span></pre></div></div>

<h3>mockParams</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockParams.<span style="color: #006600;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">params.<span style="color: #006600;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span></pre></div></div>

<p>So you can replace all <code>mockParams</code> with just <code>params</code>.</p>
<h3>mockFlash</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #ff0000;">&quot;Expected Message&quot;</span>, mockFlash.<span style="color: #006600;">error</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;Expected Message&quot;</span> <span style="color: #66cc66;">==</span> flash.<span style="color: #006600;">error</span></pre></div></div>

<p>So you can replace all <code>mockFlash</code> with just <code>flash</code>.</p>
<h3>renderArgs and redirectArgs</h3>
<p>The very useful <code>renderArgs</code> and <code>redirectArgs</code> mock objects are gone in grails 2.0 and I find the new way of testing without them to be harder than it was.</p>
<h3>renderArgs.view</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">'edit'</span> <span style="color: #66cc66;">==</span> renderArgs.<span style="color: #006600;">view</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> view.<span style="color: #006600;">endsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/edit'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>redirectArgs</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #ff0000;">'person'</span>, redirectArgs.<span style="color: #006600;">controller</span>
assertEquals <span style="color: #ff0000;">'show'</span>, redirectArgs.<span style="color: #006600;">action</span>
assertEquals personInstance.<span style="color: #006600;">id</span>, redirectArgs.<span style="color: #006600;">id</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> response.<span style="color: #006600;">redirectUrl</span>.<span style="color: #006600;">endsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/person/show/${personInstance.id}&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>(or just use <code>contains</code> or <code>.split('/')</code> to find parts of the url)</p>
<h3>renderArgs.model</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> renderArgs.<span style="color: #006600;">model</span>.<span style="color: #006600;">person</span>.<span style="color: #006600;">id</span> <span style="color: #66cc66;">==</span> person.<span style="color: #006600;">id</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> model.<span style="color: #006600;">person</span>.<span style="color: #006600;">id</span> <span style="color: #66cc66;">==</span> person.<span style="color: #006600;">id</span></pre></div></div>

<p>This seems to work unless you&#8217;re rendering a template in your controller rather than a view. In that case, the model isn&#8217;t populated and cannot be accessed as far as I can tell.  This is documented in <a href="http://jira.grails.org/browse/GRAILS-8659" onclick="pageTracker._trackPageview('/outgoing/jira.grails.org/browse/GRAILS-8659?referer=');">GRAILS-8659</a> which is marked as &#8220;Not a Bug&#8221;, something I disagree with as the workaround is much more painful.</p>
<p>The workaround, per Graeme Rocher on that ticket is to mock the template rendering:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">views<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'/test/_bar.gsp'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Hello ${name}'</span>
controller.<span style="color: #006600;">renderTemplate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> response.<span style="color: #006600;">contentAsString</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;Hello John&quot;</span></pre></div></div>

<p>Where controller:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> renderTemplate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    render template:<span style="color: #ff0000;">&quot;bar&quot;</span>, model:<span style="color: #66cc66;">&#91;</span>name:<span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The naming convention of what you set in the map should be in the format &#8220;/person/_editAddress.gsp&#8221;, where you&#8217;re in the <code>PersonController</code> and the template name is <code>editAddress</code> but there were a few situations where I had trouble getting this right.</p>
<p>If you want to be sure what value to set the path to in the <code>views</code> map, set a breakpoint in <code>RenderDynamicMethod.java</code> around like 319 where it does this check:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #aaaadd; font-weight: bold;">String</span> templateUri <span style="color: #66cc66;">=</span> webRequest.<span style="color: #006600;">getAttributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTemplateURI</span><span style="color: #66cc66;">&#40;</span>controller, templateName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>The value of that <code>templateUri</code> is what you want to set in your <code>views</code> map.</p>
<h3>redirectArgs.params</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">==</span> redirectArgs.<span style="color: #006600;">params</span>.<span style="color: #006600;">foo</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// strip off everything up till the ? that marks the start of the query string</span>
<span style="color: #000000; font-weight: bold;">def</span> params <span style="color: #66cc66;">=</span> WebUtils.<span style="color: #006600;">fromQueryString</span><span style="color: #66cc66;">&#40;</span>response.<span style="color: #006600;">redirectUrl</span>.<span style="color: #663399;">find</span><span style="color: #66cc66;">&#40;</span>/<span style="color: #66cc66;">&#91;</span>^<span style="color: #66cc66;">?</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">*</span>$/<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">==</span> params.<span style="color: #006600;">foo</span></pre></div></div>

<p>gross.  This really should be better and usability has regressed on this.</p>
<h3>mockResponse</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #cc66cc;">404</span>, mockResponse.<span style="color: #006600;">status</span>
assertEquals <span style="color: #ff0000;">&quot;foo bar baz&quot;</span>, mockResponse.<span style="color: #006600;">contentAsString</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #cc66cc;">404</span> <span style="color: #66cc66;">==</span> response.<span style="color: #006600;">status</span>
assertEquals <span style="color: #ff0000;">&quot;foo bar baz&quot;</span>, response.<span style="color: #006600;">contentAsString</span></pre></div></div>

<p>So you should be able to find/replace <code>mockResponse</code> with just <code>response</code></p>
<h3>mockLogging</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockLogging<span style="color: #66cc66;">&#40;</span>Foo<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>grails 2.0.x:</p>
<p>In many cases, this can simply be deleted.  If it&#8217;s either the class under test (the class you are doing a <code>@TestFor</code> on) or something you&#8217;ve already got in a <code>@Mock</code> or a <code>@Build</code> class level annotation, it already has logging.</p>
<p>For those classes that need logging, but are not already mocked, you can use the <code>grails.test.MockUtils.mockLogging</code> static method.  Just add this static import to your class to get the same behavior:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">static</span> grails.<span style="color: #006600;">test</span>.<span style="color: #006600;">MockUtils</span>.<span style="color: #66cc66;">*</span>
    ...
    <span style="color: #006600;">mockLogging</span><span style="color: #66cc66;">&#40;</span>MyService<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If it&#8217;s a collaborator that isn&#8217;t already mocked out, you can use the <code>mockFor</code> method as described in the <a href="http://grails.org/doc/latest/guide/testing.html#mockingCollaborators" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_mockingCollaborators?referer=');">mocking collaborators</a> section of the grails user docs.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> foo <span style="color: #66cc66;">=</span> mockFor<span style="color: #66cc66;">&#40;</span>Foo, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// true here is &quot;loose&quot; mocking, otherwise you need to `demand` method calls for them to not fail.</span></pre></div></div>

<p>Overall, I&#8217;m much happier with grails 2.0.  It has a number of great features and speed improvements that make it a worthwhile upgrade.  I complain above about a few places where I think testing is not as easy as it was, but most of these could be remedied with a few patches to grails-core.  </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating New Instances of Spring &#8220;Singleton&#8221; Beans with Grails BeanBuilder</title>
		<link>http://naleid.com/blog/2011/03/07/creating-new-instances-of-spring-singleton-beans-with-grails-beanbuilder/</link>
		<comments>http://naleid.com/blog/2011/03/07/creating-new-instances-of-spring-singleton-beans-with-grails-beanbuilder/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 05:11:41 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=301</guid>
		<description><![CDATA[When I&#8217;m integration testing Grails service classes, I often want to mock off a part of the class so that a complicated code branch isn&#8217;t followed that I&#8217;m not trying to test. Grails will helpfully inject fully autowired Spring service beans into my test if I ask for them. Unfortunately, if I change the metaClass [...]]]></description>
			<content:encoded><![CDATA[<p>When I&#8217;m integration testing Grails service classes, I often want to mock off a part of the class so that a complicated code branch isn&#8217;t followed that I&#8217;m not trying to test.</p>
<p>Grails will helpfully inject fully autowired Spring service beans into my test if I ask for them.  Unfortunately, if I change the metaClass of the injected service, that change persists beyond where we want it to:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyService <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> myMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">&quot;unmodified&quot;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyServiceTests <span style="color: #000000; font-weight: bold;">extends</span> GroovyTestCase <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> myService  <span style="color: #808080; font-style: italic;">// injected automatically by spring/grails</span>
&nbsp;
    <span style="color: #993333;">void</span> testOne<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        myService.<span style="color: #006600;">metaClass</span>.<span style="color: #006600;">myMethod</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">-&gt;</span> <span style="color: #ff0000;">&quot;modified&quot;</span> <span style="color: #66cc66;">&#125;</span>
        assertEquals <span style="color: #ff0000;">&quot;modified&quot;</span>, myService.<span style="color: #006600;">myMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span> 
&nbsp;
    <span style="color: #993333;">void</span> testTwo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        assertEquals  <span style="color: #ff0000;">&quot;unmodified&quot;</span>, myService.<span style="color: #006600;">myMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// WTF!  Returns &quot;modified&quot;, pollution from first test</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Grails services are Spring &#8220;singleton&#8221; objects.  They&#8217;re not true singletons though, singleton&#8217;s are just cached in the application context and returned whenever <code>getBean</code> is called.  </p>
<p>Historically, if I wanted to mock part of my service manually, I&#8217;d need to &#8220;new&#8221; up my own instance of the service and manually inject any dependencies that the service might need to function.   This is both painful and fragile, if the service adds or removes dependencies, chances are that the tests are going to break.</p>
<p>I realized that if I could ask Grails/Spring for a new instance of the &#8220;singleton&#8221; service that I could muck with it all I wanted in my test without worrying about polluting other tests with my changes.   After some digging into the grails spring support, I came up with the following method that could be added to an integration test (or integration test base class):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// spring &quot;singleton&quot; objects really aren't they're just cached by their application context</span>
<span style="color: #000000; font-weight: bold;">def</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> beanName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;prototype${clazz.name}&quot;</span>
    BeanBuilder beanBuilder <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BeanBuilder<span style="color: #66cc66;">&#40;</span>ApplicationHolder.<span style="color: #006600;">application</span>.<span style="color: #006600;">mainContext</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    beanBuilder.<span style="color: #006600;">beans</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #ff0000;">&quot;$beanName&quot;</span><span style="color: #66cc66;">&#40;</span>clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> bean <span style="color: #66cc66;">-&gt;</span>
            bean.<span style="color: #006600;">autowire</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'byName'</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    beanBuilder.<span style="color: #006600;">createApplicationContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getBean</span><span style="color: #66cc66;">&#40;</span>beanName<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This method uses the BeanBuilder to construct a temporary ApplicationContext with the Grails mainContext as a parent so that other dependencies can be resolved.   This method won&#8217;t work if your service has changes to how it&#8217;s wired up and configured by spring, but the majority of Grails service classes are simply autowired <code>byName</code>.</p>
<p>Here&#8217;s a more detailed example of use.  Given this service:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyService <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> injectedService
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> serviceMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> otherMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> otherMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">&quot;original value&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I&#8217;m able to generate per-test autowired instances of my service and mock out <code>otherMethod</code> without polluting other tests (or the Spring injected version of the bean):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.spring.BeanBuilder</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.codehaus.groovy.grails.commons.ApplicationHolder</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyServiceTests <span style="color: #000000; font-weight: bold;">extends</span> GroovyTestCase <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> myServiceInstance  <span style="color: #808080; font-style: italic;">// our new</span>
    <span style="color: #000000; font-weight: bold;">def</span> myService <span style="color: #808080; font-style: italic;">// spring injected version</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">setUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        myServiceInstance <span style="color: #66cc66;">=</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span>MyService<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> tearDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">tearDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// spring &quot;singleton&quot; objects really aren't they're just cached by their application context</span>
    <span style="color: #000000; font-weight: bold;">def</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">String</span> beanName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;prototype${clazz.name}&quot;</span>
        BeanBuilder beanBuilder <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BeanBuilder<span style="color: #66cc66;">&#40;</span>ApplicationHolder.<span style="color: #006600;">application</span>.<span style="color: #006600;">mainContext</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        beanBuilder.<span style="color: #006600;">beans</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #ff0000;">&quot;$beanName&quot;</span><span style="color: #66cc66;">&#40;</span>clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> bean <span style="color: #66cc66;">-&gt;</span>
                bean.<span style="color: #006600;">autowire</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'byName'</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        beanBuilder.<span style="color: #006600;">createApplicationContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getBean</span><span style="color: #66cc66;">&#40;</span>beanName<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> testNewSingletonInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        assertNotNull myServiceInstance <span style="color: #808080; font-style: italic;">// created uniquely for this test in setUp</span>
        assertNotNull myService         <span style="color: #808080; font-style: italic;">// spring injected into integration test</span>
&nbsp;
        assertNotSame myService, myServiceInstance
&nbsp;
        <span style="color: #808080; font-style: italic;">// we've got unique instances of MyService, but both are injected with the same singleton dependencies</span>
        assertSame myService.<span style="color: #006600;">injectedService</span>, myServiceInstance.<span style="color: #006600;">injectedService</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> testMessingWithMetaClassDoesNotAffectOriginalSingleton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        myServiceInstance.<span style="color: #006600;">metaClass</span>.<span style="color: #006600;">otherMethod</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">-&gt;</span> <span style="color: #ff0000;">&quot;new value&quot;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
        assertEquals <span style="color: #ff0000;">&quot;new value&quot;</span>, myServiceInstance.<span style="color: #006600;">serviceMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        assertEquals <span style="color: #ff0000;">&quot;original value&quot;</span>, myService.<span style="color: #006600;">serviceMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">def</span> anotherInstance <span style="color: #66cc66;">=</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span>MyService<span style="color: #66cc66;">&#41;</span>
&nbsp;
        assertEquals <span style="color: #ff0000;">&quot;original value&quot;</span>, anotherInstance.<span style="color: #006600;">serviceMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Using this technique lets you leverage Spring&#8217;s autowiring in your tests, but also gives you the flexibility to override areas not under test to improve test readability and maintainability.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/03/07/creating-new-instances-of-spring-singleton-beans-with-grails-beanbuilder/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Adding Logging around all of the Methods of a Class with Groovy</title>
		<link>http://naleid.com/blog/2010/09/11/adding-logging-around-all-of-the-methods-of-a-class-with-groovy/</link>
		<comments>http://naleid.com/blog/2010/09/11/adding-logging-around-all-of-the-methods-of-a-class-with-groovy/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 19:09:12 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=213</guid>
		<description><![CDATA[An interesting question came up on stack overflow, &#8220;In Groovy Is there a way to decorate every class to add tracing&#8221;. I came up with the following solution using groovy&#8217;s invokeMethod. The invokeMethod method gets called by groovy&#8217;s MOP for every method call that happens on an object. If we add our own version, we [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting question came up on stack overflow, <a href="http://stackoverflow.com/questions/3691933/in-groovy-is-there-a-way-to-decorate-every-class-to-add-tracing/3692250" onclick="pageTracker._trackPageview('/outgoing/stackoverflow.com/questions/3691933/in-groovy-is-there-a-way-to-decorate-every-class-to-add-tracing/3692250?referer=');">&#8220;In Groovy Is there a way to decorate every class to add tracing&#8221;</a>.  I came up with the following solution using groovy&#8217;s invokeMethod.</p>
<p>The invokeMethod method gets called by groovy&#8217;s MOP for every method call that happens on an object.  If we add our own version, we need to make sure we keep a reference to the original metaClass so that within our invokeMethod, we can still get access to the other methods that we want to delegate to.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;in bar&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> baz<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;in baz with $name&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> decorateMethodsWithLogging<span style="color: #66cc66;">&#40;</span>clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> mc <span style="color: #66cc66;">=</span> clazz.<span style="color: #006600;">metaClass</span>
&nbsp;
    mc.<span style="color: #993399;">invokeMethod</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #aaaadd; font-weight: bold;">String</span> name, args <span style="color: #66cc66;">-&gt;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;before $name, args = $args&quot;</span>
        <span style="color: #000000; font-weight: bold;">def</span> result <span style="color: #66cc66;">=</span> mc.<span style="color: #006600;">getMetaMethod</span><span style="color: #66cc66;">&#40;</span>name, args<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span>delegate, args<span style="color: #66cc66;">&#41;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;after $name&quot;</span>
        <span style="color: #000000; font-weight: bold;">return</span> result
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
decorateMethodsWithLogging<span style="color: #66cc66;">&#40;</span>Foo.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> f <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
f.<span style="color: #006600;">bar</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
f.<span style="color: #006600;">baz</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;qux&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>prints</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">before bar, args <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #b1b100;">in</span> bar
after bar
before baz, args <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>qux<span style="color: #66cc66;">&#93;</span>
<span style="color: #b1b100;">in</span> baz with qux
after baz</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2010/09/11/adding-logging-around-all-of-the-methods-of-a-class-with-groovy/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Running Grails Unit and Integration Tests in Parallel</title>
		<link>http://naleid.com/blog/2010/08/26/running-grails-unit-and-integration-tests-in-parallel/</link>
		<comments>http://naleid.com/blog/2010/08/26/running-grails-unit-and-integration-tests-in-parallel/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 03:31:31 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=205</guid>
		<description><![CDATA[The number of tests on my current project is getting close to the 2000 mark (1504 unit and 351 integration) and running the full suite can take 4+ minutes to execute. When you&#8217;re trying to do TDD and want to run all tests before pushing your code out to the shared repository, this length of [...]]]></description>
			<content:encoded><![CDATA[<p>The number of tests on my current project is getting close to the 2000 mark (1504 unit and 351 integration) and running the full suite can take 4+ minutes to execute.</p>
<p>When you&#8217;re trying to do TDD and want to run all tests before pushing your code out to the shared repository, this length of time can really bog you down when it happens a number of times per day.</p>
<p>There are a number of tests that we need to go back in and refactor to be faster, but I wanted to see how much of a boost I could get by running unit and integration tests in parallel.  </p>
<p>With 2 threads (one for unit, one for integration) the best case would be taking 50% of the time that it takes to run serially.  That would only happen if my unit and integration tests took the same amount of time to run.  For my tests, I was able to get a 39% speed improvement (with the integration tests taking a bit longer than the unit tests).</p>
<p>Here&#8217;s the script:<br />
<span id="more-205"></span><br />
(just save it as splitTests.groovy in your path and make it executable, then run it in your grails project dir)</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env groovy</span>
&nbsp;
<span style="color: #aaaadd; font-weight: bold;">Map</span> exitStatuses <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>.<span style="color: #663399;">asSynchronized</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> currentDir<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">'pwd'</span>.<span style="color: #993399; font-weight: bold;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span>.<span style="color: #006600;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> getGrailsPropertyArgs<span style="color: #66cc66;">&#40;</span>group<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">def</span> testDir <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;${currentDir()}/test-$group&quot;</span>
	<span style="color: #66cc66;">&#91;</span>
	    <span style="color: #ff0000;">&quot;-Dgrails.work.dir=${testDir}&quot;</span>,
	    <span style="color: #ff0000;">&quot;-Dgrails.project.class.dir=${testDir}/classes&quot;</span>,
	    <span style="color: #ff0000;">&quot;-Dgrails.project.test.class.dir=${testDir}/test-classes&quot;</span>,
	    <span style="color: #ff0000;">&quot;-Dgrails.project.test.reports.dir=${testDir}/test-reports&quot;</span>
	<span style="color: #66cc66;">&#93;</span>.<span style="color: #663399;">join</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">synchronized</span> out<span style="color: #66cc66;">&#40;</span>group, message<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #993399;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;${group.padLeft(12, ' ')}: $message&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#91;</span>
        integration: <span style="color: #ff0000;">'integration:'</span>,
        unit: <span style="color: #ff0000;">'unit:'</span>
<span style="color: #66cc66;">&#93;</span>.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> testGroup, args <span style="color: #66cc66;">-&gt;</span>
	<span style="color: #aaaadd; font-weight: bold;">Thread</span>.<span style="color: #993399;">start</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">def</span> command <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;grails ${getGrailsPropertyArgs(testGroup)} test-app $args&quot;</span>
		out testGroup, command
		exitStatuses<span style="color: #66cc66;">&#91;</span>testGroup<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> command.<span style="color: #993399; font-weight: bold;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">with</span> <span style="color: #66cc66;">&#123;</span> proc <span style="color: #66cc66;">-&gt;</span>
			proc.<span style="color: #b1b100;">in</span>.<span style="color: #FFCC33;">eachLine</span> <span style="color: #66cc66;">&#123;</span> line <span style="color: #66cc66;">-&gt;</span> out testGroup, line <span style="color: #66cc66;">&#125;</span>
			proc.<span style="color: #006600;">waitFor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			proc.<span style="color: #006600;">exitValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
		out testGroup, <span style="color: #ff0000;">&quot;exit value: ${exitStatuses[testGroup]}&quot;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #663399;">join</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> failingGroups <span style="color: #66cc66;">=</span> exitStatuses.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">value</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>failingGroups<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;All tests were successful!&quot;</span>
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
	failingGroups.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> failingGroup, exitStatus <span style="color: #66cc66;">-&gt;</span>
		out<span style="color: #66cc66;">&#40;</span>failingGroup, <span style="color: #ff0000;">&quot;WARNING: '$failingGroup' exit status was $exitStatus&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">exit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The script is pretty simple.  It creates one thread for unit tests, and one for integration tests.  It then creates a grails test-app command that includes a number of grails system properties necessary to define unique target and working directories so the threads don&#8217;t step all over each other.</p>
<p>As the threads run, they spit their output to the command line with a prefix on each line showing which thread it came from, ex:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">...
 <span style="color: #006600;">integration</span>: Running test com.<span style="color: #006600;">bloomhealthco</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">ProductTests</span>...<span style="color: #006600;">PASSED</span>
        unit: Running test com.<span style="color: #006600;">bloomhealthco</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">QuestionUnitTests</span>...<span style="color: #006600;">PASSED</span>
 integration: Running test com.<span style="color: #006600;">bloomhealthco</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">TransactionalFalseTests</span>...<span style="color: #006600;">PASSED</span>
        unit: Running test com.<span style="color: #006600;">bloomhealthco</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">RateAreaUnitTests</span>...<span style="color: #006600;">PASSED</span>
        unit: Running test com.<span style="color: #006600;">bloomhealthco</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">RateDependentTypeUnitTests</span>...<span style="color: #006600;">PASSED</span>
 integration: Running test com.<span style="color: #006600;">bloomhealthco</span>.<span style="color: #006600;">filter</span>.<span style="color: #006600;">EmployerFiltersTests</span>...<span style="color: #006600;">PASSED</span>
        unit: Running test com.<span style="color: #006600;">bloomhealthco</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">RateUnitTests</span>...<span style="color: #006600;">PASSED</span>
...</pre></div></div>

<p>When it&#8217;s done, it checks the exit status code of each individual run.  If any failed, the splitTests script will enumerate the ones that failed and will also return a non-zero exit status code.</p>
<p>The first time you run this, it will probably be slow, as each thread is compiling it&#8217;s own set of class files into it&#8217;s own &#8220;target&#8221; directory.  After the initial compilation, things should be a lot quicker for the 2nd run.</p>
<p>If you&#8217;ve only got a couple of tests, you likely won&#8217;t see any speedup from this as the fixed grails cost for boostrapping the environment in each thread will outweigh the parallelization of tests.</p>
<p>If you&#8217;ve got other types of tests that you&#8217;re running, it&#8217;s easy to just add other phases into the existing threads (or create new threads for them), just by adding to the map that we spin through.</p>
<p>I&#8217;ll probably dig into the grails internals some more to see if some of it can be refactored to make running tests in parallel easier.  The latest versions of JUnit have some support for running tests in parallel, but it isn&#8217;t easily achieved with the current grails test/environment bootstrap.   </p>
<p>My long term goal is to create a grails script that lets the user specify a number of worker threads to be used for testing.  Those workers are then fed test classes to chew on and the load would be distributed better than it is currently.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2010/08/26/running-grails-unit-and-integration-tests-in-parallel/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Grails Testing Alias to Rerun Failed Tests</title>
		<link>http://naleid.com/blog/2009/11/03/grails-testing-alias-to-rerun-failed-tests/</link>
		<comments>http://naleid.com/blog/2009/11/03/grails-testing-alias-to-rerun-failed-tests/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 03:29:58 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[zshrc]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=99</guid>
		<description><![CDATA[A while ago I blogged about my grails testing aliases and how much time they save me. I&#8217;ve made some enhancements to them in the interim that have made them even easier to use. The most important alias is gtaf, which is short for &#8220;grails test-app&#8221; for failed tests. It will search through your test [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I <a href="http://naleid.com/blog/2009/06/14/grails-testing-command-line-aliases/">blogged about my grails testing aliases</a> and how much time they save me.</p>
<p>I&#8217;ve made some enhancements to them in the interim that have made them even easier to use.  </p>
<p>The most important alias is <code>gtaf</code>, which is short for &#8220;grails test-app&#8221; for failed tests.</p>
<p>It will search through your test output directory and look for any tests that failed.  If it finds any, it will rerun only those tests.  Otherwise, it will rerun all tests.  That makes it easy to just use <code>gtaf</code> all the time.  If any tests fail, it will open them up using Console.app.  </p>
<p>If you&#8217;re not on OSX, or would like to use something else to view the failed test logs, just modify the <code>testlog</code> alias to do something different.<br />
<span id="more-99"></span><br />
There are alternative versions of it to only run failed integration tests (<code>gtaif</code> &#8211; Grails Test App Integration Failed), failed unit tests (<code>gtauf</code> &#8211; Grails Test App Unit Failed) or attach a debugger to the tests as they run (<code>gtadf</code> &#8211; Grails Test App Debug Failed).</p>
<p>These aliases, plus most of the rest of my zsh setup, is <a href="http://bitbucket.org/tednaleid/shared-zshrc/src/" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/shared-zshrc/src/?referer=');">available on bitbucket</a>.  If you haven&#8217;t used zsh before and are still using bash, I suggest <a href="http://naleid.com/blog/2009/05/13/shared-zshrc-file/">switching over for the numerous benefits that it gives you</a>.</p>
<p>Here&#8217;s the section of zshrc_general that has the grails testing aliases, just add this to your .zshrc/.bashrc and make sure to uncomment the appropriate GRAILS_TEST_LOG_DIRECTORY export if you&#8217;re not running grails 1.2 yet:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># grails &gt; 1.2</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_TEST_LOG_DIRECTORY</span>=target<span style="color: #000000; font-weight: bold;">/</span>test-reports
&nbsp;
<span style="color: #666666; font-style: italic;"># grails &lt; 1.2</span>
<span style="color: #666666; font-style: italic;"># export GRAILS_TEST_LOG_DIRECTORY=test/reports</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># after grails-test if there were ERROR messages, you can open those logs with the Console.app using this</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">testlog</span>=<span style="color: #ff0000;">'for F in `grep -lE &quot;FAILED|Caused\ an\ ERROR&quot; $GRAILS_TEST_LOG_DIRECTORY/plain/*.txt`; do echo &quot;&gt;&gt;&gt; opening&quot; $F; open -a Console $F; done;'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># grails-debug-suspend doesn't exist by default, it's a copy of grails-debug with the suspend flag changed to &quot;y&quot; so that</span>
<span style="color: #666666; font-style: italic;"># we can attach a remote debugger before it proceeds past startup</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># aliases where you can optionally pass in a set of tests to run (or no argument to run all tests in that group)</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gta</span>=grailsTestApp
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtad</span>=grailsTestAppDebug
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtau</span>=grailsTestAppUnit
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaud</span>=grailsTestAppUnitDebug
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtai</span>=grailsTestAppIntegration
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaid</span>=grailsTestAppIntegrationDebug
&nbsp;
<span style="color: #666666; font-style: italic;"># aliases that will rerun any failed tests (or all tests if there aren't any failed tests)</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaf</span>=grailsTestAppFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtadf</span>=grailsTestAppDebugFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtauf</span>=grailsTestAppUnitFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaudf</span>=grailsTestAppUnitDebugFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaif</span>=grailsTestAppIntegrationFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaidf</span>=grailsTestAppIntegrationDebugFailed
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestApp<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppDebugFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails-debug-suspend <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnit<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #660033;">-unit</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnitFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails <span style="color: #660033;">-unit</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnitDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #660033;">-unit</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnitDebugFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails-debug-suspend <span style="color: #660033;">-unit</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegration<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #660033;">-integration</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegrationFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails <span style="color: #660033;">-integration</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegrationDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #660033;">-integration</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegrationDebugFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails-debug-suspend <span style="color: #660033;">-integration</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsFailedTests<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #007800;">FAILED_TESTS</span>=<span style="color: #ff0000;">''</span>
	<span style="color: #000000; font-weight: bold;">for</span> F <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-lE</span> <span style="color: #ff0000;">&quot;FAILED|Caused\ an\ ERROR&quot;</span> <span style="color: #007800;">$GRAILS_TEST_LOG_DIRECTORY</span><span style="color: #000000; font-weight: bold;">/</span>plain<span style="color: #000000; font-weight: bold;">/*</span>.txt<span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
		<span style="color: #007800;">FAILED_TESTS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$FAILED_TESTS</span> <span style="color: #780078;">`echo $F | sed -E 's/.*TEST-(.*)Tests.txt/\1/g'`</span>&quot;</span>
	<span style="color: #000000; font-weight: bold;">done</span>;
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed tests: <span style="color: #007800;">$FAILED_TESTS</span>&quot;</span>
	grailsTest <span style="color: #007800;">$1</span> <span style="color: #007800;">$2</span> <span style="color: #007800;">$FAILED_TESTS</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTest<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Running: $1 test-app $2 $3 || testlog &quot;</span>
    <span style="color: #000000; font-weight: bold;">time</span> <span style="color: #007800;">$1</span> test-app <span style="color: #007800;">$2</span> <span style="color: #007800;">$3</span> <span style="color: #000000; font-weight: bold;">||</span> testlog
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>I believe that they&#8217;ll also work fine as-is in bash, though I haven&#8217;t tested them.</p>
<p><script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/11/03/grails-testing-alias-to-rerun-failed-tests/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Grails build-test-data presentation</title>
		<link>http://naleid.com/blog/2009/07/14/grails-build-test-data-presentation/</link>
		<comments>http://naleid.com/blog/2009/07/14/grails-build-test-data-presentation/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 03:28:56 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=59</guid>
		<description><![CDATA[I gave a presentation tonight on the build-test-data grails plugin at the Groovy Users of Minnesota (GUM) meeting that was well received. Lots of good questions from the people in attendance. Thanks to everyone for showing up. Here&#8217;s a version of it on slideshow: Grails build-test-data Plugin View more documents from tednaleid. I also released [...]]]></description>
			<content:encoded><![CDATA[<p>I gave a presentation tonight on the <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/Home" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/Home?referer=');">build-test-data</a> grails plugin at the <a href="http://groovy.mn" onclick="pageTracker._trackPageview('/outgoing/groovy.mn?referer=');">Groovy Users of Minnesota (GUM)</a> meeting that was well received.</p>
<p>Lots of good questions from the people in attendance.  Thanks to everyone for showing up.</p>
<p>Here&#8217;s a version of it on slideshow:<span id="more-59"></span></p>
<div style="width:650px;text-align:left" id="__ss_1723277"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/tednaleid/grails-buildtestdata-plugin-1723277" title="Grails build-test-data Plugin" onclick="pageTracker._trackPageview('/outgoing/www.slideshare.net/tednaleid/grails-buildtestdata-plugin-1723277?referer=');">Grails build-test-data Plugin</a><object style="margin:0px" width="650" height="547"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=build-test-datapresentation-090714222053-phpapp01&#038;stripped_title=grails-buildtestdata-plugin-1723277" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=build-test-datapresentation-090714222053-phpapp01&#038;stripped_title=grails-buildtestdata-plugin-1723277" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="650" height="547"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/" onclick="pageTracker._trackPageview('/outgoing/www.slideshare.net/?referer=');">documents</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/tednaleid" onclick="pageTracker._trackPageview('/outgoing/www.slideshare.net/tednaleid?referer=');">tednaleid</a>.</div>
</div>
<p>I also released a new bugfix version of build-test-data earlier today (0.2.2).  Mostly fixed bugs, but there&#8217;s also a new config setting that lets you disable the plugin in specified environments (suggested by Scott Vlaminck @ <a href="http://refactr.com"/>refactr</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/07/14/grails-build-test-data-presentation/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Groovy closures make unit testing with &#8220;soft asserts&#8221; simple</title>
		<link>http://naleid.com/blog/2009/06/25/groovy-closures-make-unit-testing-with-soft-asserts-simple/</link>
		<comments>http://naleid.com/blog/2009/06/25/groovy-closures-make-unit-testing-with-soft-asserts-simple/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 04:25:45 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=58</guid>
		<description><![CDATA[A recent blog post, Cedric Beust asks about how to cleanly implement &#8220;soft asserts&#8221;. Soft asserts are test assertions that don&#8217;t &#8220;fail fast&#8221;. Instead, all of the assertion failures in the test method are collected and reported at the end of the test. So far, the proposals in the comments look fairly clunky to me [...]]]></description>
			<content:encoded><![CDATA[<p>A recent blog post, <a href="http://beust.com/weblog/archives/000514.html" onclick="pageTracker._trackPageview('/outgoing/beust.com/weblog/archives/000514.html?referer=');">Cedric Beust asks about how to cleanly implement &#8220;soft asserts&#8221;</a>.  Soft asserts are test assertions that don&#8217;t &#8220;fail fast&#8221;.  Instead, all of the assertion failures in the test method are collected and reported at the end of the test.</p>
<p>So far, the proposals in the comments look fairly clunky to me and include defining whole sets of new methods like &#8220;assertEqualsButContinue(&#8220;foo&#8221;, &#8220;foo&#8221;)&#8221;, chaning assertions together jQuery-style, or using lists to hold all of our assertions.</p>
<p>Wouldn&#8217;t it be a lot nicer if we could continue to use the same methods that we&#8217;re already using?</p>
<p>In groovy, enabling soft assertions is easy with a little closure magic.<span id="more-58"></span></p>
<p>Closures have a property called the &#8220;resolveStrategy&#8221; that determines where the code inside the closure should look to resolve method names.  The default resolve strategy is OWNER_FIRST.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">Closure closure <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> closure.<span style="color: #006600;">getResolveStrategy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> Closure.<span style="color: #006600;">OWNER_FIRST</span></pre></div></div>

<p>OWNER_FIRST means that if something isn&#8217;t defined inside the closure (local scope), it should check the owner of the closure to see if it can resolve it.  The owner of the closure is the declaring class or another closure that contains our closure.</p>
<p>Closures also have a property called the &#8220;delegate&#8221;.  By default, the delegate is equal to the owner, but we have the ability to change the delegate to something else.  We can also change the resolveStrategy for the closure so when something isn&#8217;t defined, the delegate is the first place that is checked.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Original <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">def</span> closure <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>   
&nbsp;
   <span style="color: #000000; font-weight: bold;">def</span> value <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">&quot;value from owner&quot;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SomethingElse <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> value <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">&quot;value from delegate&quot;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> obj <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Original<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;value from owner&quot;</span> <span style="color: #66cc66;">==</span> obj.<span style="color: #006600;">closure</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// the owner is the enclosing class, and its the same as the delegate by default</span>
<span style="color: #000000; font-weight: bold;">assert</span> obj.<span style="color: #006600;">closure</span>.<span style="color: #006600;">owner</span>.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #66cc66;">==</span> Original
<span style="color: #000000; font-weight: bold;">assert</span> obj.<span style="color: #006600;">closure</span>.<span style="color: #006600;">owner</span> <span style="color: #66cc66;">==</span> obj.<span style="color: #006600;">closure</span>.<span style="color: #006600;">delegate</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// but we can change the closure's delegate to something else if we want</span>
obj.<span style="color: #006600;">closure</span>.<span style="color: #006600;">delegate</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SomethingElse<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// by default, the closure looks at the owner before the delegate, value is unchanged</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;value from owner&quot;</span> <span style="color: #66cc66;">==</span> obj.<span style="color: #006600;">closure</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #000000; font-weight: bold;">assert</span> obj.<span style="color: #006600;">closure</span>.<span style="color: #006600;">getResolveStrategy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> Closure.<span style="color: #006600;">OWNER_FIRST</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// changing the closure's resolve strategy switches who gets to handle value()</span>
obj.<span style="color: #006600;">closure</span>.<span style="color: #006600;">resolveStrategy</span> <span style="color: #66cc66;">=</span> Closure.<span style="color: #006600;">DELEGATE_FIRST</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;value from delegate&quot;</span> <span style="color: #66cc66;">==</span> obj.<span style="color: #006600;">closure</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Groovy also has a <a href="http://groovy.codehaus.org/Using+invokeMethod+and+getProperty" onclick="pageTracker._trackPageview('/outgoing/groovy.codehaus.org/Using+invokeMethod+and+getProperty?referer=');">special metaClass method called invokeMethod</a> that allows you to manipulate any method calls that are invoked on that metaClass.</p>
<p>Using these two things in conjunction, we can write a small class that lets us collect JUnit assertion errors thrown by any method starting with &#8220;assert&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> SoftAsserts <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> oldDelegate
    <span style="color: #000000; font-weight: bold;">def</span> failedAssertions <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> softAsserts<span style="color: #66cc66;">&#40;</span>closure<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> SoftAsserts<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bundleAsserts</span><span style="color: #66cc66;">&#40;</span>closure<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> bundleAsserts <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> closure <span style="color: #66cc66;">-&gt;</span>
        closure.<span style="color: #006600;">resolveStrategy</span> <span style="color: #66cc66;">=</span> Closure.<span style="color: #006600;">DELEGATE_ONLY</span>
        oldDelegate <span style="color: #66cc66;">=</span> closure.<span style="color: #006600;">delegate</span>
        closure.<span style="color: #006600;">delegate</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">this</span>
        closure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>   
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>failedAssertions<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Exception</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;${failedAssertions.size()} failed assertions found:<span style="color: #000099; font-weight: bold;">\n</span>${failedAssertions.message.join('<span style="color: #000099; font-weight: bold;">\n</span>')}&quot;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> <span style="color: #993399;">invokeMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> name, args<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>name.<span style="color: #006600;">startsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;assert&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> oldDelegate.<span style="color: #993399;">invokeMethod</span><span style="color: #66cc66;">&#40;</span>name, args<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>junit.<span style="color: #006600;">framework</span>.<span style="color: #006600;">AssertionFailedError</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                failedAssertions <span style="color: #66cc66;">&lt;&lt;</span> e
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> oldDelegate.<span style="color: #993399;">invokeMethod</span><span style="color: #66cc66;">&#40;</span>name, args<span style="color: #66cc66;">&#41;</span>         
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This test class does a static import of the SoftAsserts.softAsserts closure so it can use it in a natural way.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.test.*</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">static</span> SoftAsserts.<span style="color: #006600;">softAsserts</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BookTests <span style="color: #000000; font-weight: bold;">extends</span> GroovyTestCase <span style="color: #66cc66;">&#123;</span>
    <span style="color: #993333;">void</span> testBookTrueAssertsPasses<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> book <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>title: <span style="color: #ff0000;">&quot;Infinite Jest&quot;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// all 4 assertions pass and we are able to refer to local variables </span>
        <span style="color: #808080; font-style: italic;">// like book and local methods like trueMethod() </span>
        softAsserts <span style="color: #66cc66;">&#123;</span> 
            assertTrue <span style="color: #000000; font-weight: bold;">true</span>
            assertTrue trueMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            assertNotNull book
            assertEquals <span style="color: #ff0000;">&quot;Infinite Jest&quot;</span>, book.<span style="color: #006600;">title</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> testBookFalseAssertsCollectsFailures<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> book <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>title: <span style="color: #ff0000;">&quot;Infinite Jest&quot;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// all 4 assertions throw errors, but all are collected before </span>
        <span style="color: #808080; font-style: italic;">// an exception is actually thrown</span>
        softAsserts <span style="color: #66cc66;">&#123;</span> 
            assertTrue <span style="color: #ff0000;">&quot;False is not true&quot;</span>, <span style="color: #000000; font-weight: bold;">false</span>
            assertTrue <span style="color: #ff0000;">&quot;falseMethod() is not true&quot;</span>, falseMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            assertNull <span style="color: #ff0000;">&quot;Book is not null&quot;</span>, book
            assertEquals <span style="color: #ff0000;">&quot;House of Leaves&quot;</span>, book.<span style="color: #006600;">title</span>
        <span style="color: #66cc66;">&#125;</span>   
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> trueMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#125;</span>   
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> falseMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The first test shows how a passing test can successfully refer to other variables and methods defined inside the test class.  The second test fails, but it doesn&#8217;t fail till all 4 of the assertions are tried and collected.  It then throws an error with the assertion failure count and the individual error messages:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">Testcase: testBookFalseAssertsCollectsFailures took <span style="color: #cc66cc;">0.371</span> sec
	Caused an ERROR
<span style="color: #cc66cc;">4</span> failed assertions found:
<span style="color: #000000; font-weight: bold;">False</span> is not <span style="color: #000000; font-weight: bold;">true</span>
falseMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> is not <span style="color: #000000; font-weight: bold;">true</span>
<span style="color: #aaaadd; font-weight: bold;">Book</span> is not <span style="color: #000000; font-weight: bold;">null</span>
expected:<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#91;</span>House of Leaves<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span> but was:<span style="color: #66cc66;">&lt;</span><span style="color: #66cc66;">&#91;</span>Infinite Jest<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>It might be useful to also print out the individual stack traces for each assertion error, but this is left as an exercise for the reader.</p>
<p>In most situations, I like the fail fast behavior and limiting tests to exercising one feature at a time, but there have been a few test cases where something like this would&#8217;ve been nice.<br />
<script type="text/javascript">var dzone_url = 'http://naleid.com/blog/2009/06/25/groovy-closures-make-unit-testing-with-soft-asserts-simple/';</script><br />
<script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/06/25/groovy-closures-make-unit-testing-with-soft-asserts-simple/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Grails plugin build-test-data 0.2.1 released</title>
		<link>http://naleid.com/blog/2009/06/15/grails-plugin-build-test-data-021-released/</link>
		<comments>http://naleid.com/blog/2009/06/15/grails-plugin-build-test-data-021-released/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 05:37:20 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[integration testing]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=56</guid>
		<description><![CDATA[I&#8217;ve just released version 0.2.1 of the build-test-data grails plugin. The build-test-data plugin makes creating integration test data easy. It decorates your domain objects with a &#8220;build&#8221; method that will create new domain instances and will automatically populate required fields with data and save it to the database. This enables you to create more maintainable [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just released version 0.2.1 of the <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/Home" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/Home?referer=');">build-test-data grails plugin</a>.</p>
<p>The build-test-data plugin makes creating integration test data easy.  It decorates your domain objects with a &#8220;build&#8221; method that will create new domain instances and will automatically populate required fields with data and save it to the database.  This enables you to create more maintainable tests where the data you create is targeted specifically at the situations you&#8217;re trying to test, without having to go through all of the ceremony of creating the rest of the object graph that you don&#8217;t care about.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// creates a new Book instance with all required fields </span>
<span style="color: #808080; font-style: italic;">// (like the Author that the book belongsTo) populated</span>
<span style="color: #000000; font-weight: bold;">def</span> b <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Book</span>.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>To learn more about the basics of the build-test-data plugin, see <a href="http://naleid.com/blog/2009/04/14/grails-build-test-data-01-plugin-released/">this blog post</a> and check out the <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/BasicUsage" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/BasicUsage?referer=');">Basic Usage</a> and <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/SampleCode" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/SampleCode?referer=');">Sample Code</a> wiki pages.<br />
<span id="more-56"></span><br />
To install, just run the install-plugin grails command (this will also upgrade an existing plugin):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails install-plugin build-test-data</pre></div></div>

<p>The 0.2 version of the plugin includes a number of enhancements and <a href="http://bitbucket.org/tednaleid/grails-test-data/issues/" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/issues/?referer=');">bugfixes</a>.  Major highlights include:</p>
<ul>
<li>
JodaTime support (along with any other persistable class with a zero argument constructor)
</li>
<li>
much easier and more powerful config files (see the <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/TestDataConfig" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/TestDataConfig?referer=');">TestDataConfig</a> wiki page for details and examples)
</li>
</ul>
<p>Additionally, the code has been cleaned up and refactored to be easier to understand.</p>
<p>See the <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/ReleaseNotes" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/ReleaseNotes?referer=');">Release Notes</a> wiki page for full details. </p>
<p>I&#8217;d be interested to hear any further feedback on this plugin and other features that people might find useful.  I&#8217;ve successfully been using it on my current project and it&#8217;s been a big help in keeping my tests clean and free from superfluous object construction code, as well as flexible in that it automatically adapts to a changing domain model.</p>
<p><script type="text/javascript">var dzone_url = 'http://naleid.com/blog/2009/06/15/grails-plugin-build-test-data-021-released/';</script><br />
<script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/06/15/grails-plugin-build-test-data-021-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Grails Testing Command Line Aliases</title>
		<link>http://naleid.com/blog/2009/06/14/grails-testing-command-line-aliases/</link>
		<comments>http://naleid.com/blog/2009/06/14/grails-testing-command-line-aliases/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 05:30:57 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=55</guid>
		<description><![CDATA[I&#8217;ve recently thrown together a few different command line aliases that have been very helpful in my grails development and I thought others might benefit from them. The aliases are primarily just the first letter of the words in the command, which makes them easy to remember (and saves on typing). Some examples: gta # [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently thrown together a few different command line aliases that have been very helpful in my grails development and I thought others might benefit from them.</p>
<p>The aliases are primarily just the first letter of the words in the command, which makes them easy to remember (and saves on typing).  Some examples:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gta                   <span style="color: #666666; font-style: italic;"># grails test-app</span>
gtai                  <span style="color: #666666; font-style: italic;"># grails test-app -integration</span>
gtaud AuthorService   <span style="color: #666666; font-style: italic;"># grails-debug-suspend test-app -unit AuthorService</span></pre></div></div>

<p><span id="more-55"></span>My testing aliases are probably my most often used grails aliases.  They&#8217;re all &#8220;gta&#8221; (for grails test-app) followed by an optional &#8220;u&#8221; or &#8220;i&#8221; (to run only unit or integration).</p>
<p>There&#8217;s also an alternate for each of those commands with a &#8220;d&#8221; on the end for &#8220;debug&#8221;.  I call a script named &#8220;grails-debug-suspend&#8221; which is simply a copy of the normal &#8220;grails-debug&#8221; command, but with the &#8220;suspend&#8221; flag set to &#8220;y&#8221;.  This causes the application to stop right away till you attach a remote debugger.  This is very useful for quick tests as its often the case that the code gets past where you have breakpoints set before you launch the debugger.</p>
<p>These commands all also let you pass in the name of a specific test class so that only that test runs.</p>
<p>The overall format for all of the aliases is:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gta<span style="color: #7a0874; font-weight: bold;">&#91;</span>i<span style="color: #000000; font-weight: bold;">|</span>u<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span>d<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>TestClassName<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>This example suspends until a debugger is attached and then runs the AuthorServiceTests.groovy unit test class:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gtaud AuthorService</pre></div></div>

<p>These aliases are all set up to run the &#8220;testlog&#8221; alias if a failing exit code is returned from the test run.  This searches through all of the testlog results for any failures and automatically opens up each failing testlog in the OSX Console.app (if you&#8217;re on another platform, just substitute a different editor).  I wrote <a href="http://naleid.com/blog/2008/03/13/using-the-osx-consoleapp-to-aid-grails-test-driven-development/">a more detailed post a while ago about how nice Console.app is for looking at grails test logs</a>.</p>
<p>Lastly, each command is also run with the unix &#8220;time&#8221; command, which I find useful to help me see how long my tests are taking and get a feel over time for the overall performance of the system under test.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># grails-debug-suspend doesn't exist by default, it's a copy of grails-debug with the suspend flag changed to &quot;y&quot; so that</span>
<span style="color: #666666; font-style: italic;"># we can attach a remote debugger before it proceeds past startup</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gta</span>=grailsTestApp
<span style="color: #000000; font-weight: bold;">function</span> grailsTestApp<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtad</span>=grailsTestAppDebug
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtau</span>=grailsTestAppUnit
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnit<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #660033;">-unit</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaud</span>=grailsTestAppUnitDebug
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnitDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #660033;">-unit</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtai</span>=grailsTestAppIntegration
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegration<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #660033;">-integration</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaid</span>=grailsTestAppIntegrationDebug
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegrationDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #660033;">-integration</span> <span style="color: #007800;">$1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTest<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Running: $1 test-app $2 $3 || testlog &quot;</span>
    <span style="color: #000000; font-weight: bold;">time</span> <span style="color: #007800;">$1</span> test-app <span style="color: #007800;">$2</span> <span style="color: #007800;">$3</span> <span style="color: #000000; font-weight: bold;">||</span> testlog
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># after grails test if there were ERROR messages, you can open those logs with the Console.app using this</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">testlog</span>=<span style="color: #ff0000;">'for F in `grep -lE &quot;FAILED|Caused\ an\ ERROR&quot; test/reports/plain/*.txt`; do echo &quot;&gt;&gt;&gt; opening&quot; $F; open -a Console $F; done;'</span></pre></div></div>

<p>You can place these aliases in your .bashrc/.bash_profile in your home directory (you can also use them with <a href="http://www.cygwin.com/" onclick="pageTracker._trackPageview('/outgoing/www.cygwin.com/?referer=');">cygwin</a> on windows.</p>
<p>Alternatively, if you&#8217;re using zsh (or interested in switching from bash), I&#8217;ve got all of my zsh setup <a href="http://naleid.com/blog/2009/05/13/shared-zshrc-file/">shared in a source control repo</a> in a format that&#8217;s easy to use and extend for yourself.</p>
<p>This is the modified grails-debug command called grails-debug-suspend, just put it somewhere in your PATH and make it executable (chmod +x grails-debug-suspend):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">DIRNAME</span>=<span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin
. <span style="color: #ff0000;">&quot;<span style="color: #007800;">$DIRNAME</span>/startGrails&quot;</span>
&nbsp;
<span style="color: #007800;">JAVA_OPTS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$JAVA_OPTS</span> -Xdebug -Xnoagent -Dgrails.full.stacktrace=true -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005&quot;</span>
startGrails org.codehaus.groovy.grails.cli.GrailsScriptRunner <span style="color: #ff0000;">&quot;$@&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/06/14/grails-testing-command-line-aliases/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Grails build-test-data Plugin Released!</title>
		<link>http://naleid.com/blog/2009/04/14/grails-build-test-data-01-plugin-released/</link>
		<comments>http://naleid.com/blog/2009/04/14/grails-build-test-data-01-plugin-released/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 02:34:58 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=51</guid>
		<description><![CDATA[Creating maintainable test data is hard. Often an entire object graph needs to be created to support the instantiation of a single domain object. This leads to either the cutting and pasting of that creation code, or relying on a canned set of objects that we&#8217;ve grown over time and maintained as the domain objects [...]]]></description>
			<content:encoded><![CDATA[<p>Creating maintainable test data is hard. </p>
<p>Often an entire object graph needs to be created to support the instantiation of a single domain object. This leads to either the cutting and pasting of that creation code, or relying on a canned set of objects that we&#8217;ve grown over time and maintained as the domain objects change. After a while, adding just one more Widget to that set of canned data ends up breaking tests just about every time.</p>
<p>There has to be a better solution, right?<span id="more-51"></span></p>
<p>Yep! Due to the &#8220;Power and the Glory of Grails&#8221;&trade;, we have a lot of metadata at our fingertips about those domain objects. We know what <a href="http://grails.org/doc/1.1/guide/single.html#7.1%20Declaring%20Constraints" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/1.1/guide/single.html_7.1_20Declaring_20Constraints?referer=');">validation constraints</a> we&#8217;ve placed on our objects, and which objects depend on other objects to live.</p>
<p>Using this additional information, Joe Hoover and I have created a <a href="http://grails.org/plugin/build-test-data" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/build-test-data?referer=');">grails plugin called &#8220;build-test-data&#8221;</a>.  With it, you can create test data and omit the fields that you don&#8217;t care about for your test.  The plugin takes care of populating the values required by constraints and persists your domain objects to the database.</p>
<p>This plugin is focused on creating test data for integration testing. If you&#8217;re doing unit testing, there are a number of mocking solutions out there (including a few ways to do it built right into groovy). Unit testing only goes so far though, and if you want to ensure that your code works when it&#8217;s hooked up to a real database, with real transactions, saving, triggers, etc, you&#8217;re only going to get that from an integration test.</p>
<p>Installation of the plugin is easy.  Just run this grails task in the root of your project:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails install-plugin build-test-data</pre></div></div>

<p>When the plugin is installed, most domain object should be able to be instantiated and persisted out of the box.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// automatically creates/saves a Book with all required fields populated</span>
<span style="color: #000000; font-weight: bold;">def</span> book <span style="color: #66cc66;">=</span> <span style="color: #aaaadd; font-weight: bold;">Book</span>.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Once installed, all you have to do is call the new <code>build()</code> method on your domain class and you&#8217;ll be given a valid instance that has been persisted to the database.</p>
<p>It will populate the required fields as defined by the constraints and it has the ability to deal with <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/Features" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/Features?referer=');">almost all of them</a>.</p>
<p>A couple of them, such as custom validations and regular expressions, are very difficult to work with so if a sample value that we try doesn&#8217;t get past validation, we&#8217;ll throw an error.</p>
<h3>Build Test Data Example</h3>
<p>Here&#8217;s a quick example.  Say you have the following domain objects:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// grails-app/domain/Author.groovy</span>
<span style="color: #000000; font-weight: bold;">class</span> Author <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> firstName
    <span style="color: #aaaadd; font-weight: bold;">String</span> lastName
    <span style="color: #000000; font-weight: bold;">static</span> hasMany <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>books: <span style="color: #aaaadd; font-weight: bold;">Book</span><span style="color: #66cc66;">&#93;</span>    
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// grails-app/domain/Book.groovy</span>
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #aaaadd; font-weight: bold;">Book</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> title
    <span style="color: #aaaadd; font-weight: bold;">Date</span> published
    <span style="color: #aaaadd; font-weight: bold;">BigDecimal</span> price
    <span style="color: #000000; font-weight: bold;">static</span> belongsTo <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>author: Author<span style="color: #66cc66;">&#93;</span>    
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Remember that grails properties are required unless nullable is explicitly set to false, so all of those properties are required.</p>
<p>If you&#8217;re testing a BookPurchasingService and the method you&#8217;re calling needs a Book, normally you&#8217;d need to also create an Author to go with it.  Here&#8217;s how you might normally create your test data without the build-test-data plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #993333;">void</span> testPurchaseBookService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">def</span> author <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Author<span style="color: #66cc66;">&#40;</span>firstName: <span style="color: #ff0000;">&quot;first&quot;</span>, lastName: <span style="color: #ff0000;">&quot;last&quot;</span><span style="color: #66cc66;">&#41;</span>
	author.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #aaaadd; font-weight: bold;">Book</span> tenDollarBook <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Book</span><span style="color: #66cc66;">&#40;</span>author: author, title: <span style="color: #ff0000;">&quot;title&quot;</span>, published: <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, price: <span style="color: #cc66cc;">10.00</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #aaaadd; font-weight: bold;">BigDecimal</span><span style="color: #66cc66;">&#41;</span>
	tenDollarBook.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">def</span> result <span style="color: #66cc66;">=</span> service.<span style="color: #006600;">purchaseBook</span><span style="color: #66cc66;">&#40;</span>tenDollarBook<span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//... assertions about expected result of service call</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You need to create an Author just to create the Book, even though your service only cares about the Book.  If the Author class ever adds an additional required field, you not only need to update all of your Author test classes, but all of your Book test classes as well.</p>
<p>Additionally, there&#8217;s all kinds of &#8220;noise&#8221; in your test class that makes it more difficult to understand exactly what it is you&#8217;re trying to test.</p>
<p>If you&#8217;ve got the build-test-data plugin installed, you don&#8217;t need to worry about any of that.  Creating sample data is easy and you can specify only the parameters that you actually need specific values in to exercise your test.  The plugin will fill in the rest and persist it to the database automatically.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #993333;">void</span> testPurchaseBookService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">def</span> result <span style="color: #66cc66;">=</span> service.<span style="color: #006600;">purchaseBook</span><span style="color: #66cc66;">&#40;</span> <span style="color: #aaaadd; font-weight: bold;">Book</span>.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>price: <span style="color: #cc66cc;">10.00</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #aaaadd; font-weight: bold;">BigDecimal</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">//... assertions about expected result of service call</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Behind the scenes, build-test-data populates the Book.title and Book.purchased fields, it also creates a new Author with a firstName and lastName and saves all of that to the database.</p>
<p>This test is much cleaner as you only see the things that actually matter to the test and none of the administrative flotsam and jetsam needed to set things up.</p>
<p>In the repository on the bitbucket website, there is <a href="http://bitbucket.org/tednaleid/grails-test-data/src/tip/bookStore/grails-app/domain/bookstore/" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/src/tip/bookStore/grails-app/domain/bookstore/?referer=');">a test &#8220;bookstore&#8221; grails application</a> that has a fairly complex domain model that exercises the build-test-data build method and it&#8217;s ability to serialize an object graph.</p>
<p>Check out the full set of documentation on the <a href="http://bitbucket.org/tednaleid/grails-test-data/wiki/Home" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/Home?referer=');">bitbucket build-test-data wiki</a> and let Joe and I know what you think of it.</p>
<p><script type="text/javascript">var dzone_url = 'http://naleid.com/blog/2009/04/14/grails-build-test-data-01-plugin-released/';</script><br />
<script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/04/14/grails-build-test-data-01-plugin-released/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

