<?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>Turnleaf Design &#187; Mock</title>
	<atom:link href="http://www.turnleafdesign.com/tag/mock/feed" rel="self" type="application/rss+xml" />
	<link>http://www.turnleafdesign.com</link>
	<description>Ramblings of a junior developer</description>
	<lastBuildDate>Wed, 11 Nov 2009 05:56:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mocking JDBC Connections with MockRunner</title>
		<link>http://www.turnleafdesign.com/mocking-jdbc-connections-with-mockrunner</link>
		<comments>http://www.turnleafdesign.com/mocking-jdbc-connections-with-mockrunner#comments</comments>
		<pubDate>Wed, 11 Nov 2009 05:54:11 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=285</guid>
		<description><![CDATA[Last week I published an article, An Intro into Test Driven Development with Junit4, which got somewhat mixed reviews. One particular commenter on reddit suggested I didn't understand that unit tests shouldn't talk to its data source. I decided to do some investigating into his claims, you can check out my findings here. I was [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fmocking-jdbc-connections-with-mockrunner"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Fmocking-jdbc-connections-with-mockrunner" height="61" width="51" /></a></div><p>Last week I published an article, <a href="  http://www.turnleafdesign.com/an-intro-into-test-driven-development-with-junit4" target="_blank">An Intro into Test Driven Development with Junit4</a>, which got somewhat mixed reviews. One particular commenter on reddit suggested I didn't understand that unit tests <a href="http://www.reddit.com/r/programming/comments/9yq8p/an_intro_into_test_driven_development_with_junit4/c0f29uc" target="_blank">shouldn't talk to its data source</a>. I decided to do some investigating into his claims, you can check out my findings <a href="http://www.turnleafdesign.com/ramblings-should-unit-tests-talk-to-a-data-source" target="_blank">here</a>. I was however intrigued none the less and started looking for a tool that would allow me to easily mock a JDBC connection. I found <a href="http://mockrunner.sourceforge.net/" target="_blank">Mockrunner</a> to be the easiest to use of the ones I have found. Below is a brief tutorial on how to use Mockrunner. (Keep in mind that I am still fairly new to using Mockrunner)<span id="more-285"></span></p>
<p>In the class I am testing I am explicitly calling the columns I am retrieving by name. So I need to add these column names to my mock ResultSet.</p>
<pre class="java" name="code" style="display: none;">
result.addColumn("Date");
result.addColumn("High");
result.addColumn("Low");
result.addColumn("Condition");
result.addColumn("ID");</pre>
<p>Obviously I also retrieve data from a result set so I must also insert data into the mock ResultSet. There are several ways to do this, but the easiest I found was using a list. You must add elements to the list in the same order as you added columns to the ResultSet.</p>
<pre class="java" name="code" style="display: none;">
List&lt;Object&gt; rowItems = new ArrayList&lt;Object&gt;();
rowItems.add(new Date(0));
rowItems.add(72);
rowItems.add(56);
rowItems.add("Cloudy");
rowItems.add(1);</pre>
<p>In my actual test case I can verify that I am actually gathering the contents of the result set correctly and that the correct SQL statement was run.</p>
<pre class="java" name="code" style="display: none;">
prepareRS();
ForecastDAO dao = new ForecastDaoImpl(getConnection());
List&lt;Forecast&gt; forecasts = dao.getAllForecasts();

assertTrue(!forecasts.isEmpty());
verifySQLStatementExecuted("SELECT * FROM forecast");
assertEquals(73, forecasts.get(1).getHigh());</pre>
<p>This is but scratching the surface of MockRunner's potential and using mocks at large. I hope this brief tutorial was helpful in getting you started using MockRunner. You can checkout the entire test class <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/22/trunk/%20forecastaccuracychecker/forecastchecker/test/com/tld/dao/TestForecastDaoImpl.java" target="_blank">here</a> and the class that I am testing <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/20/trunk/%20forecastaccuracychecker/forecastchecker/src/com/tld/dao/ForecastDaoImpl.java" target="_blank">here</a>. If you want to get an entire working version of the project be sure to read the <a href="http://forecastaccuracychecker.googlecode.com/svn/!svn/bc/20/trunk/%20forecastaccuracychecker/forecastchecker/src/com/tld/dao/ForecastDaoImpl.java" target="_blank">technical guide</a>. Let me know if you have any questions or want to add your own comments.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Mock' rel='tag' target='_blank'>Mock</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a>, <a class='technorati-link' href='http://technorati.com/tag/Testing' rel='tag' target='_blank'>Testing</a>, <a class='technorati-link' href='http://technorati.com/tag/Unit+testing' rel='tag' target='_blank'>Unit testing</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/mocking-jdbc-connections-with-mockrunner/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ramblings: Should unit tests talk to a data source?</title>
		<link>http://www.turnleafdesign.com/ramblings-should-unit-tests-talk-to-a-data-source</link>
		<comments>http://www.turnleafdesign.com/ramblings-should-unit-tests-talk-to-a-data-source#comments</comments>
		<pubDate>Thu, 05 Nov 2009 04:48:40 +0000</pubDate>
		<dc:creator>Billy Korando</dc:creator>
				<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Mock]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://www.turnleafdesign.com/?p=280</guid>
		<description><![CDATA[Last week I published an article on test driven development. One person who read my article (briefly) suggested I did not know that unit tests shouldn't talk to their data source. I plan on covering how to mock JDBC connections later this week, however I wanted to do some research to see if what my [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.turnleafdesign.com%2Framblings-should-unit-tests-talk-to-a-data-source"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.turnleafdesign.com%2Framblings-should-unit-tests-talk-to-a-data-source" height="61" width="51" /></a></div><p>Last week I published an article on test driven development. One person who read my article (briefly) suggested I did not know that unit tests <a href="http://www.reddit.com/r/programming/comments/9yq8p/an_intro_into_test_driven_development_with_junit4/c0f29uc" target="_blank">shouldn't talk to their data source</a>. I plan on covering how to mock JDBC connections later this week, however I wanted to do some research to see if what my critic says is an industry standard or a philosophical choice.</p>
<p>Surprisingly there seems to be relatively little information on this subject, or I have been incapable of finding information. From what I have gathered though, the theory that data sources should be mocked is sound, but its may not always be practical to implement it. Below is two reasons why data source connections should be mocked and three reasons why they should not. <span id="more-280"></span></p>
<h3>Why you should mock connections</h3>
<p><strong>Speed –</strong> Talking to a data source is one the primary operations that slows an application's performance. Most data sources require some sort of network connection and retrieving data is often a processor intensive task. Obviously performing these tasks take time. Unit tests are most useful when they are run automatically and frequently to ensure changes being made are not breaking the application. Unit tests that are run automatically should be somewhat speedy, by mocking a data source connection, this can reduce considerably the amount of time it takes to run unit tests.</p>
<p><strong>Remove the reliance on the data source logic –</strong> Unit tests should be testing relatively small parts of code, individual methods and/or small collections of methods. By connecting to a data source you are depending upon the logic of not only how you are connecting, but how the data source operates. This goes beyond the scope of what a unit test should be testing.</p>
<h3>Why you shouldn't mock connections</h3>
<p><strong>It's time consuming –</strong> Writing mocks can be a quite laborious task. I often avoid writing mocks like the plague, because even relatively simple mocks can require a considerable amount of time to get running and running correctly. Even my very simple example of running a single query against a five column table took several hours to setup. Granted this was my first time creating a JDBC mock and future mocks would be easier and quicker to write, but it would still take longer than simply connecting to my database.</p>
<p><strong>It's ugly –</strong> It takes a lot of typing to get even relatively simple mocks working. A lot of the rules that apply to how real business code should be written need not be followed when writing unit tests, that said looking at the code of a unit test shouldn't make your eyes bleed. Unit tests that cannot be easily maintained often become ignored when they break and broken unit test have no value (if ignored).</p>
<p><strong>It's not real – </strong>Mocks don't really care what you put into or take out of them. Real data sources are often not so forgiving. If I misspelled a column or table name in a query, a mock would not pick up on this (you can verify the sql statements run, but again that could be misspelled), where as a real database would. By actually connecting to the real data source you can be more confident that the application will perform its intended tasks.</p>
<h3>Why you should use your head</h3>
<p>Even within the same project there will be instances where mocking a connection to a data source is the right choice and instances when you should actually connect to a real data source. In the early stages of a project using mocks can be more practical as the structure of the data source is more abstract and subject to change. Rewriting a mock is often quicker and easier than restructuring a data source. However as a project matures and the unit tests become more complex, connecting to the real data source may be more practical as it is not only (more) well defined, but writing the mocks begins to require more time. As with all practices, you need decide which one is best to follow (or not) based upon your requirements and restrictions. Though I think it is worth noting standards are standards for a reason. Feel free to weigh in with your own thoughts on the subject.</p>
<p>Additional reading:<br />
<a href="http://www.javaranch.com/journal/2003/12/UnitTestingDatabaseCode.html" target="_blank">http://www.javaranch.com/journal/2003/12/UnitTestingDatabaseCode.html</a><br />
<a href="http://www.buunguyen.net/blog/unit-testing-the-data-access-layer.html" target="_blank">http://www.buunguyen.net/blog/unit-testing-the-data-access-layer.html</a><br />
<script type="text/javascript"><!--
google_ad_client = "pub-3063474103916505";
/* 468x60, created 9/14/09 */
google_ad_slot = "1115297999";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/Best+practices' rel='tag' target='_blank'>Best practices</a>, <a class='technorati-link' href='http://technorati.com/tag/Mock' rel='tag' target='_blank'>Mock</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_blank'>Programming</a>, <a class='technorati-link' href='http://technorati.com/tag/Testing' rel='tag' target='_blank'>Testing</a>, <a class='technorati-link' href='http://technorati.com/tag/Unit+testing' rel='tag' target='_blank'>Unit testing</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.turnleafdesign.com/ramblings-should-unit-tests-talk-to-a-data-source/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
