<?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>Intelligrape  Groovy &#38; Grails Blogs &#187; grails-test</title>
	<atom:link href="http://www.intelligrape.com/blog/tag/grails-test/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.intelligrape.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 02 Feb 2012 07:48:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
			<title>Intelligrape  Groovy &amp; Grails Blogs</title>
			<url>http://www.intelligrape.com/blog/wp-content/uploads/2011/05/favicon2.ico</url>
			<link>http://www.intelligrape.com/blog</link>
			<width></width>
			<height></height>
			<description></description>
		</image>		<item>
		<title>Unit-Tests : Getting started with Service Unit Test</title>
		<link>http://www.intelligrape.com/blog/2010/06/14/unit-tests-getting-started-with-service-unit-test/</link>
		<comments>http://www.intelligrape.com/blog/2010/06/14/unit-tests-getting-started-with-service-unit-test/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 09:54:45 +0000</pubDate>
		<dc:creator>Imran Mir</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[grails testing]]></category>
		<category><![CDATA[grails-test]]></category>
		<category><![CDATA[unit-test]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=1056</guid>
		<description><![CDATA[Hi all,
Here I am giving a brief introduction about  unit testing of services in grails. I will explain it with the help of a simple example.
We have a domain class named: Item
Two services : UtilService and ItemService
Code of UtilService.groovy

// class UtilService code
&#160;
class UtilService &#123;
    boolean transactional = true
    [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all,</p>
<p>Here I am giving a brief introduction about  unit testing of services in grails. I will explain it with the help of a simple example.</p>
<p>We have a domain class named: Item<br />
Two services : UtilService and ItemService</p>
<p>Code of UtilService.groovy</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #808080; font-style: italic;">// class UtilService code</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UtilService <span style="color: #66cc66;">&#123;</span>
    <span style="color: #993333;">boolean</span> transactional <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
    <span style="color: #aaaadd; font-weight: bold;">Integer</span> calculatePrice<span style="color: #66cc66;">&#40;</span>Item item, <span style="color: #aaaadd; font-weight: bold;">Integer</span> vat, <span style="color: #aaaadd; font-weight: bold;">Integer</span> profit<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> item.<span style="color: #006600;">price</span> <span style="color: #66cc66;">*</span> vat <span style="color: #66cc66;">*</span> profit
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Code of ItemService.groovy</p>

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #808080; font-style: italic;">// ItemService class code</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ItemService <span style="color: #66cc66;">&#123;</span>
    <span style="color: #993333;">boolean</span> transactional <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
    <span style="color: #000000; font-weight: bold;">def</span> utilService
    <span style="color: #aaaadd; font-weight: bold;">Integer</span> calculateCost<span style="color: #66cc66;">&#40;</span>Item item,<span style="color: #aaaadd; font-weight: bold;">Integer</span> quantity<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">Integer</span> price <span style="color: #66cc66;">=</span> utilService.<span style="color: #006600;">calculatePrice</span><span style="color: #66cc66;">&#40;</span>item,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #aaaadd; font-weight: bold;">Integer</span> totalCost <span style="color: #66cc66;">=</span> price <span style="color: #66cc66;">*</span> quantity
        <span style="color: #000000; font-weight: bold;">return</span> totalCost
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Our test case:-</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">  <span style="color: #993333;">void</span> test_calculateCost<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
 <span style="color: #808080; font-style: italic;">// Step-1: Mocking the domain class</span>
        <span style="color: #000000; font-weight: bold;">def</span> instance <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> Item<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        mockDomain<span style="color: #66cc66;">&#40;</span>Item, instance<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Step-2: Mocking the service and its method used in the function that has to be tested</span>
        <span style="color: #000000; font-weight: bold;">def</span> otherService <span style="color: #66cc66;">=</span> mockFor<span style="color: #66cc66;">&#40;</span>UtilService<span style="color: #66cc66;">&#41;</span>
        otherService.<span style="color: #006600;">demand</span>.<span style="color: #006600;">calculatePrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>l,m, n -<span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Step-3: creating the instance of our service</span>
        <span style="color: #000000; font-weight: bold;">def</span> itemService <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ItemService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        itemService.<span style="color: #006600;">utilService</span> <span style="color: #66cc66;">=</span> otherService.<span style="color: #006600;">createMock</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Step-4: Calling the method to be tested</span>
        <span style="color: #000000; font-weight: bold;">def</span> amount <span style="color: #66cc66;">=</span> itemService.<span style="color: #006600;">calculateCost</span><span style="color: #66cc66;">&#40;</span>instance<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        assertEquals <span style="color: #cc66cc;">1500</span>, amount
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Hope it helps.<br />
For more&#8230;. wait for the next blog on unit testing</p>
<p>Regards<br />
Imran<br />
imran@intelligrape.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/06/14/unit-tests-getting-started-with-service-unit-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

