<?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>Transient Technology &#187; unitils</title>
	<atom:link href="http://martinaharris.com/tag/unitils/feed/" rel="self" type="application/rss+xml" />
	<link>http://martinaharris.com</link>
	<description>Next time you look it might be gone</description>
	<lastBuildDate>Tue, 24 Jan 2012 18:14:10 +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>Unitils update &#8211; TestListeners</title>
		<link>http://martinaharris.com/2012/01/unitils-update-testlisteners/</link>
		<comments>http://martinaharris.com/2012/01/unitils-update-testlisteners/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 18:08:02 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[news]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[unitils]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=1108</guid>
		<description><![CDATA[In a previous post I got some help from Tim Ducheyne to work out a way to use a spring wired DataSource with Unitils.  He mentioned a better way was coming&#8230; Sometime back now they added the support for TestListener which &#8230; <a href="http://martinaharris.com/2012/01/unitils-update-testlisteners/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In a <a title="How to link unitils to a spring wired datasource" href="http://martinaharris.com/2010/05/unitils-spring-wired-datasource/">previous post</a> I got some help from Tim Ducheyne to work out a way to use a spring wired DataSource with Unitils.  He mentioned a better way was coming&#8230;</p>
<p>Sometime back now they added the support for TestListener which solves this issue.</p>
<p><a title="Unitils - TestListerer API" href="http://www.unitils.org/apidocs/org/unitils/core/TestListener.html">Unitils &#8211; TestListerer API</a></p>
<p>I don&#8217;t work with conventional databases much now, but was intrigued to see this post explaining how to extend unitils and use TestListeners to test ldap.  Neat eh!</p>
<p><a title="unitils library for enhance unit test by using annotation approach" href="http://blog.esofthead.com/unitils-library-for-enhance-unit-test-by-using-annotation-approach/">Extending Unitils to test LDap.</a></p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F&amp;title=Unitils+update+-+TestListeners" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F&amp;title=Unitils+update+-+TestListeners" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F&amp;title=Unitils+update+-+TestListeners" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F&amp;headline=Unitils+update+-+TestListeners" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Unitils+update+-+TestListeners&amp;url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Unitils+update+-+TestListeners&amp;u=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Unitils+update+-+TestListeners&amp;url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Unitils+update+-+TestListeners&amp;url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Unitils+update+-+TestListeners&amp;url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F&amp;title=Unitils+update+-+TestListeners&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2012%2F01%2Funitils-update-testlisteners%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2012/01/unitils-update-testlisteners/';
var dzone_title = 'Unitils update &#8211; TestListeners';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2012/01/unitils-update-testlisteners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to link unitils to a spring wired datasource</title>
		<link>http://martinaharris.com/2010/05/unitils-spring-wired-datasource/</link>
		<comments>http://martinaharris.com/2010/05/unitils-spring-wired-datasource/#comments</comments>
		<pubDate>Mon, 31 May 2010 22:06:35 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[datasource]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[unitils]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=963</guid>
		<description><![CDATA[Unitils provides its own DataSource. This causes problems if you want to use Spring IOC to inject your own. There is a way around this but its not elegant. In future versions of unitils I believe the team is going &#8230; <a href="http://martinaharris.com/2010/05/unitils-spring-wired-datasource/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://unitils.org/" target="_blank">Unitils</a> provides its own DataSource. This causes problems if you want to use Spring IOC to inject your own. There is a way around this but its not elegant. In future versions of unitils I believe the team is going to provide better support for less intrusive methods. This post documents a way to use your own spring defined DataSource.<br />
<span id="more-963"></span><br />
Currently there is a unitils property that allows you to specify your own SpringDataSourceFactory.</p>
<p># unitils.properties</p>
<p># Assign the SpringDataSourceFactory DataSource.<br />
org.unitils.database.config.DataSourceFactory.implClassName= com.tek.design.unitils.SpringDataSourceFactory</p>
<p>Write your SpringDataSourceFactory and implement both DataSourceFactory and springs ApplicationContextAware interfaces. Then in the init method you will be able to fetch your spring DataSource bean from the application context using a service look up. Not ideal, but until future releases it will do just fine.</p>
<pre class="brush: java; gutter: false">package com.tek.design.unitils;

import java.util.Properties;
import javax.sql.DataSource;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.unitils.database.config.DataSourceFactory;

/**
 *
 * Unitil's provides a way to override the default DataSourceFactory.  This class provides facility to
 * spring wire in the same datasource used by your ORM by fetching it from the ApplicationContext.
 *
 * Use the org.unitils.database.config.DataSourceFactory.implClassName property to supply this factory to unitils.
 *
 */
public class SpringDataSourceFactory implements DataSourceFactory, ApplicationContextAware {

    private static ApplicationContext appContext;

    private DataSource dataSource;

    @Override
    public DataSource createDataSource() {
        return this.dataSource;
    }

    @Override
    public void init(Properties arg0) {

        if (null == this.dataSource) {
            String dataSourceBeanName = "TekDesignDataSource";
            this.dataSource = (DataSource) appContext.getBean(dataSourceBeanName);
            if (this.dataSource == null) {
                throw new SpringBeanLookupException(DataSource.class, "Cant find bean named " + dataSourceName);
            }
        }

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appContext = applicationContext;

    }

}</pre>
<h2>Full unitils properties file:</h2>
<p># unitils.properties</p>
<p># Provide a CSV of all used database schemas. ie schema1,schema2.<br />
# The first schema name is the default one.<br />
# If no schema name is specified in a dbunit data set the default one is used.<br />
#<br />
# A schema names are case sensitive when surrounded by database identifier quotes (e.g. &#8220;&#8221; for oracle)<br />
#<br />
# Example unitils description using a schema<br />
#<br />
#&lt;dataset xmlns:frt=&#8221;FRUIT&#8221;&gt;<br />
#   &lt;frt:APPLE NAME=&#8221;COX&#8221;<br />
#                           DESCRIPTION=&#8221;[null]&#8221; /&gt;<br />
#&lt;/dataset&gt;<br />
#<br />
database.schemaNames=fruit,veg</p>
<p># This property specifies the underlying DBMS implementation. Supported values are &#8216;oracle&#8217;, &#8216;db2&#8242;, &#8216;mysql&#8217;, &#8216;hsqldb&#8217;,<br />
# &#8216;postgresql&#8217;, &#8216;derby&#8217; and &#8216;mssql&#8217;. The value of this property defines which vendor specific implementations of<br />
# DbSupport and ConstraintsDisabler are chosen.<br />
database.dialect=oracle</p>
<p># The dao tests often share the same data in the setup files, so its important to<br />
# rollback between tests otherwise constraints will be violated.<br />
DatabaseModule.Transactional.value.default=rollback</p>
<p># Assign the SpringDataSourceFactory DataSource.<br />
org.unitils.database.config.DataSourceFactory.implClassName=<br />
com.tek.design.unitils.SpringDataSourceFactory</p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F&amp;title=How+to+link+unitils+to+a+spring+wired+datasource" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F&amp;title=How+to+link+unitils+to+a+spring+wired+datasource" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F&amp;title=How+to+link+unitils+to+a+spring+wired+datasource" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F&amp;headline=How+to+link+unitils+to+a+spring+wired+datasource" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=How+to+link+unitils+to+a+spring+wired+datasource&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=How+to+link+unitils+to+a+spring+wired+datasource&amp;u=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=How+to+link+unitils+to+a+spring+wired+datasource&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=How+to+link+unitils+to+a+spring+wired+datasource&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=How+to+link+unitils+to+a+spring+wired+datasource&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F&amp;title=How+to+link+unitils+to+a+spring+wired+datasource&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2010%2F05%2Funitils-spring-wired-datasource%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2010/05/unitils-spring-wired-datasource/';
var dzone_title = 'How to link unitils to a spring wired datasource';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2010/05/unitils-spring-wired-datasource/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unitils update &#8211; Its great!</title>
		<link>http://martinaharris.com/2009/12/unitils-update-dao-testing/</link>
		<comments>http://martinaharris.com/2009/12/unitils-update-dao-testing/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 05:00:57 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[unitils]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=416</guid>
		<description><![CDATA[In a previous post I looked at <a title="Testing the JPA DAO" href="http://martinaharris.com/2009/11/lessons-on-testing-jpa-dao/ " target="_blank">Unitils</a>.  This is a quick update: Yes its great but the product could do with some polish, better documentation, bug fixing and some api revision.  Despite that its still the best system I have looked at to test a DAO.  Also the project is alive and kicking, it looks like there is more to come in future revisions.  See the full post for more detail. <a href="http://martinaharris.com/2009/12/unitils-update-dao-testing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div>In a previous post I looked at <a title="Testing the JPA DAO" href="http://martinaharris.com/2009/11/lessons-on-testing-jpa-dao/ " target="_blank">Unitils</a>.  This is a quick update.</div>
<div>
<h2>Conclusion</h2>
<p>Yes its great but the product could do with some polish, better documentation, bug fixing and some api revision.  Despite that its still the best system I have looked at to test a DAO.  Also the project is alive and kicking, it looks like there is more to come in future revisions.<br />
<span id="more-416"></span></p>
<h2>Technical Detail</h2>
<ul>
<li>Unitils provides two handy annotations, @DataSet and @ExpectedDataset.  DataSet is used to specify xml describing tables, colums and data to be setup before the test.  The workflow for basic testing is a joy, and very test driven:
<ol>
<li>Setup empty test method with annotation for file or files describing the data.</li>
<li>Add files and extend data until you no longer have foreign key insert failures.  Enrich data until your happy.</li>
<li>Add dao search call.</li>
<li>Add assertions and simple expectations.</li>
<li>Tidy test code.</li>
</ol>
</li>
<li>@ExpectedDataset makes insert, update and delete tests very simple.  Once you have your dataset, you just copy the file, add, delete or amend a record, watch the test fail, then add the dao calls to fix it. Nb. see below, delete is not working in 2.1.</li>
<li>Very good test failure reporting with lists of differences.</li>
<li>assertReflectionPropertyEquals is not helpful in my demo model because I can&#8217;t get at the identifiers, so you get null or 0 match failures.  I could not get IGNORE_DEFAULTS to work.  Not too much of an issue as its possible to hand assert by building standard validation routines and these are little effort.  There is an incarnation that has this signature:</li>
<pre class="brush: java; light: true">assertPropertyReflectionEquals(java.lang.String propertyName, java.util.Collection expectedPropertyValues, java.util.Collection actualObjects, ReflectionComparatorMode... modes)</pre>
<p>That one I liked a lot, as I could check a single property across a collection, with a range of values.  Nice!<br />
The next one I am not so keen on.</p>
<pre class="brush: java; light: true">assertPropertyLenientEquals("id", 1, user);</pre>
<p>I would rather use this, as I find it more descriptive, and type safe.</p>
<pre class="brush: java; light: true">assertEquals(1L, user.getId);</pre>
<li>There seems to be a problem inserting into self referencing or pigs ear tables, and inserting null into colums, but I have not spent much time on it, this may be me misusing the api.</li>
<li>Missing record deltas do not work. I.e If your @ExpectedDataSet describes one less record than @DataSet, and you don&#8217;t delete the test should fail, it does not.</li>
</ul>
</div>
<p>In the main though I would use it should the need arise.</p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F&amp;title=Unitils+update+-+Its+great%21" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F&amp;title=Unitils+update+-+Its+great%21" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F&amp;title=Unitils+update+-+Its+great%21" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F&amp;headline=Unitils+update+-+Its+great%21" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Unitils+update+-+Its+great%21&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Unitils+update+-+Its+great%21&amp;u=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Unitils+update+-+Its+great%21&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Unitils+update+-+Its+great%21&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Unitils+update+-+Its+great%21&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F&amp;title=Unitils+update+-+Its+great%21&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F12%2Funitils-update-dao-testing%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2009/12/unitils-update-dao-testing/';
var dzone_title = 'Unitils update &#8211; Its great!';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2009/12/unitils-update-dao-testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lessons on testing a JPA Dao</title>
		<link>http://martinaharris.com/2009/11/lessons-on-testing-jpa-dao/</link>
		<comments>http://martinaharris.com/2009/11/lessons-on-testing-jpa-dao/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 00:50:24 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[unitils]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=339</guid>
		<description><![CDATA[I wanted to explore unit testing JPA DAO and models. Hand crafting solutions is quite time consuming. I found something called Unitils which refines another project Dbunit. In theory it should significantly reduce the complexity and save some time. So one Saturday, I sat down to explore the space and write this blog. <a href="http://martinaharris.com/2009/11/lessons-on-testing-jpa-dao/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I wanted to explore unit testing JPA DAO and models.  Hand crafting solutions is quite time consuming.  I found something called <a href="http://unitils.org/">Unitils</a> which refines another project <a href="http://www.dbunit.org/">Dbunit</a>.  In theory it should significantly reduce the complexity and save some time.  So one Saturday, I sat down to explore the space and write this blog.</p>
<p><span id="more-339"></span><br />
One way to test a DAO method is to provide a data fixture written in java.  This will provide a populated in memory database and immutable objects that contain the data inserted.  The immutable objects can then be used later for making assertions against the data returned from the DAO method under test.  This describes a similar pattern: <a title="Db Unit Test with in memory Db" href="http://www.theserverside.com/tt/articles/article.tss?l=UnitTesting" target="_blank">Unit Test with in memory Db</a>.  I wrote a more modern version using the EntityManager instead of Jdbc and SpringTests with Junit 4 but the principle is the same.</p>
<p>There are some rules for my data fixture:</p>
<ul>
<li>The data must not be inserted using the DAO as this means your relying on the DAO under test.  <em>I used the entity manager providing direct insert statements and just executed the sql.</em></li>
<li>The data inserted must be driven from some immutable holder objects that encapsulate that data.  This provides a robust test and some handy containers that are used later in the test.  This also means that the test has no need whatsoever to know anything about the data!  It just needs to make assertions that the data holders contain the same data as the model returned from the dao.</li>
<li>Assert that default values are not used.  I have seen bugs because &#8217;0&#8242; was used as data in <strong>long</strong> type identifiers, then it turns out the code never set the variable.  <em>My data holders validate their invariants.</em></li>
<li>The fixture should be responsible for stringing together primary and foreign keys, avoiding problems with uniqueness etc.  I.e the fixture is aware of the database requirements.</li>
<li>The fixture should allow for several generations of itself to be created.  Thus from the test, several rows of data and the associated graph can be created to provide a rich enough set.  Rich enough to provide good data coverage without being complex or slow.</li>
<li>Finally labels, names and other data should be identifiable, this makes it easier to debug if required.</li>
</ul>
<p>So with careful thought, use of the builder pattern for the main fixture, immutable object design and a hierarchy of data and object creation I arrive at the finished product.  The design is strong, the test works and it satisfies the above requirements, but<strong> I am not happy.</strong></p>
<p>It took too long to hand craft all this code.  The fixture is big although easy to understand.  There has to be a better way.  So I look for an alternative.  What I want is something that provides the above as far as possible but lighter.  I envisaged, something where the data is configurable and you don&#8217;t have to write the complex fixture at all.</p>
<h2>Unitils a possible shortcut</h2>
<p>Quoting from the Unitils site: <cite>Unit tests for the database layer can be extremely valuable when building enterprise applications, but are often abandoned because of their complexity.<br />
</cite> How true that is!</p>
<p>So my search took me through the links at the bottom of the page, until I hit upon <strong>Unitils</strong>: <a title="Unitils.org Cookbook" href="http://unitils.org/cookbook.html" target="_blank">http://unitils.org/cookbook.html</a>.  Instead of writing a complex fixture to encapsulate the data safely you write XML that describes the data needed for a given test.  This is associated with the class or method using a naming strategy, although this can be overridden by specifying files.  This could be handy if one dataset fits several test classes.  This is already easier than writing actual insert statements and can be achieved easily with the database schema browser and a text editor.  I am not interested in the mocking or database setup described there, whilst useful I had solved most of those issues with spring.  In fact unitils uses spring under the hood, so is probably using the same facilities I do in my work.  Unitils provides some powerful aid with the assertions in the test too, take a look at this!</p>
<h3>ReflectionAssert</h3>
<p>The reflection assert class provides a toolkit that addresses the kind of problems you often run into when making assertions against the data a DAO returns.  For instance lets say you have a model object with a primary key value.  This field is often hidden from you, and may not be used in the equals method.  In fact the equals methods on model objects are not always implemented or have some business specific implementation, so you can&#8217;t rely on them in the tests.  My solution was to use my own holder objects for data, and then to write my own validation routines against the model object.  Time consuming!</p>
<p>Take a look at these examples taken directly from the tutorial, (thanks guys!)</p>
<pre class="brush: java; light: true">import static org.unitils.reflectionassert.ReflectionAssert.*;

// Exact field-by-field comparison
assertReflectionEquals(
  new Person("John", "Doe", new Address("New street", 5, "Brussels")),
  new Person("John", "Doe", new Address("New street", 5, "Brussels"));

// Ignore Null / 0 values in the expected object
assertReflectionEquals(
  new Person("John", null, new Address("New street", 0, null)),
  new Person("John", "Doe", new Address("New street", 5, "Brussels"),
ReflectionComparatorMode.IGNORE_DEFAULTS); 

// Ignore collection order
assertReflectionEquals(
Arrays.asList(
  new Person("John"), new Person("Jane")),
  new Person[] {new Person("Jane"), new Person("John")},
ReflectionComparatorMode.LENIENT_ORDER);

// Ignore null/0 values + collection order
assertLenientEquals(
  Arrays.asList(
  new Person("John"), null),
  new Person[] {new Person("Jane", "Doe"), new Person("John", "Doe")});

// Check only the firstName property
assertPropertyLenientEquals("firstName", Arrays.asList("John", "Jane"),
  new Person[] {new Person("Jane", "Doe"), new Person("John", "Doe")});</pre>
<h3>Comments on the assertion example</h3>
<ul>
<li>Note the way its possible to ignore null and defaults like &#8217;0&#8242; and certain fields.  This should mean I can use constructors on my model and not have to worry about keys.  These are tested because you get data back in the child tables anyway.</li>
<li>Note the system for ignoring order.  I use google multiset but this is even simpler than that.</li>
</ul>
<h3>General Problems that might still arise</h3>
<p>I am not sure if my model is always going to be so accommodating.  I hope that visibility of appropriate constructors will allow the assertions I want to make via reflection.  I guess there is little impact in adding more for testing, but my design meant I did not have to worry about it because my assertions are made against an object provided by the fixture.  In fact the design of this object was to facilitate creation without complex constructors.  A constraint the model does not have to worry about.  i.e Only the DAO needs to create a model object  This also had the advantage of reducing the failure to one point, the DAO.  In the system above, you type data into the XML and into the test, so its possible to miss the match and the test will fail.</p>
<p>It will be interesting to see if its possible to work with nested objects and complex graphs. JPA or currently Hibernate is knitting things together so it may not be possible to create the objects required.  Its going to take a bit longer to finish this off, so I will explore further and post up my finished result later on.</p>
<p>If I had a request it would be for this framework to load a model object off the xml defining the data, and have an assertions like this:</p>
<pre class="brush: java; light: true">public void validateModelLoadedFromTestDataAgainstReturnedModel(T y)</pre>
<p>This would close the loop would it not?</p>
<h2>Links</h2>
<p><a title="DBUnit" href="http://www.dallaway.com/acad/dbunit.html" target="_blank">http://www.dallaway.com/acad/dbunit.html</a></p>
<p><a title="Article from the server side describing a good test pattern for DAO's" href="http://www.theserverside.com/tt/articles/article.tss?l=UnitTesting" target="_blank">http://www.theserverside.com/tt/articles/article.tss?l=UnitTesting</a></p>
<p><a title="Unitils testing with JPA" href="http://unitils.org/tutorial.html#Testing_with_JPA" target="_blank">http://unitils.org/tutorial.html#Testing_with_JPA</a></p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 114px; width: 1px; height: 1px;">http://www.theserverside.com/tt/articles/article.tss?l=UnitTestinghttp://www.theserverside.com/tt/articles/article.tss?l=UnitTesting</div>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F&amp;title=Lessons+on+testing+a+JPA+Dao" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F&amp;title=Lessons+on+testing+a+JPA+Dao" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F&amp;title=Lessons+on+testing+a+JPA+Dao" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F&amp;headline=Lessons+on+testing+a+JPA+Dao" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Lessons+on+testing+a+JPA+Dao&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Lessons+on+testing+a+JPA+Dao&amp;u=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Lessons+on+testing+a+JPA+Dao&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Lessons+on+testing+a+JPA+Dao&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Lessons+on+testing+a+JPA+Dao&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F&amp;title=Lessons+on+testing+a+JPA+Dao&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F11%2Flessons-on-testing-jpa-dao%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2009/11/lessons-on-testing-jpa-dao/';
var dzone_title = 'Lessons on testing a JPA Dao';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2009/11/lessons-on-testing-jpa-dao/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

