<?xml version="1.0" encoding="utf-8"?>

			<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cc="http://web.resource.org/cc/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">

			<channel>
			<title>Brian Kotek: Inversion of Control - ColdMock</title>
			<link>http://www.briankotek.com/blog/index.cfm</link>
			<description>Brian Kotek on ColdFusion, Flex, AIR, Java, Groovy, Design Patterns, and Object-Oriented Programming</description>
			<language>en-us</language>
			<pubDate>Fri, 10 Sep 2010 01:04:04 -0700</pubDate>
			<lastBuildDate>Tue, 15 Jul 2008 13:44:00 -0700</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>brian428@briankotek.com</managingEditor>
			<webMaster>brian428@briankotek.com</webMaster>
			<itunes:subtitle></itunes:subtitle>
			<itunes:summary></itunes:summary>
			<itunes:category text="Technology" />
			<itunes:category text="Technology">
				<itunes:category text="Podcasting" />
			</itunes:category>
			<itunes:category text="Technology">
				<itunes:category text="Tech News" />
			</itunes:category>
			<itunes:keywords></itunes:keywords>
			<itunes:author></itunes:author>
			<itunes:owner>
				<itunes:email>brian428@briankotek.com</itunes:email>
				<itunes:name></itunes:name>
			</itunes:owner>
			
			<itunes:explicit>no</itunes:explicit>
			
			
			
			
			
			<item>
				<title>My IECFUG Presentation Recording and Files</title>
				<link>http://www.briankotek.com/blog/index.cfm/2008/7/15/My-IECFUG-Presentation-Recording-and-Files</link>
				<description>
				
				Last Friday I gave a presentation to the Inland Empire ColdFusion User Group. It covered a range of topics: &lt;a href=&quot;http://www.mxunit.org&quot; target=&quot;_blank&quot;&gt;MXUnit&lt;/a&gt;, &lt;a href=&quot;http://coldmock.riaforge.org/&quot; target=&quot;_blank&quot;&gt;ColdMock&lt;/a&gt;, and elements of my RIAForge &lt;a href=&quot;http://formutils.riaforge.org/&quot; target=&quot;_blank&quot;&gt;FormUtilities&lt;/a&gt; and &lt;a href=&quot;http://coldspringutils.riaforge.org/&quot; target=&quot;_blank&quot;&gt;ColdSpring Bean Utilities&lt;/a&gt; projects.

The presentation went well, I think. There are a few technical issues towards the end when they were trying to set up a microphone to get a Q&amp;A going, but they are minor. I&apos;ve linked to the files and the recording below in case anyone is interested in having a look. Thanks again to Sami, Luis, and the IECFUG for allowing me to present!

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.briankotek.com/blog/files/iecfug_presentation_brian_kotek.zip&quot; target=&quot;_blank&quot;&gt;PowerPoint presentation and associated code examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://adobechats.adobe.acrobat.com/p58908414/&quot; target=&quot;_blank&quot;&gt;Connect recording&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
				
				</description>
				
				
				<category>ColdFusion</category>
				
				<category>Unit Testing</category>
				
				<category>ColdSpring</category>
				
				<category>FormUtilities CFC</category>
				
				<category>ColdMock</category>
				
				<category>Presentations</category>
				
				<pubDate>Tue, 15 Jul 2008 13:44:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2008/7/15/My-IECFUG-Presentation-Recording-and-Files</guid>
				
				
			</item>
			
		 	
			
			
			<item>
				<title>Small Update to ColdMock</title>
				<link>http://www.briankotek.com/blog/index.cfm/2008/3/21/Small-Update-to-ColdMock</link>
				<description>
				
				In response to a thread on the MXUnit forums about mocking, I&apos;ve made a quick update to &lt;a href=&quot;http://coldmock.riaforge.org/&quot; target=&quot;_blank&quot;&gt;ColdMock&lt;/a&gt;. I added a method named methodCallCount(methodName) to the mock objects. This will return the number of times that the specified method has been called up to the current execution point. This might be useful if you are testing a component that makes calls to a mock object but doesn&apos;t return anything or returns void, to ensure that the call to the mock object actually happened as expected.

