<?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; integration test</title>
	<atom:link href="http://www.intelligrape.com/blog/tag/integration-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>Grails : Save time while writing Integration tests</title>
		<link>http://www.intelligrape.com/blog/2010/05/06/save-time-while-writing-integration-tests/</link>
		<comments>http://www.intelligrape.com/blog/2010/05/06/save-time-while-writing-integration-tests/#comments</comments>
		<pubDate>Thu, 06 May 2010 11:48:29 +0000</pubDate>
		<dc:creator>Amit Jain</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[integration test]]></category>
		<category><![CDATA[No bootstrapping]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=643</guid>
		<description><![CDATA[Hi Friends,
I always used to spend lot of time writing integration tests as running even a single test involves loading the complete application, which includes loading complete database every time. Then I realized that data which I use for testing is always the same, why can&#8217;t we restore the database once created, before we run [...]]]></description>
			<content:encoded><![CDATA[<p>Hi Friends,</p>
<p>I always used to spend lot of time writing integration tests as running even a single test involves loading the complete application, which includes loading complete database every time. Then I realized that data which I use for testing is always the same, why can&#8217;t we restore the database once created, before we run the test so that at least time spent on loading data can be saved. Following is the script which I wrote and worked for me.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre>mysqldump --user=myusername --password=mypassword databaseName  /home/amit/proj_dev_backup.sql
mysql -u myusername --password=mypassword &lt; /home/amit/updateTestDatabase.sql
&nbsp;
cd /home/amit/applicationPath
mv ./grails-app/conf/BootStrap.groovy /home/amit/BootStrap_org.groovy
cp /home/amit/BootStrap.groovy ./grails-app/conf/
&nbsp;
grails test-app -integration $@
&nbsp;
mv /home/amit/BootStrap_org.groovy ./grails-app/conf/BootStrap.groovy
rm  /home/amit/proj_dev_backup.sql</pre></div></div>

<p>In the above script, databaseName is to be replaced with my development environment database, which is expected to be updated. Here BootStrap.groovy file gets replaced temporarily. If we like, we can also load data in a bootstrap.groovy only for development environment, then we won&#8217;t need to replace it even temporarily as in the above script.</p>
<p>Following is the updateTestDatabase.sql file used in the above script which creates and updates the test database.</p>
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="sql"> <span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> testDatabaseName; <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> testDatabaseName;
 <span style="color: #993333; font-weight: bold;">USE</span> testDatabaseName;
 source <span style="color: #66cc66;">/</span>home<span style="color: #66cc66;">/</span>amit<span style="color: #66cc66;">/</span>proj_dev_backup<span style="color: #66cc66;">.</span>sql
 exit</pre></div></div>

<p>We also need to update DataSource.groovy file for the test environment as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy">test <span style="color: #66cc66;">&#123;</span>
	dataSource <span style="color: #66cc66;">&#123;</span>
             dbCreate <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;update&quot;</span> 
             url <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;jdbc:mysql://localhost/testDatabaseName&quot;</span>
	     driverClassName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span>
	     username <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;myusername&quot;</span>
	     password <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;mypassword&quot;</span>
       <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now I just run this script, provides the list of files to be tested (if any) as command line arguments, and see the integration test results much faster then ever before. </p>
<p>Hope this helped!</p>
<p>~~Amit Jain~~<br />
amit@intelligrape.com</p>
<p>http://www.IntelliGrape.com/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/05/06/save-time-while-writing-integration-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Grails Integration Tests : Access your application path</title>
		<link>http://www.intelligrape.com/blog/2010/01/21/grails-integration-test-access-application-path/</link>
		<comments>http://www.intelligrape.com/blog/2010/01/21/grails-integration-test-access-application-path/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 10:32:21 +0000</pubDate>
		<dc:creator>Amit Jain</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[integration test]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=349</guid>
		<description><![CDATA[Access your application path in your integration test cases.]]></description>
			<content:encoded><![CDATA[<p>Hi Friends,</p>
<p>Recently I needed to access my grails application&#8217;s path while writing  an integration test. I tried to do it with servlet context, Application Holder and few more options. But none worked. Then I encountered the simplest way as given below, which worked.</p>
<blockquote>
<div class="code">

<div class="wp_syntax"><div class="code"><pre class="groovy"><span style="color: #aaaadd; font-weight: bold;">System</span>.<span style="color: #006600;">properties</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'base.dir'</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

</div>
</blockquote>
<p>Cheers!<br />
~~Amit Jain~~<br />
amit@intelligrape.com<br />
IntelliGrape Softwares</p>
<p>http://www.intelligrape.com/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2010/01/21/grails-integration-test-access-application-path/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Login user for Integration test when using Jsecurity plugin</title>
		<link>http://www.intelligrape.com/blog/2009/12/07/login-user-for-integration-test-when-using-jsecurity-plugin/</link>
		<comments>http://www.intelligrape.com/blog/2009/12/07/login-user-for-integration-test-when-using-jsecurity-plugin/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 09:46:25 +0000</pubDate>
		<dc:creator>Amit Jain</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[integration test]]></category>
		<category><![CDATA[jsecurity plugin]]></category>
		<category><![CDATA[logged in user]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=335</guid>
		<description><![CDATA[Hello Friends,
I was using Jsecurity plugin in my project. There was an action in a controller which needed logged in user information and I was finding it difficult to write an integration test for the same. Then Brent Fisher shared the following code which worked nicely for both services and controllers:



import org.jsecurity.SecurityUtils
import com.aps.domain.security.JsecUser
import org.jsecurity.subject.Subject

class MyControllerTests [...]]]></description>
			<content:encoded><![CDATA[<p>Hello Friends,</p>
<p>I was using Jsecurity plugin in my project. There was an action in a controller which needed logged in user information and I was finding it difficult to write an integration test for the same. Then Brent Fisher shared the following code which worked nicely for both services and controllers:</p>
<div class="wp_syntax">
<div class="code">
<pre class="groovy">
import org.jsecurity.SecurityUtils
import com.aps.domain.security.JsecUser
import org.jsecurity.subject.Subject

class MyControllerTests extends GrailsUnitTestCase {	

  protected void setUp() {
	super.setUp()
	//following code sets admin as a logged in user
	def subject = [isAuthenticated: true,
		       principal: "admin"
		      ] as Subject

	SecurityUtils.metaClass.static.getSubject = {-> return subject }
	Subject.metaClass.getPrincipal = {-> return "admin" }
	...
  }
 ...
}	</pre>
</div>
</div>
<p>Using metaclass, We changed the implementation of getPrincipal() and getSubject() to work in our way. So looking at this, I could see the power of a metaclass, which can be used to change or add new method implementations to an API which is not even accessible to us.</p>
<p>Cheers!</p>
<p>~~Amit Jain~~<br />
amit@intelligrape.com<br />
IntelliGrape Softwares</p>
<p>http://www.intellgrape.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2009/12/07/login-user-for-integration-test-when-using-jsecurity-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Grails: Few tips for writing integration tests</title>
		<link>http://www.intelligrape.com/blog/2009/11/05/grails-few-tips-for-writing-integration-tests/</link>
		<comments>http://www.intelligrape.com/blog/2009/11/05/grails-few-tips-for-writing-integration-tests/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 19:00:34 +0000</pubDate>
		<dc:creator>Amit Jain</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[controller integration test]]></category>
		<category><![CDATA[integration test]]></category>

		<guid isPermaLink="false">http://www.intelligrape.com/blog/?p=290</guid>
		<description><![CDATA[Just thought I would share with you few tips or key points we should remember while writing integration test cases:
1.  Flush and clear the session object for every single test case for example


class FooTests extends GroovyTestCase {
  def sessionFactory
  void setUp() {
    sessionFactory.currentSession.flush()
    sessionFactory.currentSession.clear()
  }
}


2.  Drop [...]]]></description>
			<content:encoded><![CDATA[<p>Just thought I would share with you few tips or key points we should remember while writing integration test cases:</p>
<p>1.  Flush and clear the session object for every single test case for example</p>
<div class="wp_syntax">
<div class="code">
<pre class="groovy">class FooTests extends GroovyTestCase {
  def sessionFactory
  void setUp() {
    sessionFactory.currentSession.flush()
    sessionFactory.currentSession.clear()
  }
}</pre>
</div>
</div>
<p>2.  Drop and create the test database before you run integration test: Recently I found, after commiting my integration tests in the repository, some started failing and to my surprise, at the same time that failing test passed on my machine. Later I discovered it was because of some database issue. Since then I have made it a practice to drop and recreate the database again before commiting it to the repository, and then run the test cases to ensure that all test passes.</p>
<div class="wp_syntax">
<div class="code">
<pre class="groovy">drop database &lt;myTestDatabase&gt;;
create database &lt;myTestDatabase&gt;;
</pre>
</div>
</div>
<p>Note :If you use in-memory database you may not need to follow this step.</p>
<p>3.  Make different test cases to test different methods or even parts of it defined in a controller or a service: for example</p>
<div class="wp_syntax">
<div class="code">
<pre class="groovy"> void testAccountService_addSavingsAccount() {...}
 void testAccountService_addCheckingAccount() {...}
 void testAccountService_addSavingsAccount_invalidName() {...}  //to test the expected exceptions</pre>
</div>
</div>
<p>This would make your test cases more understandable and focussed on individual task.</p>
<p>4.  Avoid making your test case depedent upon the data. Otherwise there is high probability that it would break if any changes were made to the data</p>
<p>Lets have a look at the sample code for Integration test written for the controller:</p>
<div class="wp_syntax">
<div class="code">
<pre class="groovy">class AccountControllerTests extends GrailsUnitTestCase {
    def accountService
    def sessionFactory
    def renderMap
    def controller

    protected void setUp() {
        super.setUp()
        controller = new AccountController()
        controller.accountService = accountService
        sessionFactory.currentSession.flush()
        sessionFactory.currentSession.clear()
        AccountController.metaClass.render = {Map map -&gt;	//This is to access model when view or template is rendered in the controller
            renderMap = map
        }
    }
   public void testSave() {
        def oldCount = Account.list().size()
        loadParams()
        controller.save()
        assertEquals "Created a new account", renderMap.model.status //accessing model values rendered with view/template
        assertEquals(oldCount + 1, Account.list().size())   //not being dependent on the data
    }

   private void loadParams() {
        def params = [routeTransit: '789456124', accountNumber: '23444', accountType: "ACH",...]
        controller.params.putAll(params)
    }
}</pre>
</div>
</div>
<p>Hope this helped!</p>
<p>Cheers!<br />
~~Amit Jain~~<br />
amit@intelligrape.com<br />
IntelliGrape Softwares</p>
<p>http://www.intelligrape.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.intelligrape.com/blog/2009/11/05/grails-few-tips-for-writing-integration-tests/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

