I took Edward and implemented it like this:
I used the JavaLoader library to create my mockito object. variables.cfEvernote = "";
variables.classLoader = createObject("component", "resources.JavaLoader"). init(["#expandPath('../lib/mockito-all-1.8.5.jar')#", "#expandPath('../lib/CFEvernote.jar')#", "#expandPath('../lib/libthrift.jar')#", "#expandPath('../lib/evernote-api-1.18.jar')#"]); variables.mockito = variables.classLoader.create("org.mockito.Mockito").init();
Then, in the installation method of my munit test, I create my new mock java object:
<cffunction name="setUp" access="public" output="false" returntype="void"> <cfscript> variables.cfEvernote = createObject("component","com.714studios.cfevernote.CFEvernote"). Init(variables.configArray[1],variables.configArray[2], "sandbox.evernote.com", "http://localhost/cfevernote/callback.cfm" "#ExpandPath('../lib')#"); variables.mockCFEvernote = variables.mockito.mock(variables.classLoader.create("com.sudios714.cfevernote.CFEvernote"). Init("123","S1","232","sandbox.evernote.com","mock").getClass()); variables.cfEvernote.setCFEvernote(mockCFEvernote); </cfscript>
Then in my tests I create my mock behavior like this.
<cffunction name="test..." returntype="void" access="public" output="false" > <cfscript> var notebooks = ""; var expected = 12; var i = 0; var retArray = createObject("Java","java.util.ArrayList"); var actual = ""; for(i = 1; i lte 12; i = i + 1){ retArray.Add(""); } variables.mockito.when(mockCFEvernote.listNotebooks(12)).thenReturn(retArray); notebooks = variables.cfEvernote.getNotebooks(12); actual = arrayLen(notebooks); assertEquals(expected,actual); </cfscript>
I also talked about this in more detail here - http://blog.bittersweetryan.com/2011/07/unit-testing-coldfusion-components-that.html .
source share