<?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>Grails Rocks &#187; unit testing</title>
	<atom:link href="http://www.anyware.co.uk/2005/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anyware.co.uk/2005</link>
	<description>Grails, Apple, usability and world stuff</description>
	<lastBuildDate>Fri, 27 Jan 2012 13:52:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unit tests &#8211; now they work, now they don&#8217;t</title>
		<link>http://www.anyware.co.uk/2005/2008/11/19/unit-tests-now-they-work-now-they-dont/</link>
		<comments>http://www.anyware.co.uk/2005/2008/11/19/unit-tests-now-they-work-now-they-dont/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 18:45:33 +0000</pubDate>
		<dc:creator>Marc Palmer</dc:creator>
				<category><![CDATA[Groovy and Grails]]></category>
		<category><![CDATA[Programming links]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.anyware.co.uk/2005/?p=300</guid>
		<description><![CDATA[Writing unit tests is often quite tricky. However even simple ones can cause problems. I&#8217;ve been merging in my GSP whitespace handling changes into Grails 1.1 and found that for me a bunch of unrelated tests were failing &#8211; but the Bamboo CI build and presumably the builds on other developers&#8217; machines were not failing [...]]]></description>
			<content:encoded><![CDATA[<p>Writing unit tests is often quite tricky. However even simple ones can cause problems.</p>
<p>I&#8217;ve been merging in my GSP whitespace handling changes into Grails 1.1 and found that for me a bunch of unrelated tests were failing &#8211; but the Bamboo CI build and presumably the builds on other developers&#8217; machines were not failing on these.</p>
<p>In a nutshell&#8230; beware assumptions of hash key ordering. This is pretty basic stuff but it is very easy to overlook, especially when they work when you run them.</p>
<p>Don&#8217;t write test code that asserts the toString() of any object that contains unordered Hash objects, or uses keySet()s from them. For example this test renders some objects to JSON, and the order of the properties of each object cannot be guaranteed:</p>
<pre lang="groovy">void testConvertErrors() {
  def c = ga.getControllerClass("RestController").newInstance()
  c.testErrors()
  // @todo this test is fragile and depends
  // on runtime environment because
  // of hash key ordering variations
  assertEquals(
     '{"errors":[{"object":"Book","field":"title",'+
     '"rejected-value":null,"message":'+
     '"Property [title] cannot be null" },'+
     '{"object":"Book","field":"author","rejected-value":null,'+
     '"message":'+
     '"Property [author] cannot be null"}]}',
     response.contentAsString)
}</pre>
<p>In that case the solution is a bit more tricky. It is arguably better to test for substrings in the response, rather than parse out the whole JSON again and check the data &#8211; after all then you&#8217;re also unit testing the parsing code too.</p>
<p>Also don&#8217;t assume that an iterator/enumerator you get contains just the element you want:</p>
<pre lang="groovy">assertEquals "foo", pageContext.getAttributeNamesInScope(
   PageContext.PAGE_SCOPE).nextElement()</pre>
<p>If you need to test for the presence of an object in something that is a list or collection, unless part of your test contract is to ensure it is the -only- entry.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anyware.co.uk/2005/2008/11/19/unit-tests-now-they-work-now-they-dont/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 11/18 queries in 0.111 seconds using disk: basic

Served from: www.anyware.co.uk @ 2012-02-10 03:54:08 -->