So you can do something like:

&lt;code&gt;
Call a method that retuns void: &lt;cfset facade.doSomething() /&gt;
This call to facade.methodCallCount(&apos;doSomething&apos;) should be 1: #facade.methodCallCount(&apos;doSomething&apos;)#
&lt;/code&gt;

I also added a debug method called coldMock_debug() to the mocked objects to view the internal variables scope aid in any debugging issues with mocking.

Happy mocking! nyah nyah! Oh, not that kind of mocking. ;-)
				
				</description>
				
				
				<category>ColdFusion</category>
				
				<category>Unit Testing</category>
				
				<category>ColdMock</category>
				
				<pubDate>Fri, 21 Mar 2008 10:15:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2008/3/21/Small-Update-to-ColdMock</guid>
				
				
			</item>
			
		 	
			
			
			<item>
				<title>BeanInjector Update (now at RIAForge!) and ColdMock update</title>
				<link>http://www.briankotek.com/blog/index.cfm/2008/3/12/BeanInjector-Update-now-at-RIAForge-and-ColdMock-update</link>
				<description>
				
				I&apos;ve updated the BeanInjector component and have created an RIAForge project to host these until we can figure out where these will live in the ColdSpring repository. The ColdSpring Bean Utilities project is at &lt;a href=&quot;http://coldspringutils.riaforge.org/&quot; target=&quot;_blank&quot;&gt;http://coldspringutils.riaforge.org/&lt;/a&gt;. It includes the BeanInjector and TDOBeanInjectorObserver, as well as the AbstractMetadataAwareAdvice, VOConverterAdvice, and GenericVOConverter. A zip file and Subversion access are available at RIAForge.

There is an update to the BeanInjector specifically. Bob Silverberg noticed a non-var-scoped variable during the initial caching of the metadata for the target object. This shouldn&apos;t actually affect anyone since the code lives in an exclusive lock, but better safe than sorry. Thanks Bob!

There&apos;s also a small update to ColdMock. You can now pass an optional argument named &quot;isVirtualMethod&quot; when you define a mock method. By default this is false, but if you specify true, ColdMock will skip all checks for the existence of the method, as well as argument and return type checks. This is useful if you want to mock a method that is injected dynamically into the component, since the metadata for the object will have no information on the dynamic method. So basically, you can define what the method will return, but you&apos;re on your own as far as making sure it is accepting and returning the proper types.

I hope folks find these useful!
				
				</description>
				
				
				<category>ColdFusion</category>
				
				<category>ColdSpring</category>
				
				<category>ColdMock</category>
				
				<pubDate>Wed, 12 Mar 2008 15:02:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2008/3/12/BeanInjector-Update-now-at-RIAForge-and-ColdMock-update</guid>
				
				
			</item>
			
		 	
			
			
			<item>
				<title>Unit Testing Transfer Decorators with CFCUnit, BeanInjector, and ColdMock</title>
				<link>http://www.briankotek.com/blog/index.cfm/2008/1/28/Unit-Testing-Transfer-Decorators-with-CFCUnit-BeanInjector-and-ColdMock</link>
				<description>
				
				Paul Marcotte asked me to post about how one might unit test a Transfer Decorator. So here you go Paul (and anyone else who might be interested) :-) .

To start with, basic testing of a Transfer Decorator is really no different than testing any other component. First, I set up a ColdSpring file to use for the test. Note that, depending on how many things you need to test and what you&apos;re testing, you can use the same ColdSpring file to test numerous things. In other words, you don&apos;t necessarily have to have a separate ColdSpring XML file for every test.

&lt;code&gt;
&lt;beans&gt;
	
	&lt;bean id=&quot;transferFactory&quot; class=&quot;transfer.transferFactory&quot;&gt;
	   &lt;constructor-arg name=&quot;datasourcePath&quot;&gt;&lt;value&gt;/tests/transfer/testing/datasource.xml&lt;/value&gt;&lt;/constructor-arg&gt;
	   &lt;constructor-arg name=&quot;configPath&quot;&gt;&lt;value&gt;/tests/transfer/testing/transfer.xml&lt;/value&gt;&lt;/constructor-arg&gt;
	   &lt;constructor-arg name=&quot;definitionPath&quot;&gt;&lt;value&gt;/tests/transfer/testing/definitions&lt;/value&gt;&lt;/constructor-arg&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;datasource&quot; factory-bean=&quot;transferFactory&quot; factory-method=&quot;getDatasource&quot; /&gt;

	&lt;bean id=&quot;transfer&quot; factory-bean=&quot;transferFactory&quot; factory-method=&quot;getTransfer&quot; /&gt;
	
