<?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>Sword Systems &#187; Unit Testing</title>
	<atom:link href="http://swordsystems.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://swordsystems.com</link>
	<description>Cutting Commentary</description>
	<lastBuildDate>Tue, 15 May 2012 14:50:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Grails 2 Unit Tests &#8211; Undocumented Properties</title>
		<link>http://swordsystems.com/2012/05/15/grails-2-unit-tests-undocumented-properties/</link>
		<comments>http://swordsystems.com/2012/05/15/grails-2-unit-tests-undocumented-properties/#comments</comments>
		<pubDate>Tue, 15 May 2012 14:50:27 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[unit test]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=242</guid>
		<description><![CDATA[GrailsUnitTestMixin (and its descendents DomainClassUnitTestMixin, ServiceUnitTestMixin, etc) give your test classes some static and instance variables that aren&#8217;t mentioned in the docs yet. You get applicationContext, grailsApplication, config, and messageSource. Previously, if you had code that needed a grailsApplication (such as the JSON configurations), you had to set it up yourself. Now you get it [...]]]></description>
			<content:encoded><![CDATA[<p><code>GrailsUnitTestMixin</code> (and its descendents <code>DomainClassUnitTestMixin</code>, <code>ServiceUnitTestMixin</code>, etc) give your test classes some static and instance variables that aren&#8217;t mentioned in the docs yet.  You get <code>applicationContext, grailsApplication, config</code>, and <code>messageSource</code>.  Previously, if you had code that needed a grailsApplication (such as the JSON configurations), you had to set it up yourself.  Now you get it for free.  The mixin also implements the <code>shouldFail</code> method from <code>GroovyTestCase</code> so you can keep that functionality even though your tests are now JUnit 4 based.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2012/05/15/grails-2-unit-tests-undocumented-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When dynamic programming backfires</title>
		<link>http://swordsystems.com/2009/11/20/when-dynamic-properties-backfire/</link>
		<comments>http://swordsystems.com/2009/11/20/when-dynamic-properties-backfire/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 05:53:22 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[frustration]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=35</guid>
		<description><![CDATA[I love Groovy&#8217;s meta-programming capabilities.  Duck typing, DSLs, ExpandoMetaClass&#8230; it&#8217;s all great until you blow your arm off with them and spend two hours picking the pieces back up.  Such was my experience today as I was trying to set up my first unit test for a Grails controller.  I needed to pass in some [...]]]></description>
			<content:encoded><![CDATA[<p>I love Groovy&#8217;s meta-programming capabilities.  Duck typing, DSLs, ExpandoMetaClass&#8230; it&#8217;s all great until you blow your arm off with them and spend two hours picking the pieces back up.  Such was my experience today as I was trying to set up my first unit test for a Grails controller.  I needed to pass in some parameters.  As I just wrote in my last post, the grails user guide has information on integration testing controllers, but nothing more than a brief mention of the <code>ControllerUnitTestCase</code>.  Unfortunately, searching for phrases like &#8220;grails unit test controller&#8221; on Google turn up posts from last Fall, before the <code>ControllerUnitTestCase </code>was released with Grails 1.1.  Don&#8217;t ask me why I didn&#8217;t just search for <code>ControllerUnitTestCase </code>from the start.  20/20 hindsight.</p>
<p>Working with what I could find, I came across a <a href="http://kousenit.wordpress.com/2008/03/24/integration-tests-of-controllers-in-grails/" target="_blank">blog post from March 2008</a> that covered integration testing with controllers, much like the grails User Guide.  It at least had some explicit examples of how to pass in parameters, so I hoped the approach would work with my <code>ControllerUnitTestCase </code>implementation.  Using the code snippets in the post as a guide, I setup my test something like this:</p>
<p><code>controller.request.params = [layoutType:'filmstrip',compKey:'testKey']<br />
controller.create()<br />
</code><br />
This looks good, right?  The test scaffolding connects the injected <code>params </code>property of the controller with the <code>params </code>property of the request.  Should work like a charm.</p>
<p>And so started my descent into WTF Land.</p>
<p>The tests compiled and ran fine, except that no params were getting passed through to the controller.  I assumed that the old documentation for integration testing no longer exactly applied for the new unit test setups.  Eventually I dug through enough of <code>ControllerUnitTestCase</code>, <code>MvcUnitTestCase</code>, and <code>MockUtils </code>that I figured out there was a way to get direct access to the <code>params </code>property of the controller.  &#8220;Makes sense,&#8221; I thought, &#8220;sort of a mini-mock.  Who needs the request for a unit test?&#8221;  Great, so let&#8217;s try this:</p>
<p><code>controller.params = [layoutType:'filmstrip',compKey:'testKey']<br />
controller.create()<br />
</code><br />
Run again and&#8230;</p>
<p><code>groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: params for class: YourController<br />
</code><br />
Erk.  Closer inspection of <code>grails.test.MockUtils.addCommonWebProperties()</code> reveals how the <code>params </code>property is added to the controller class with only a <code>get </code>method.  Okay, so we can still manipulate the map once we get it.  One more try:</p>
<p><code>controller.params.putAll([layoutType:"filmstrip",compKey:'testKey'])<br />
controller.create()<br />
</code><br />
Success!  Finally, information is going where it needs to be.  I later realized that section 9.2 of the user guide does actually show an example similar to this, except that it sets each item on params individually:</p>
<p><code>def controller = new AuthenticationController()<br />
controller.params.login = "marcpalmer"<br />
controller.params.password = "secret"<br />
controller.params.passwordConfirm = "secret"<br />
controller.signup()</p>
<p></code>&#8220;Oh well,&#8221; I say to myself, &#8220;at least I got it working.  I&#8217;d better post a comment on that old blog post so that others know the new way to set parameters.&#8221;  I returned to the blog page, moved down to the comments section&#8230; and saw that someone else had posted a comment just last month thanking the author for the good information.  Wait a second&#8230; you normally don&#8217;t thank someone if their examples don&#8217;t work.  So I scrolled up and took a closer look at the examples again:</p>
<p><code>controller.request.parameters = [sender:'me',msg:'test']<br />
controller.create()<br />
</code><br />
Anyone with a better eye for detail than me see the difference that sent me down the rat hole? It&#8217;s <code>request.parameters</code>, not <code>request.params</code> as I had entered.  For the 100th time today, Doh!</p>
<p>But wait a second (or an hour, as the case may be)&#8230; If I was calling <code>request.params</code> and there is no params property, how did my test run at all?  Dig, dig, dig&#8230; It turns out that the request is a <code>GrailsMockHttpServletRequest </code>object.  Scanning through that class, I see the ever so powerful and ever so dangerous <code>setProperty(String name, value)</code> override.  If you try to set a property that doesn&#8217;t exist, it adds the value as an entry in the request&#8217;s <code>attributes </code>map.  So the test scaffolding was perfectly happy with my <code>request.params</code> call.  It nicely tucked my params map away in the attributes and continued on its way.</p>
<p>Sigh&#8230;it almost makes me miss compile-time method checking&#8230; but not quite.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/11/20/when-dynamic-properties-backfire/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ControllerUnitTestCase: Documentation Has Arrived</title>
		<link>http://swordsystems.com/2009/11/19/controllerunittestcase-documentation-has-arrived/</link>
		<comments>http://swordsystems.com/2009/11/19/controllerunittestcase-documentation-has-arrived/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 04:53:35 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Controller]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=32</guid>
		<description><![CDATA[My team started on our first Grails app back in mid-August.  I set out to provide good examples of using the new testing features in Grails 1.1 to encourage the rest of the team.  Domain class testing went great, as did Service testing.  Unfortunately, there wasn&#8217;t any documentation on the ControllerUnitTestCase class in the Grails [...]]]></description>
			<content:encoded><![CDATA[<p>My team started on our first Grails app back in mid-August.  I set out to provide good examples of using the new testing features in Grails 1.1 to encourage the rest of the team.  Domain class testing went great, as did Service testing.  Unfortunately, there wasn&#8217;t any documentation on the ControllerUnitTestCase class in the Grails User Guide, and I couldn&#8217;t find any blog or forum posts to help out.  I tried creating an instance of it and tossing in a call to mockController(), but I didn&#8217;t get very far.  I had to shift focus to other tasks before I could dive into the source code itself and figure out what it did, so progress stopped for a while.</p>
<p>Fast forward to today when I finally had a strong need for a controller unit test setup.  I dove into the code and realized three things very quickly:</p>
<ol>
<li>You don&#8217;t need to call mockController().  It does that for you based on the name of the test class itself.</li>
<li>You don&#8217;t want to call the constructor of your controller class in the test cases.  Use the newInstance() method instead so that all the proper scaffolding is put in place.</li>
<li>You don&#8217;t even have to call newInstance() most of the time because the setUp() method inits a controller variable for you already.</li>
</ol>
<p>Doh!  I wish I had taken 15 minutes back in August to look at the source.  It would have saved quite a bit of time by encouraging us to get proper controller test cases in place faster.</p>
<p>To add insult to injury, after spending that time and more getting some test cases just right, I decided to try a search for more documentation online again.   This time there was an answer to my query for help.  A post to the <a href="http://www.pubbs.net/grails/200910/43539/" target="_blank">grails-user</a> mailing list less than a month ago generated several responses with useful info, including Scott Davis posting about the most recent addition to his <a href="http://www.ibm.com/developerworks/java/library/j-grails10209/index.html" target="_blank">Mastering Grails</a> series which was published just days before the post.</p>
<p>So nine months after the release of Grails 1.1 there is now good documentation on this great piece.  Many thanks to those who pulled it together.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/11/19/controllerunittestcase-documentation-has-arrived/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Corrections to Grails Documentation</title>
		<link>http://swordsystems.com/2009/09/05/corrections-to-grails-documentation/</link>
		<comments>http://swordsystems.com/2009/09/05/corrections-to-grails-documentation/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 14:24:44 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[frustration]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[GORM]]></category>
		<category><![CDATA[Persistance]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://swordsystems.com/?p=20</guid>
		<description><![CDATA[Here are two quick tips for anyone working with Grails 1.1.1: 1) The User Guide has conflicting info about cascading persistence.   Section 5.2.1.2 says &#8220;The default cascading behaviour is to cascade saves and updates, but not deletes unless a belongsTo is also specified.&#8221; Section 5.3.3 says, &#8220;If you do not define belongsTo then no [...]]]></description>
			<content:encoded><![CDATA[<p>Here are two quick tips for anyone working with Grails 1.1.1:</p>
<p>1) The User Guide has conflicting info about cascading persistence.    Section 5.2.1.2 says &#8220;The default cascading behaviour is to cascade saves and updates, but not deletes unless a belongsTo is also specified.&#8221;  Section 5.3.3 says, &#8220;If you do not define belongsTo then no cascades will happen and you will have to manually save each object.&#8221;</p>
<p>The former, 5.2.1.2, is correct.  Create and Update actions are cascaded for one-to-many relations.  However, hey are not cascaded for one-to-one relations.</p>
<p>2) Section 9.1 (Unit Testing) demonstrates how to pass in a list to the mockDomain(clazz, instanceList) call.  It then says that you can check that list to verify that persistence actions behaved as expected.  This is no longer correct.  The list is copied inside the mockDomain call.  To get access to the list of persisted objects for a class, use the MockUtils.TEST_INSTANCES map with the class name as the key.  For example:</p>
<pre>def testInstances = []
mockDomain(Item, testInstances)
def i = new Item(name:"foo").save()
assertEquals 0, testInstances.size()
testInstances = MockUtils.TEST_INSTANCES[Item]
assertEquals 1, testInstances.size()</pre>
<p>And in case you try the same thing as me, the mocking behavior does not support cascades.</p>
]]></content:encoded>
			<wfw:commentRss>http://swordsystems.com/2009/09/05/corrections-to-grails-documentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

