<?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>Coding Clarity &#187; unit testing</title>
	<atom:link href="http://chrisdail.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisdail.com</link>
	<description>Writing simple, clear and readable code.</description>
	<lastBuildDate>Sun, 19 Jun 2011 15:54:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Groovy for Java Unit Testing</title>
		<link>http://chrisdail.com/2007/12/13/groovy-for-java-unit-testing/</link>
		<comments>http://chrisdail.com/2007/12/13/groovy-for-java-unit-testing/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 15:37:00 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://chrisdail.com/2007/12/13/groovy-for-java-unit-testing/</guid>
		<description><![CDATA[I listened to a great podcast the other day on &#8220;Groovy Programming for Java Guys&#8220;. It was a really good look into the groovy language and how it relates to Java software development. I&#8217;ve sent this to management to try and help them understand better this whole &#8220;Groovy&#8221; thing. My team has been integrating groovy [...]]]></description>
			<content:encoded><![CDATA[<p>I listened to a great podcast the other day on &#8220;<a href="http://www.javaworld.com/podcasts/jtech/2007/110107jtech004.html">Groovy Programming for Java Guys</a>&#8220;. It was a really good look into the groovy language and how it relates to Java software development. I&#8217;ve sent this to management to try and help them understand better this whole &#8220;Groovy&#8221; thing.</p>
<p>My team has been integrating groovy code into our legacy Java project slowly over the last little while. Recently I have added it into our build scripts so Groovy can live alongside Java as a first class citizen. More importantly, now Groovy can be used in our unit tests. The groovyc ant task allows for joint compiling of Java and Groovy sources. This task is available with the new Groovy 1.5 release at <a href="http://groovy.codehaus.org/">groovy.codehaus.org</a>.<br />
<groovyc srcdir="${compile.src.dir}" destdir="${compile.dest.dir}"><classpath></classpath></groovyc></p>
<pathelement location="${compile.dest.dir}">
<path refid="classpath"></path><javac source="1.5" target="1.5"></javac><br />
There are a lot of features that Groovy brings to unit testing Java code. A big one is using map coercion to create mock objects. Your Java code may use an interface or class that does some particular operation in production. You might not want to actually perform the operation in a unit test but verify that the operation was performed. Coercion allows us to create a mock object that acts as a particular class but has different functionality. For example, if you have a production class that sends an email:</pathelement><span style="font-family: courier new">class Mailer {</span><br />
<span style="font-family: courier new">    public void sendEmail() {</span><br />
<span style="font-family: courier new">        // Email sending code&#8230;</span><br />
<span style="font-family: courier new">        </span><br />
<span style="font-family: courier new">        System.out.println(&#8220;email sent&#8221;);</span><br />
<span style="font-family: courier new">    }</span><br />
<span style="font-family: courier new">}</span></p>
<p>You can simulate a Mailer by creating a map that maps method names to a closure. The as keyword coerces the map to act as the Mailer class.</p>
<p><span style="font-family: courier new">def fakeMailClosure = {</span><br />
<span style="font-family: courier new">    println &#8220;fake email&#8221;</span><br />
<span style="font-family: courier new">}</span><br />
<span style="font-family: courier new">def testObject = [sendEmail: fakeMailClosure] as Mailer</span><br />
<span style="font-family: courier new">testObject.sendEmail()</span></p>
<p>Just a few lines of groovy can create dummy objects that can be used to do whatever you want in a test environment. The testObject can even be passed into Java code where it is treated as a real Mailer.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrisdail.com/2007/12/13/groovy-for-java-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

