ColdFusion Unit Testing With MXUnit and IntelliJ IDEA

Written by

This post will show you how to get quickly up and running Unit Testing with ColdFusion and IntelliJ IDEA. As an example I will show you how to create a simple CFC with its corresponding unit test using MXUnit and then run the test locally in your browser. This mirrors the project example in the similar post I made earlier on in the week: ColdFusion Unit Testing with MXUnit and TextMate.

1) Install IntelliJ IDEA:

jetbrains.com/idea/download/

2) Download and follow setup instructions for MXUnit (mxunit.org).

3) Create a new project in IntelliJ: File > New Project.

4) Keep the default Create project from scratch option and click next.

5) Name your project example_project and ensure your project files location is in your ColdFusion root e.g. /Users/newtriks/Sites/example_project

6) Ensure create module is selected and the module to use is the default Java Module and click next.

7) Select do not create a src directory and then click next and then finish.

8) Create an Application.cfc in the root of example_project by right clicking on the example_project directory > New > ColdFusion Tag Component and enter the following:

1
2
3
4
5
<cfcomponent output="false">
  <!--- set up per application mappings --->
  <cfset this.mappings = {}>
  <cfset this.mappings["/com"] = GetDirectoryFromPath( GetCurrentTemplatePath() ) & "com">
</cfcomponent>

9) Create a new directory in example_project and name it com.

10) Within the newly created com directory create a new file named App.cfc and enter the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<cfcomponent>
    <cffunction name="addRapper" 
              access="remote"
              returntype="any">
      <cfargument name="rapperName" 
                  type="string" 
                  required="true" 
                  hint="Name of a rapper">
      <cfscript>
          var dummyResult=StructNew();
          dummyResult.name=rapperName;
            return dummyResult;
      </cfscript>
  </cffunction>
</cfcomponent>

11) Create the following package structure:

example_project > test

12) Within the test directory create AppTest.cfc and enter the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<cfcomponent extends="mxunit.framework.TestCase">
  <cffunction name="setUp" returntype="void" access="public" hint="Setup prior to running tests">
      <cfset obj = createObject("component","com.App")>
  </cffunction>
  <cffunction name="tearDown" returntype="void" access="public" hint="Tear down post running tests">
      <!--- cleanup --->
  </cffunction>
  <cffunction name="testAddRapper" returntype="void" access="public">
      <cfscript>
          var rapperName="Kool Keith";
          var result = obj.addRapper(rapperName);
          debug(result);
          assertEquals(rapperName,result.NAME);
      </cfscript>
  </cffunction>
</cfcomponent>

13) Select: Run > Edit Configurations and add a new configuration by clicking the plus icon in the top left corner.

14) Select ColdFusion in the list of available run configurations and name it test-example.

15) In the web path input field enter the below url (amend your localhost param accordingly) and click OK:

1
http://localhost/example_project/test/AppTest.cfc?method=runTestRemote

16) Select Run > Run or click the green triangle in the toolbar. You should see the below image: MXunit Test

Comments