<?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 - Unit Testing</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>Tue, 07 Sep 2010 10:45:34 -0700</pubDate>
			<lastBuildDate>Tue, 15 Jun 2010 09:27: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>Using the Swiz AutowiredTestCase</title>
				<link>http://www.briankotek.com/blog/index.cfm/2010/6/15/Using-the-Swiz-AutowiredTestCase</link>
				<description>
				
				One of the utility classes that is part of the Swiz library is AutowiredTestCase. As the name implies, you can use this class to help test your Swiz applications. 

In this case, code will show what&apos;s going on much better than doing a lot of typing, so here is a sample FlexUnit 4 test case. First, the class under test:

&lt;code&gt;js|
package com.briankotek.flex4swiztests.control
{
	import com.briankotek.flex4swiztests.event.MyEvent;
	import flash.events.IEventDispatcher;

	public class MyController
	{
		
		[Dispatcher]
		public var dispatcher : IEventDispatcher;
		
		public var didSomething : Boolean = false;
		
		[Mediate( event=&quot;MyEvent.CONTROLLER_ACTION_REQUESTED&quot; )]
		public function handleAction() : void
		{
			didSomething = true;
			actionComplete();
		}
		
		private function actionComplete() : void
		{
			dispatcher.dispatchEvent( new MyEvent( MyEvent.CONTROLLER_ACTION_COMPLETE ) );
		}
		
	}
}
&lt;/code&gt;

You can see this is a simple controller, which responds to the CONTROLLER_ACTION_REQUESTED event, updates a property, and then dispatches CONTROLLER_ACTION_COMPLETE. We want to test that the controller is responding to the correct event, property updating the property, and finally dispatching the completion event.

The event itself is a simple event class:

&lt;code&gt;js|
package com.briankotek.flex4swiztests.event
{
	import flash.events.Event;
	
	public class MyEvent extends Event
	{
		public static const CONTROLLER_ACTION_REQUESTED : String = &quot;controllerActionRequested&quot;;
		public static const CONTROLLER_ACTION_COMPLETE : String = &quot;controllerActionComplete&quot;;
		
		public function MyEvent(type:String)
		{
			super(type, true, false);
		}
	}
}
&lt;/code&gt;

Finally, the test case itself:

&lt;code&gt;js|
package com.briankotek.flex4swiztests.control
{
	import com.briankotek.flex4swiztests.control.MyController;
	import com.briankotek.flex4swiztests.event.MyEvent;
	import org.flexunit.Assert;
	import org.flexunit.async.Async;
	import org.swizframework.core.*;
	import org.swizframework.utils.test.AutowiredTestCase;

	public class MyControllerTestCase extends AutowiredTestCase
	{	
		private var myController : MyController;
		
		[Before]
		public function setUp():void
		{
			myController = new MyController();
			
			swizConfig = new SwizConfig();
			swizConfig.eventPackages = &quot;com.briankotek.flex4swiztests.event.*&quot;;
			
			beanProviders = [new BeanProvider( [myController] )];
		}
		
		[After]
		public function tearDown():void
		{
			myController = null;
		}
		
		[Test(async)]
		public function testControllerActionRequested() : void
		{
			Assert.assertTrue( &quot;Controller property is already true.&quot;, myController.didSomething == false );	
			Async.handleEvent( this, swiz.dispatcher, MyEvent.CONTROLLER_ACTION_COMPLETE, checkEvent ); 
			swiz.dispatcher.dispatchEvent( new MyEvent( MyEvent.CONTROLLER_ACTION_REQUESTED ) );
		}
		
		private function checkEvent( event : Event, passThroughData : Object ) : void
		{
			Assert.assertTrue( &quot;Controller property was not updated.&quot;, myController.didSomething == true );	
		}
		
	}
}
&lt;/code&gt;

I start off setting up an instance of Swiz that my test can use. AutowiredTestCase has a method marked with [Before] metadata, so that FlexUnit runs it before each test. I create an instance of the class under test (MyController), then set the event packages on the SwizConfig. Finally, I place the MyController instance into a BeanProvider, so that Swiz will process it as a bean. This way, any Swiz metadata in MyController is processed.

The test method dispatches a CONTROLLER_ACTION_REQUESTED event. If all goes well, the mediated event in the controller should run, update the property, and then the controller will dispatch the completion event. Running the test produces a passing result, so everything is working as expected!

Note that because Swiz actually processes your test case itself as a bean, you can use Swiz metadata in your test if you want or need to. So injecting a dispatcher, mediating an event, or testing custom metadata processors are all possible as well.
				
				</description>
				
				
				<category>Unit Testing</category>
				
				<category>Flex</category>
				
				<category>Swiz</category>
				
				<pubDate>Tue, 15 Jun 2010 09:27:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2010/6/15/Using-the-Swiz-AutowiredTestCase</guid>
				
				
			</item>
			
		 	
			
			
			<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>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>Get Model-Glue Training, With ColdSpring, Transfer and Reactor Too</title>
				<link>http://www.briankotek.com/blog/index.cfm/2007/11/7/Get-ModelGlue-Training-With-ColdSpring-Transfer-and-Reactor-Too</link>
				<description>
				
				Just a quick note that Doug recently posted about &lt;a href=&quot;http://www.alagad.com/go/training/enterprise-coldfusion/enterprise-coldfusion-with-model-glue&quot; target=&quot;_blank&quot;&gt;a sweet price decrease for his first Model-Glue training class&lt;/a&gt;. Setting up a training business is difficult so the price drop doesn&apos;t surprise me at all. You really need to build up a base of happy students who spread the word, and in the beginning that means you need appealing pricing.

Well, this is good news for anyone interested in building their ColdFusion skill set. Not only will you get a good dose of Model-Glue, from the basics on up, but there are course sections on CFCUnit,  ColdSpring, Transfer, and Reactor as well. Given the number of questions on various lists and blogs about MVC, dependency injection, object-relational mapping, and general object-oriented programming concepts, this seems like a great way to get your head around a lot of topics quickly and efficiently. Just because some of us long-time CF&apos;ers had to beat our heads against he wall trying to learn things doesn&apos;t mean you have to! (I could also tell you about walking to school in the snow uphill both ways...)

Anyway, the upshot is that anyone in the midst of trying to learn these things should definitely have a look at the class. $800 is just really darn cheap, and I wouldn&apos;t expect it to stay at that kind of price point for very long. Get in while the getting is good.
				
				</description>
				
				
				<category>Development</category>
				
				<category>ColdFusion</category>
				
				<category>Unit Testing</category>
				
				<category>OOP CF</category>
				
				<category>Model-Glue</category>
				
				<category>ColdSpring</category>
				
				<category>Reactor</category>
				
				<pubDate>Wed, 07 Nov 2007 10:49:00 -0700</pubDate>
				<guid>http://www.briankotek.com/blog/index.cfm/2007/11/7/Get-ModelGlue-Training-With-ColdSpring-Transfer-and-Reactor-Too</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>