&lt;/beans&gt;
&lt;/code&gt;

From here, the unit test might look like this (I&apos;m using CFCUnit but the approach in CFUnit would be similar):

&lt;code&gt;
&lt;cfcomponent name=&quot;TestRegisteredUser&quot; extends=&quot;org.cfcunit.framework.TestCase&quot;&gt;
	&lt;cffunction name=&quot;setUp&quot; access=&quot;public&quot; output=&quot;false&quot; hint=&quot;&quot;&gt;
		&lt;cfset var local = StructNew() /&gt;
		&lt;cfset local.config = &apos;/tests/transfer/testing/coldspring.xml.cfm&apos; /&gt;
		&lt;cfset beanFactory = CreateObject(&apos;component&apos;, &apos;coldspring.beans.DefaultXmlBeanFactory&apos;).init() /&gt;
		&lt;cfset beanFactory.loadBeansFromXmlFile(beanDefinitionFile=local.config) /&gt;
		&lt;cfset setRegisteredUser(beanFactory.getBean(&apos;transfer&apos;).get(&apos;user&apos;, 2)) /&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction name=&quot;test_customDecoratorMethod&quot; returntype=&quot;void&quot; access=&quot;public&quot; output=&quot;false&quot; hint=&quot;&quot;&gt;
		&lt;cfset var local = StructNew() /&gt;
		&lt;cfset assertEqualsString(&quot;This is a custom method in the Decorator.&quot;, getRegisteredUser().customDecoratorMethod(), &apos;Custom decorator response does not match expected value.&apos;) /&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction name=&quot;getRegisteredUser&quot; access=&quot;private&quot; output=&quot;false&quot; hint=&quot;I return the RegisteredUser.&quot;&gt;
		&lt;cfreturn variables.instance.RegisteredUser /&gt;
	&lt;/cffunction&gt;
		
	&lt;cffunction name=&quot;setRegisteredUser&quot; access=&quot;private&quot; output=&quot;false&quot; hint=&quot;I set the RegisteredUser.&quot;&gt;
		&lt;cfargument name=&quot;RegisteredUser&quot; required=&quot;true&quot; hint=&quot;RegisteredUser&quot; /&gt;
		&lt;cfset variables.instance.RegisteredUser = arguments.RegisteredUser /&gt;
	&lt;/cffunction&gt;
	
&lt;/cfcomponent&gt;
&lt;/code&gt;

As you can see, you grab the Decorator from Transfer, and then are free to test the methods in the Decorator the same way you would test any other CFC method.

To take this further, you can actually mix a number of the techniques that I&apos;ve talked about to handle more complex tests. In this example, I&apos;m using the TDOBeanInjectorObserver to wire in a ValidatorFactory to my Decorator. However, to keep the test isolated to the Decorator, I&apos;m using ColdMock to create a mock of the ValidatorFactory and injecting that into the Decorator:

&lt;code&gt;
&lt;beans&gt;
	
	&lt;bean id=&quot;transferFactory&quot; class=&quot;transfer.transferFactory&quot;&gt;
	   &lt;constructor-arg name=&quot;datasourcePath&quot;&gt;&lt;value&gt;/tests/transfer/testing/datasource.xml&lt;/value&gt;&lt;/constructor-arg&gt;
	   &lt;constructor-arg name=&quot;configPath&quot;&gt;&lt;value&gt;/tests/transfer/testing/transfer.xml&lt;/value&gt;&lt;/constructor-arg&gt;
	   &lt;constructor-arg name=&quot;definitionPath&quot;&gt;&lt;value&gt;/tests/transfer/testing/definitions&lt;/value&gt;&lt;/constructor-arg&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;datasource&quot; factory-bean=&quot;transferFactory&quot; factory-method=&quot;getDatasource&quot; /&gt;

	&lt;bean id=&quot;transfer&quot; factory-bean=&quot;transferFactory&quot; factory-method=&quot;getTransfer&quot; /&gt;
	
	&lt;bean id=&quot;validatorFactory&quot; factory-bean=&quot;MockFactory&quot; factory-method=&quot;createMock&quot;&gt;
		&lt;constructor-arg name=&quot;objectToMock&quot;&gt;
			&lt;value&gt;tests.transfer.testing.ValidatorFactory&lt;/value&gt;
		&lt;/constructor-arg&gt;
	&lt;/bean&gt;
	&lt;bean id=&quot;MockFactory&quot; class=&quot;coldmock.MockFactory&quot; /&gt;
	
	&lt;bean id=&quot;beanInjector&quot; class=&quot;common.components.utility.BeanInjector&quot;&gt;
		&lt;constructor-arg name=&quot;debugMode&quot;&gt;&lt;value&gt;false&lt;/value&gt;&lt;/constructor-arg&gt;
		&lt;constructor-arg name=&quot;suffixList&quot;&gt;&lt;value&gt;&lt;/value&gt;&lt;/constructor-arg&gt;
	&lt;/bean&gt;
	
	&lt;bean id=&quot;TDOBeanInjectorObserver&quot; class=&quot;common.components.transfer.TDOBeanInjectorObserver&quot; lazy-init=&quot;false&quot;&gt;
		&lt;constructor-arg name=&quot;transfer&quot;&gt;
			&lt;ref bean=&quot;transfer&quot; /&gt;
		&lt;/constructor-arg&gt;
		&lt;property name=&quot;beanInjector&quot;&gt;
			&lt;ref bean=&quot;beanInjector&quot; /&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
	
&lt;/beans&gt;
&lt;/code&gt;

This way, I can test any methods in the Decorator that actually rely on the ValidatorFactory by mocking the results from the ValidatorFactory method calls:

&lt;code&gt;
&lt;cfcomponent name=&quot;TestRegisteredUser&quot; extends=&quot;org.cfcunit.framework.TestCase&quot;&gt;
	&lt;cffunction name=&quot;setUp&quot; access=&quot;public&quot; output=&quot;false&quot; hint=&quot;&quot;&gt;
		&lt;cfset var local = StructNew() /&gt;
		&lt;cfset local.config = &apos;/tests/transfer/testing/coldspring.xml.cfm&apos; /&gt;
		&lt;cfset beanFactory = CreateObject(&apos;component&apos;, &apos;coldspring.beans.DefaultXmlBeanFactory&apos;).init() /&gt;
		&lt;cfset beanFactory.loadBeansFromXmlFile(beanDefinitionFile=local.config) /&gt;
		&lt;cfset setRegisteredUser(beanFactory.getBean(&apos;transfer&apos;).get(&apos;user&apos;, 2)) /&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction name=&quot;test_customDecoratorMethodUsingValidator&quot; returntype=&quot;void&quot; access=&quot;public&quot; output=&quot;false&quot; hint=&quot;&quot;&gt;
		&lt;cfset var local = StructNew() /&gt;
		&lt;cfset local.validator = beanFactory.getBean(&apos;validatorFactory&apos;) /&gt;
		&lt;cfset local.validator.returns(&quot;This is the MOCKED ValidatorFactory method.&quot;) /&gt;
		&lt;cfset assertEqualsString(&quot;This is the MOCKED ValidatorFactory method.&quot;, getRegisteredUser().customDecoratorMethodUsingValidator(), &apos;Custom decorator using validator response does not match expected value.&apos;) /&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction name=&quot;getRegisteredUser&quot; access=&quot;private&quot; output=&quot;false&quot; hint=&quot;I return the RegisteredUser.&quot;&gt;
		&lt;cfreturn variables.instance.RegisteredUser /&gt;
	&lt;/cffunction&gt;
		
	&lt;cffunction name=&quot;setRegisteredUser&quot; access=&quot;private&quot; output=&quot;false&quot; hint=&quot;I set the RegisteredUser.&quot;&gt;
		&lt;cfargument name=&quot;RegisteredUser&quot; required=&quot;true&quot; hint=&quot;RegisteredUser&quot; /&gt;
		&lt;cfset variables.instance.RegisteredUser = arguments.RegisteredUser /&gt;
	&lt;/cffunction&gt;
	
