[Mockbox 3.2] Creating mock with methods overridden to return consistent responses by return type

26 views
Skip to first unread message

Jason Durham

unread,
Apr 16, 2015, 1:59:11 PM4/16/15
to col...@googlegroups.com
I was demoing MockBox today for someone who does not have a background in CF.  He was pretty delighted to see that it's following conventions of other testing frameworks in other languages.   

There was a feature in Mockito that caused methods in mocked classes to be replaced with methods with the same signature, but return predefined static values.

Does this capability exist in MockBox? 

Jason Durham

Luis Majano

unread,
Apr 16, 2015, 2:32:02 PM4/16/15
to col...@googlegroups.com, col...@googlegroups.com
Yes you can make any method return any value as well


--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/coldbox/CAC6EvRrMncfSj8nZOvfwRNe3o3DYu1CPt_b6HBdsv1pzyOH%2B1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jason Durham

unread,
Apr 16, 2015, 3:24:56 PM4/16/15
to col...@googlegroups.com
I know I can override the methods manually.  Mockbox wouldn't be nearly as useful if I couldn't. :)

Given this class..
=======================
component {

   array function getSomething() { return getArray() };
  
   array function getArray() { return [1,2,3] };

}


And this test...
=======================
it("the default return was overridden", function(){
  
  myMock = createMock('some.path');

  expect(myMock.getSomething()).toBe( [1,2,3] );

})

I ran that test and it passed.  Mockbox didn't inherently override getArray() like I expected.  In Mockito, it _would_ have.  By default, all methods are replaced with mocked methods that return specific data based upon the return type. 


This is the manual way I've been doing in the past
======================================
it("the default return was overridden", function(){
  
  myMock = createMock('some.path').$('getArray',[]);

  expect(myMock.getSomething()).toBe( [1,2,3] );

})

Does that better explain it? Perhaps I should be using something other than createMock()?

Jason Durham

Luis Majano

unread,
Apr 17, 2015, 9:36:52 AM4/17/15
to col...@googlegroups.com, col...@googlegroups.com
Ok. So you are saying that by default it replaces all methods with mocked methods. Ok. But what do they return? I can replace all methods but some might be strings, arrays, any? What would it retune by default?

You can clear all methods immediately if you like using the clear methods arg. However I fail to see what would mockito return if it just has the signature. 

Luis Majano

unread,
Apr 17, 2015, 9:38:20 AM4/17/15
to col...@googlegroups.com, col...@googlegroups.com
Even further. What good would the returned mocked default data be? The point of mocking is to have control over the results. So I would still have to mock accordingly. In my opinion I see no point of a default value for mocked methods. 

Jason Durham

unread,
Apr 20, 2015, 3:54:45 PM4/20/15
to col...@googlegroups.com
The point is that the returned data may not matter, which is why returning any dummy data by default would save some time. With CF's loose typing, perhaps this provides little value.

For example, lets say you were writing a test for getUsers().  Let's also assume that mockUserDAO was created in such a way, that the internal methods were replaced automatically like Mockito. The same applies to getUser (returns an empty User object).  In this case, listUsers() returns a query and for the sake of discussion, the default response for methods which return queries is some random query with 5 rows.  Your test would merely need to ensure that the array returned from listUsers() is 5 in length.

public User[] function getUsers() {
  var users = [];
  var qUsers = mockUserDAO.listUsers();

  for( var row in qUsers ) {
      users.append(getUser(row));
  }

  return users;

public User function getUser(args){
   return new User(args);
}


==========================

describe( "User service", function(){

   it("retrieves a user object for every user in the database", function() {
       expect(mockUserService.listUsers()).toHaveLength(5);
   };

});


In this rather basic example, it saved me from having to mock listUsers() in the DAO and getUser() in the service.

In the end, I was just curious if this behavior existed in Mockbox. :)



Jason Durham

Luis Majano

unread,
Apr 27, 2015, 12:16:03 PM4/27/15
to col...@googlegroups.com, col...@googlegroups.com
Hi Jason,

At this point there is no such functionality.  Doesn’t mean it can’t be added, but I am yet to see value in returning any type of data.  Plus, in our dynamic world, what should “any” return unless it is strong-typed.

Jason Durham

unread,
Apr 27, 2015, 3:17:42 PM4/27/15
to col...@googlegroups.com
Right, that's why I said "With CF's loose typing, perhaps this provides little value.".   

BTW, I just did another short demo of CommandBox/TestBox/MockBox.  It's pretty awesome that we can spin up a test environment on demand!  They are running CF8 for the time being, which TestBox doesn't support directly.  It was pretty awesome to be able to spin up Railo it to actually test the CF8 code.  We're still in the early stages of seeing how well our CF8 code plays with Railo 4.2, but eventually CF8 will be replaced for us anyway.

Jason Durham

Luis Majano

unread,
Apr 27, 2015, 4:36:30 PM4/27/15
to col...@googlegroups.com, col...@googlegroups.com
Nice!!

So in conclusion, I think it might be a good idea.  Shall I wait for your pull request :)
Reply all
Reply to author
Forward
0 new messages