Unit Testing DAOs with CFCUnit setUp() and tearDown()

2007 November 20
by Paul Marcotte

I recently starting Unit Testing with CFCUnit. Unit testing is not a new topic in the CF community, but it is new to me. I've found that there are a lot of aspects related to unit testing (like Mock objects and stubs) that you need learn about. As I continue to read and research unit testing techniques, I've seen items that go back years. One topic that I could not find a lot of information on is how to test DAOs and the SQL code in <cfquery/> tags. After rephrasing my search terms a few times, I found a great post by Robert Blackburn that also lead me to a suggestion by Paul Kenney on the old CFCDev list to wrap the test within <cftransaction/>. I liked the idea of using <cftransaction/>, but decided to try something a little different.

One of the great features of a unit testing framework (my knowledge is limited to CFCUnit at present), is that for each test in a TestCase, the setUp() and tearDown() methods are called. The setUp() method is used to create the text fixture and tearDown() is used to cleanup the fixture. This makes it a great option to wrap each test in a transaction by adding a couple of methods to begin and rollback a transaction. Here's a snippet of code for a TestCase for a fictitious "UserDAO":

<cfcomponent name="TestUserDAO" extends="org.cfcunit.framework.TestCase">

<cffunction name="setUp" access="public" output="false" hint="">
<cfset var local = StructNew() />
<cfset local.serviceDefinitionLocation = ExpandPath( '/testsuite/coldspring.xml' ) />
<cfset local.beanFactory = CreateObject('component', 'coldspring.beans.DefaultXmlBeanFactory').init() />
<cfset local.beanFactory.loadBeansFromXmlFile(local.serviceDefinitionLocation) />
<cfset setUserDAO(local.beanFactory.getBean('UserDAO')) />
<cfset variables.transaction = 'UserTransaction'>
<cfset beginTransaction() />

<cffunction name="getUserDAO" access="private" output="false" hint="I return the UserDAO.">
<cfreturn variables.instance.UserDAO/>

<cffunction name="setUserDAO" access="private" output="false" hint="I set the UserDAO.">
<cfargument name="UserDAO" required="true" hint="UserDAO" />
<cfset variables.instance.UserDAO= arguments.UserDAO/>

<cffunction name="tearDown" access="public" output="true" returntype="void">
<cfset endTransaction()>

<cffunction name="beginTransaction" access="private" output="false" returntype="void">
<cfset var trans = variables.transaction>
<cfquery name="#trans#" datasource="#getUserDAO().getDatasource()#">

<cffunction name="endTransaction" access="private" output="false" returntype="void">
<cfset var trans = variables.transaction>
<cfquery name="#trans#" datasource="#getUserDAO().getDatasource()#">


Note that the code above is for an MSSQL database. I should also mention that my DAOs also return queries so I have methods like selectAll() in addition to the standard read(), create(), update(), exists(), save() and delete() methods.

Since I'm totally fresh to the unit testing scene, I actually have no idea whether this is a terrible idea or a decent solution. Thoughts, feedback, or opinions are appreciated. I'd rather know early whether I'm on the right track...or not.