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

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

Yes you can make any method return any value as well

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

I know I can override the methods manually. Mockbox wouldn’t be nearly as useful if I couldn’t. :slight_smile:

Given this class…

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
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

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.

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

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);
}

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.

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

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.

Nice!!

So in conclusion, I think it might be a good idea. Shall I wait for your pull request :slight_smile:

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox