Blog

Luis Majano

June 12, 2009

Spread the word


Share your thoughts

One of the nice things about ColdBox's model integration features are the ability to inject objects by using annotations via the cfproperty tag.  This gives a cleaner API to objects that don't really need to expose setters just for dependency injection purposes.  Why? well, in MY opinion, objects should be shy and only expose what they need to expose to the outside world.  By leveraging annotations, I can cleanly define the dependencies of my object and what is even better, it can even be self-documenting because they are cfproperties.  Not only that, since ColdFusion is a dynamic language and NOT JAVA, we can leverage much more than traditional setter/constructor dependency injection. In our next release, 3.0.0, we will expose our model integration features as a new dependency injection (IoC) framework which we are tentatively naming BlenderBox.  More about BlenderBox in other posts. So now that we introduced that ColdBox can blend your object's with other objects via annotations, how do we test them?  We have no setters or constructor arguments to inject them.  Well, but we do have mxunit and MockBox, which make our lives simpler and HAPPY!! So let's look at a simple service I am currently developing: ALL OTHER METHODS HERE /* Setup Configurations */ var config = instance.configBean; setProgramID(config.getKey('programID')); setDefaultPublicRole(config.getKey('DefaultPublicRole')); /* Get the program record */ instance.programRecord = instance.betadao.getProgramRecord(getProgramID()); As you can see, I have a method called onDIComplete() which is called by BlenderBox after all dependency injection is finalized. Basically, my component has been created, injected and now it will be configured. I get some settings from my configuration bean, call my dao for a program record and finalize. Simple stuff, so let's test this puppy. First of all, I will create my setup method with my target cfc and dependencies. function setup(){ /* Create Mocks */ mockDAO = getMockBox().createMock(className="beta2007.beta94.model.betaweb.BetaDAO",clearMethods=true,callLogging=true); mockConfig = getMockBox().createMock(className="coldbox.system.beans.configBean",clearMethods=true); /* Mock Individual Settings */ mockConfig.$("getKey").$args("programID").$results("pid123"); mockConfig.$("getKey").$args("DefaultPublicRole").$results("BetaUser"); /* Create Target Service,decorate it with Mocking Capabilities, and init it All in one single line via MockBox */ beta = getMockBox().createMock(className="beta2007.beta94.model.betaweb.BetaTransferService",callLogging=true).init(); /* Inject Mocks */ beta.$property("betaDao","instance",mockDao); beta.$property("configBean","instance",mockConfig); } What I do first is create my mock dao and mock config object. I then proceed to mock the 'getKey' method in my config object with several key target arguments and what they should return. Then I create my target object to test wich I also want decorated with mocking capabilities (just in case I need method spies) and init it. I then proceed to inject it with the mock objects into their appropriate variables in the correct scope /* Inject Mocks */ beta.$property("betaDao","instance",mockDao); beta.$property("configBean","instance",mockConfig); So by leveraging MockBox's $property() method, I can inject properties into my target object. This is how I inject my dependencies. So let's proceed now to see my onDiComplete test. function testonDiComplete(){ /* Fake Program Record via ColdBox querySim() */ pRecord = querySim("program_id,name,release_phase_id,active,published #createUUID()#|Beta Program|#createUUID()#|1|1"); /* Mock Dao Call */ mockDAO.$("getProgramRecord",pRecord); /* Run onDI Complete */ beta.onDIComplete(); /* My Asserts */ assertEquals(precord, beta.getProgramRecord(),"program record"); assertEquals(beta.getProgramID(),'pid123'); assertEquals(beta.getDefaultPublicRole(),'BetaUser'); } So in my DI complete test, I first mock a query by using the ColdBox's querysim() method, I then mock the call to my dao's getProgramRecord, with this query I just mocked. I then call the onDiComplete() for execution and finalize with some assertions to make sure everything ran smoothly. So there you go, with a few lines of MockBox and testing, you can now be on your way to smoother and pain free testing. So go forth and Mock the Box!!

Add Your Comment

Recent Entries

BoxLang 1.0.0 Beta 7 Launched

BoxLang 1.0.0 Beta 7 Launched

We are pleased to announce the release of BoxLang 1.0.0-Beta 7! This latest beta version includes improvements and essential bug fixes, but more importantly it certifies the execution of ColdBox HMVC and TestBox.

What is BoxLang?

BoxLang is a modern dynamic JVM language that can be deployed on multiple runtimes: operating system (Windows/Mac/*nix/Embedded), web server, lambda, iOS, android, web assembly, and more. BoxLang combines many features from different progr

Luis Majano
Luis Majano
July 26, 2024
New BoxLang Feature: Java Method References and Higher-Order Functions

New BoxLang Feature: Java Method References and Higher-Order Functions

We’ve added more goodies to our BoxLang Java interop: method references and higher-order functions. CFML has never let you do these things, making Java Interop feel like a second-class citizen. But with BoxLang, we’re elevating Java integration to a new level.

Maria Jose Herrera
Maria Jose Herrera
July 26, 2024
Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Are you a CFML developer looking to take your skills to the next level? Look no further than the ColdBox from Zero to Hero Virtual Live Training! This intensive two-day course will equip you with the knowledge and expertise to build robust and scalable applications using ColdBox 7, the latest version of the most popular CFML MVC framework.

What You'll Learn:

  • Master the Fun...

Cristobal Escobar
Cristobal Escobar
July 24, 2024