&lt;/cfcomponent&gt;
&lt;/code&gt;

Just for reference, the Decorator I&apos;m using in these test examples looks like this:

&lt;code&gt;
&lt;cfcomponent hint=&quot;Registered User&quot; extends=&quot;transfer.com.TransferDecorator&quot; output=&quot;false&quot;&gt;
	
	&lt;cffunction name=&quot;customDecoratorMethod&quot; access=&quot;public&quot; returntype=&quot;string&quot; output=&quot;false&quot; hint=&quot;&quot;&gt;
		&lt;cfreturn &quot;This is a custom method in the Decorator.&quot; /&gt;		
	&lt;/cffunction&gt;
	
	&lt;cffunction name=&quot;customDecoratorMethodUsingValidator&quot; access=&quot;public&quot; returntype=&quot;string&quot; output=&quot;false&quot; hint=&quot;&quot;&gt;
		&lt;cfreturn getValidatorFactory().validatorFactoryMethod() /&gt;		
	&lt;/cffunction&gt;
	
	&lt;cffunction name=&quot;getValidatorFactory&quot; access=&quot;public&quot; returntype=&quot;any&quot; output=&quot;false&quot; hint=&quot;I return the ValidatorFactory.&quot;&gt;
		&lt;cfreturn variables.instance.validatorFactory /&gt;
	&lt;/cffunction&gt;
		
	&lt;cffunction name=&quot;setValidatorFactory&quot; access=&quot;public&quot; returntype=&quot;void&quot; output=&quot;false&quot; hint=&quot;I set the ValidatorFactory.&quot;&gt;
		&lt;cfargument name=&quot;validatorFactory&quot; type=&quot;any&quot; required=&quot;true&quot; hint=&quot;ValidatorFactory&quot; /&gt;
		&lt;cfset variables.instance.validatorFactory = arguments.validatorFactory /&gt;
	&lt;/cffunction&gt;
	
&lt;/cfcomponent&gt;
&lt;/code&gt;

Hopefully that helps clear up how one might go about testing a Transfer Decorator. It also shows how you can use unit testing, ColdMock, and the Transfer Bean Injector together. Please let me know what you think or if you have any questions or comments about how I&apos;m doing this.
				
				</description>
				
				
				<category>Transfer</category>
				
				<category>ColdFusion</category>
				
				<category>Unit Testing</category>
				
				<category>ColdSpring</category>
				
				<category>ColdMock</category>
				
				<pubDate>Mon, 28 Jan 2008 12:18:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2008/1/28/Unit-Testing-Transfer-Decorators-with-CFCUnit-BeanInjector-and-ColdMock</guid>
				
				
			</item>
			
		 	
			
			
			<item>
				<title>ColdMock and CFC Stub Generator Updates</title>
				<link>http://www.briankotek.com/blog/index.cfm/2007/11/14/ColdMock-and-CFC-Stub-Generator-Updates</link>
				<description>
				
				Just a quick note about some updates to the RIAForge projects for &lt;a href=&quot;http://coldmock.riaforge.org/&quot; target=&quot;_blank&quot;&gt;ColdMock&lt;/a&gt; and the &lt;a href=&quot;http://cfcstub.riaforge.org/&quot; target=&quot;_blank&quot;&gt;CFC Stub Generator&lt;/a&gt;.

ColdMock had an issue when trying to mock a method that was defined in a superclass of the target object. This has been corrected. It will also validate arrays of components as return types (i.e. User[]) (thanks to Jo&#xe3;o Fernandes for contributing this fix).

The CFC Stub Generator has some significant additions as well. The tool now supports generating private and public properties if you define these in the UML. Public properties are set in the THIS scope, and associated cfproperty tags are created. 

The tool also supports existing CFCs now. It will not overwrite existing components, but will still generate the other files (unit tests, ColdSpring, etc.). This will allow you to quickly generate unit tests for an existing set of components by spec&apos;ing them out in UML.

You can also set up alternate package hierarchies in the tool to define paths to CFCs that are outside of your project (for example, Transfer), and mark a CFC as &quot;active&quot; in the UML tool to instruct the generator to use this path as opposed to the root path you defined for your main project. You must set up the alternate package path and set the base package for the alternate packages as &quot;root&quot; in the UML tool. In this way, the generator will know the proper paths to the external components for typing and ColdSpring path creation. Examples of this setup are included in the bookstore2.zuml/bookstore2.xmi files.

Finally, the tool now has an option to use ColdMock instead of generating staic mock CFC files, and configure the unit tests and the generated ColdSpring file to use the MockFactory to use in the unit tests (requires ColdMock and CF8).
				
				</description>
				
				
				<category>ColdFusion</category>
				
				<category>CFC Stub Generator</category>
				
				<category>Unit Testing</category>
				
				<category>OOP CF</category>
				
				<category>ColdMock</category>
				
				<pubDate>Wed, 14 Nov 2007 12:01:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2007/11/14/ColdMock-and-CFC-Stub-Generator-Updates</guid>
				
				
			</item>
			
		 	
			
			
			<item>
				<title>ColdMock Now Available at RIAForge</title>
				<link>http://www.briankotek.com/blog/index.cfm/2007/8/30/ColdMock-Now-Available-at-RIAForge</link>
				<description>
				
				OK I&apos;ve finished up my initial work on ColdMock and have released it as a &lt;a href=&quot;http://coldmock.riaforge.org/&quot; target=&quot;_blank&quot;&gt;project on RIAForge&lt;/a&gt;. The zip file and SVN repository contain the ColdMock factory as well as some examples of how it can be used.

I added what I think is a crucial element: validation of arguments and return values for the mocked methods. This was actually pretty challenging to add. This is because once the MockFactory creates an instance of your component, it purges all of the existing methods from that instance. It must do this so that your original methods don&apos;t run, since that defeats the whole point of using a Mock object. You don&apos;t want the original methods to run, since they will have code in them or references to other objects in them. Mocks just return the dummy data you specify. However, purging all the existing methods also means all the arguments, argument types, and return types are also gone.

To work around this, the Mock object uses the original component&apos;s metadata. When you call a method, onMissingMethod() runs. It then loops through the metadata and checks that all required incoming arguments are present, that the types are correct (if you specified a type in the original argument), and that the return value that you have specified is of the correct type as well (if you specified an original return type). 

The possible combinations of untyped arguments, optional arguments, named vs. positional arguments, native types vs. CFC types, etc. made this quite difficult to implement. Basically, I had to write code that mimics all of CF&apos;s built in required argument and type checking! However, I believe I have it working for all of those situations. If anyone runs into a situation that isn&apos;t handled properly, please submit an issue at the RIAForge project.
				
				</description>
				
				
				<category>ColdFusion</category>
				
				<category>Unit Testing</category>
				
				<category>OOP CF</category>
				
				<category>ColdMock</category>
				
				<pubDate>Thu, 30 Aug 2007 11:10:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2007/8/30/ColdMock-Now-Available-at-RIAForge</guid>
				
				
			</item>
			
		 	
			
			
			<item>
				<title>Unit Testing with ColdMock To Dynamically Generate Mock Objects</title>
				<link>http://www.briankotek.com/blog/index.cfm/2007/8/28/Unit-Testing-with-ColdMock-To-Dynamically-Generate-Mock-Objects</link>
				<description>
				
				I was at the RailsEdge conference in Chicago last week to hear some people talk about Ruby on Rails. I may post a separate entry about my thoughts on that conference and Rails, but this post goes in a different direction. One of the interesting things I saw was leveraging the dynamic nature of Ruby to create mock objects on the fly for use in your tests. It was a pretty cool idea since writing and managing &quot;real&quot; mock objects is kind of a pain. So I sat down and created a mock generator in ColdFusion in about an hour. I think I will call it ColdMock (not very original, I know, but I&apos;m not a marketing guy).

If you are not familiar with Mock objects, here is the idea. When you test a CFC, you want to test ONLY that CFC. However, in a real application, CFCs often depend on other CFCs to work. It is very difficult to test these CFCs, because you&apos;re really testing the entire tree of dependent CFCs at once. To avoid this, you would substitute Mock objects for the dependent CFCs. As far as the CFC you are testing is concerned, the Mock objects act exactly the same as the real components. However, they have no actual code in them. They just return dummy data to the CFC you are testing. This way, you are only testing a single CFC, which is the entire point of a good unit test.

Up until now I have been writing (or, more often, using my CFC Stub Generator to generate) actual Mock CFC files. The problem is that you still have to go into the file and hard code return values for them. And if you need to call the method more than once during a test, you have to figure out some way to make the method return different values for the different calls. And the return values are then immutable, unless you go back in and re-edit the Mock CFC. It works, and it&apos;s definitely better than nothing, but ColdMock will make handling this much easier.

It works like this: you tell ColdMock which CFC you want to mock. It will create an instance of the original component, then purge all of its methods and dynamically attach a new set of methods that handle the mocking duties. This means that you no longer need to actually create a separate Mock CFC file. Once ColdMock gives you back your mock CFC, you declare what methods you want to mock and what values the mock method will return. Then you can test your CFC, and when it needs to call the CFCs it depends on, your Mock components will return whatever values you specified.

Since just reading the description of what it does might not make things clear, let&apos;s look at some code. I&apos;ll start with a very simple example. I want to mock out a SessionFacade component.

&lt;code&gt;
&lt;!--- Create mock factory. ---&gt;
&lt;cfset mockFactory = CreateObject(&apos;component&apos;,&apos;MockFactory&apos;).init() /&gt;
&lt;!--- Create mock object. ---&gt;
&lt;cfset facade = mockFactory.createMock(&apos;SessionFacade&apos;) /&gt;
&lt;!--- Define mock return value. ---&gt;
&lt;cfset facade.returns(32) /&gt;

&lt;cfoutput&gt;#facade.doSomething()#&lt;/cfoutput&gt;
&lt;/code&gt;

This creates the mock facade object, then says &quot;when I call a method, any method, on the facade, return 32&quot;. You can also define multiple return values like this:

&lt;code&gt;
&lt;cfset facade.returns(32,12,188) /&gt;
&lt;cfoutput&gt;#facade.doSomething()# #facade.doSomething()# #facade.doSomething()# &lt;/cfoutput&gt;
&lt;/code&gt;

Which will return a different value each time, based on the order you defined them. This works for fairly straightforward situations, but you might also need to define different values (or sets of values) for specific method calls. You can do this as well:

&lt;code&gt;
&lt;cfset facade.mockMethod(&apos;getLastUserLogin&apos;).returns(&apos;1/1/2007&apos;) /&gt;
&lt;cfset facade.mockMethod(&apos;getLoginCount&apos;).returns(42) /&gt;
&lt;cfoutput&gt;#facade.getLastUserLogin()# #facade.getLoginCount()#&lt;/cfoutput&gt;
&lt;/code&gt;

The return values can be anything. You can specify a string, a structure, or even a CFC instance, such as:

&lt;code&gt;
&lt;cfset user = mockFactory.createMock(&apos;User&apos;) /&gt;
&lt;cfset facade = mockFactory.createMock(&apos;SessionFacade&apos;) /&gt;
&lt;cfset facade.returns(user) /&gt;
&lt;cfdump var=&quot;#facade.doSomething()#&quot; label=&quot;Dump the returned User object&quot;&gt;
&lt;/code&gt;

This should get across how the mocks work. The real benefit comes when you use them in your unit tests. You can create the mocks yourself, or if you use something like ColdSpring, you can have ColdSpring resolve the dependencies by creating mocks and injecting them into the CFC being tested.

Let&apos;s say I have a Trip CFC that I want to test. The Trip CFC depends on a SessionFacade CFC to do its work. I can create a ColdSpring XML file that my unit tests can all use which will handle the dependencies. It seems like a lot of work but it really isn&apos;t, and you probably can use one ColdSpring file to handle dependency resolution for all of your unit test files. Here is my simple ColdSpring XML:

&lt;code&gt;
&lt;beans&gt;   
   
	&lt;bean id=&quot;trip&quot; class=&quot;tests.coldmock.Trip&quot;&gt;
		&lt;property name=&quot;sessionFacade&quot;&gt;
			&lt;ref bean=&quot;MockSessionFacade&quot;/&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
   
	&lt;bean id=&quot;mockSessionFacade&quot; factory-bean=&quot;MockFactory&quot; factory-method=&quot;createMock&quot;&gt;
		&lt;constructor-arg name=&quot;objectToMock&quot;&gt;
			&lt;value&gt;tests.coldmock.SessionFacade&lt;/value&gt;
		&lt;/constructor-arg&gt;
	&lt;/bean&gt;
   
	&lt;bean id=&quot;MockFactory&quot; class=&quot;tests.coldmock.MockFactory&quot; /&gt;
   
&lt;/beans&gt;
&lt;/code&gt;

Now, my CFCUnit test might look like this:

&lt;code&gt;
&lt;cfcomponent name=&quot;TestTrip&quot; extends=&quot;org.cfcunit.framework.TestCase&quot;&gt;
   
	&lt;cffunction name=&quot;setUp&quot; access=&quot;public&quot; output=&quot;false&quot; hint=&quot;Runs before each test method.&quot;&gt;
		&lt;cfset var local = StructNew() /&gt;
		&lt;cfset local.serviceDefinitionLocation = ExpandPath( &apos;/tests/coldmock/coldspring.xml&apos; ) /&gt;
		&lt;cfset local.beanFactory = CreateObject(&apos;component&apos;, &apos; coldspring.beans.DefaultXmlBeanFactory&apos;).init() /&gt;
		&lt;cfset local.beanFactory.loadBeansFromXmlFile(local.serviceDefinitionLocation) /&gt;
		&lt;cfset variables.trip = local.beanFactory.getBean(&apos;trip&apos;) /&gt;
		&lt;cfset variables.facade = local.beanFactory.getBean(&apos;mockSessionFacade&apos;) /&gt;
	&lt;/cffunction&gt;
   
	&lt;cffunction name=&quot;test_getDriverName&quot; returntype=&quot;void&quot; access=&quot;public&quot; output=&quot;false&quot; hint=&quot;&quot;&gt;
		&lt;cfset var local = StructNew() /&gt;
		&lt;cfset variables.facade.returns(&apos;Brian&apos;) /&gt;
		&lt;cfset assertEqualsString(&apos;Brian&apos;, variables.trip.getDriverName(), &apos;Driver name does not match.&apos;) /&gt;
	&lt;/cffunction&gt;
   
&lt;/cfcomponent&gt;
&lt;/code&gt;

So now I can test my Trip CFC in total isolation. Even though it depends on the SessionFacade in my real application, I can substitute it with a mock to run my tests. I can easily define what the method calls to the mock objects will return right in my test. So I don&apos;t need to create or edit any Mock CFC files. This makes using mocks and writing good tests much easier.

Unfortunately, because it uses onMissingMethod(), it will only run on ColdFusion 8. This might not be a huge issue because most people will probably be running the developer edition of ColdFusion 8 locally. However, I&apos;ll keep trying to see if I can get it to work in a way that doesn&apos;t require onMissingMethod.

I&apos;m still working on this but I should have something ready for release shortly. I&apos;ll be putting it up at RIAForge. In the meantime, I&apos;m very interested to hear what people think. Any ideas, comments, or criticism of the idea?
				
				</description>
				
				
				<category>Development</category>
				
				<category>ColdFusion</category>
				
				<category>Unit Testing</category>
				
				<category>OOP CF</category>
				
				<category>ColdMock</category>
				
				<pubDate>Tue, 28 Aug 2007 12:45:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2007/8/28/Unit-Testing-with-ColdMock-To-Dynamically-Generate-Mock-Objects</guid>
				
				
			</item>
			
		 	
			</channel></rss>