Aliaspooryorik
ColdFusion ORM Book

My ColdBox / MXUnit setup

On the ColdBox mailing list the other day, a new user was having trouble getting the the in-built support for integration testing up and running. I emailed off list my typical setup (which worked for him), and thought I'd post here as well in case it inspires anyone else to get into Unit Testing.

First off, you will need the excellent MXUnit in your webroot and of course ColdBox (this setup is for ColdBox 3, but I think apart from a small path change it's the same for 2.6).

Now that we have pre-application mappings in ColdFusion, I develop my applications so that they all run off a sub-folder of the weboot. For example; http://localhost/mycoldboxapp

If your ColdBox application doesn't have a test folder then you need to create one in the application root (at the same level as the handler/model/views folders). In the test folder, create a RemoteFacade.cfc which looks like:

component extends="mxunit.Framework.RemoteFacade"
{
}

Then I create an Application.cfc in the test folder. Something like this:

component 
{
    this.name = Hash( GetCurrentTemplatePath() );
    this.sessionManagement = true;
    this.sessionTimeout = createTimeSpan( 0, 0, 5, 0 );
    this.setClientCookies = true;

    // mappings for testing    
    this.mappings["/approot"] = ExpandPath( "../" );
}

Note that it does not extend or reference ColdBox.

The next step is to create a BaseTestCase.cfc in your test folder (this makes life easier as you add more tests). Mine typcially looks like this:

component extends="coldbox.system.testing.BaseTestCase"
{
  // tell ColdBox where to find the application
  instance.appMapping = ReReplaceNoCase( CGI.SCRIPT_NAME, "/test/(.)+$", "" );
  
  /**
  * helper method to reference ColdBox controller
  */
  private any function getColdBoxController()
  {
    return instance.controller;
  }
  
  /**
  * helper method which delegates to coldbox controller getPlugin 
  */
  private any function getPlugin( required string pluginname )
  {
    return instance.controller.getPlugin( arguments.pluginname );
  }
}

You don't need the 'helper' methods, they are just there for added convenience.

Finally, you need some tests (I suggest you put these in sub-folders of the test folder). Here's a version of the ehHandlerTest.cfc which ColdBox generates, which I've put in a handlers sub-directory of test. Note that the cfc name must end with test.

component extends="approot.test.BaseTestCase"
{
    
  void function setUp()
  {
    // Call the super setup method to setup the app.
    super.setup();
        
    // Any preparation work will go here for this test.
  }

  void function testindex()
  {
    var event = "";
        
    //Place any variables on the form or URL scope to test the handler.
    //URL.name = "luis"
    event = execute("general.index");

    debug(event.getCollection());

    //Do your asserts below
    assertEquals("Welcome to ColdBox!", event.getValue("welcomeMessage",""), "Failed to assert welcome message");
  }

}

That's it, you'll now be able to use the MXUnit Eclipse plugin (very cool) by setting the remote facade URL to http://localhost/mycoldboxapp/test/RemoteFacade.cfc?wsdl. Alternatively, you can view it in a browser using the URL: http://localhost/mycoldboxapp/test/handlers/ehHandlerTest.cfc?method=runtestremote

This may not be the perfect way to do it, but it works well for me!


No comments

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts