Announcing Mockatoo - Haxe mocking framework

276 views
Skip to first unread message

Dom De Lorenzo

unread,
Oct 24, 2012, 9:58:45 PM10/24/12
to haxe...@googlegroups.com

Hi List,

I'm excited to announce Mockatoo (http://lib.haxe.org/p/mockatoo), a Haxe library for mocks creation, verification and stubbing that is loosely based on the public API of Mockito (a java mocking framework)

It uses Haxe macros to generated mock implementations of classes and interfaces for testing.

It has been thoroughly tested on Haxe 2.10 across most platforms (AVM2, JavaScript, Neko, C++, etc), and is designed to integrate seamlessly with munit and mcover

Source, examples, and documentation can be found on github (http://github.com/misprintt/mockatoo)

Main Features

Mock any class or interface (or typedef alias), including types with generics

var mockedClass = Mockatoo.mock(SomeClass);
var mockedInterface = Mockatoo.mock(SomeInterface);
var mockedClassWithGenerics = Mockatoo.mock(SomeList, [String]);


Verify a method has been called with specific paramaters

Mockatoo.verify(mock).foo();
Mockatoo.verify(mock).someMethod("foo", "bar");


Define a stub response when a method is invoked

Mockatoo.when(mock.foo("bar")).thenReturn("hello");
Mockatoo.when(mock.someMethod("foo", "bar").thenThrow(new Exception(""));


Define Custom argument matchers and wildcards

Mockatoo.when(mock.foo(anyString)).thenReturn("hello");
Mockatoo.when(mock.foo(isNull)).thenReturn("world");


Verifying exact number of invocations

Mockatoo.verify(mock, times(2)).foo();
Mockatoo.verify(mock, atLeast(2)).foo();
Mockatoo.verify(mock, atLeastOnce).foo();
Mockatoo.verify(mock, never).foo();


More documentation here: https://github.com/misprintt/mockatoo

---

Merry mocking,

Dom

Jason O'Neil

unread,
Oct 24, 2012, 10:32:08 PM10/24/12
to haxe...@googlegroups.com
Very nice!

In honesty until earlier this year I was fairly naive to the whole concept of testing, and haven't had experience with most of the unit testing frameworks and mocking libraries from other languages - so while I knew of mocking, I'd never really bothered to learn it so far. 

Looking at your library though, and the excellent readme (thank you!) I'm looking forward to using this on future projects :)

Jason


Stephane Le Dorze

unread,
Oct 25, 2012, 4:31:32 AM10/25/12
to haxe...@googlegroups.com
This is really great! :)

I wonder, as we have the excellent Haxe extensions capabilities, if an additional 'specs2' like layer could be added to even reduce the boilerplate of writing those mocks,
for instance:

Mockatoo.when(mock.foo("bar")).thenReturn("hello");

to become:

mock.foo("bar").returns("hello");

I've used both Mockito and Specs2 and it really ease readability and write those faster.

Again, thanks *a lot*!

Best,
Stephane

Dom De Lorenzo

unread,
Nov 11, 2012, 5:42:45 PM11/11/12
to haxe...@googlegroups.com

I've just released Mockatoo 1.3.0  to Haxelib.

It provides a simplified, smarter, macro enhanced API when using Haxe’s ‘using’ mixin (and is still fully backwards compatible with existing API).

using mockatoo.Mockatoo;
...
var mock = SomeClass.mock();
var mock = SomeInterface.mock();
var spy = SomeClass.spy(); // partial mock that calls real methods unless stubbed

New macros have been added for simplified stubbing with ‘using’:

mock.someMethod().returns("foo");
mock
.someMethod("foo").throws("some error");
mock
.someMethod("bar").calls(function(args){return "bar";});
mock
.someMethod().callsRealMethod();
mock
.someMethod().stub(); // resets to default stub value (i.e. null)

You can allso stub properties and getter setters (since 1.2.0)

mock.someProperty.returns("some value");

Verifications now also support raw integer counts

mock.someMethod().verify(1); //converted to verify(times(1))

Verfifying and stubbing mock fields are now validated at compile time to prevent runtime exceptions caused by out of date field references.

mock.someMethodThatDoesNotExist().verify(); //causes compilation error

The syntax for wildcard Matchers has been updated to be compiler-safe when using ‘using’

mock.someMethod(Mockatoo.anyString()).returns("foo");

For more information refer to the updated documentation , developer guide and examples on github.

Cheers,

Dom

Michael Cann

unread,
Nov 12, 2012, 3:12:09 AM11/12/12
to haxe...@googlegroups.com
Wow, thats some nice stuff dom!


--
Reply all
Reply to author
Forward
0 new messages