ANN: Borachio - native scala mocking

23 views
Skip to first unread message

Paul Butcher

unread,
Feb 25, 2011, 2:23:50 PM2/25/11
to scala-user
Many thanks to everyone on this list who pitched in to answer my questions and make suggestions about the native Scala mocking framework I've been working on.

I now have something which, although far from "done", is close enough that I think it should be useful. So I'd like to announce and solicit feedback on Borachio - a native Scala mocking framework:

Homepage: http://borachio.com/
GitHub: https://github.com/paulbutcher/borachio
Documentation: http://borachio.com/api/com/borachio/package.html

A couple of examples:

> def testTurtle {
> val t = mock[Turtle]
>
> t expects 'penDown
> t expects 'turn withArgs (90.0)
> t expects 'forward withArgs (10.0)
> t expects 'getPosition returning (0.0, 10.0)
>
> drawLine(t)
> }

> def testFoldLeft() {
> val f = mockFunction[String, Int, String]
>
> f expects ("initial", 0) returning "intermediate one"
> f expects ("intermediate one", 1) returning "intermediate two"
> f expects ("intermediate two", 2) returning "intermediate three"
> f expects ("intermediate three", 3) returning "final"
>
> expect("final") { Seq(0, 1, 2, 3).foldLeft("initial")(f) }
> }

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Daniel Hinojosa

unread,
Feb 25, 2011, 2:35:19 PM2/25/11
to Paul Butcher, scala-user
Great! I want to try this ASAP.

Question: Do you have a solution mocking a Scala object?

Kevin Wright

unread,
Feb 25, 2011, 2:42:14 PM2/25/11
to Paul Butcher, scala-user


On 25 Feb 2011 19:23, "Paul Butcher" <pa...@paulbutcher.com> wrote:
>
> Many thanks to everyone on this list who pitched in to answer my questions and make suggestions about the native Scala mocking framework I've been working on.
>
> I now have something which, although far from "done", is close enough that I think it should be useful. So I'd like to announce and solicit feedback on Borachio - a native Scala mocking framework:
>
> Homepage: http://borachio.com/
> GitHub: https://github.com/paulbutcher/borachio
> Documentation: http://borachio.com/api/com/borachio/package.html
>
> A couple of examples:
>
> > def testTurtle {
> >   val t = mock[Turtle]
> >
> >   t expects 'penDown
> >   t expects 'turn withArgs (90.0)
> >   t expects 'forward withArgs (10.0)
> >   t expects 'getPosition returning (0.0, 10.0)
> >
> >   drawLine(t)
> > }
>
> > def testFoldLeft() {
> >   val f = mockFunction[String, Int, String]
> >
> >   f expects ("initial", 0) returning "intermediate one"
> >   f expects ("intermediate one", 1) returning "intermediate two"
> >   f expects ("intermediate two", 2) returning "intermediate three"
> >   f expects ("intermediate three", 3) returning "final"
> >
> >   expect("final") { Seq(0, 1, 2, 3).foldLeft("initial")(f) }
> > }
>

Oh yes! That's looking so sexy I'd let it have my babies. At least, I would if the two I already have weren't so darned expensive!

Paul Butcher

unread,
Feb 25, 2011, 4:34:49 PM2/25/11
to Daniel Hinojosa, scala-user
On 25 Feb 2011, at 19:35, Daniel Hinojosa wrote:
> Question: Do you have a solution mocking a Scala object?


Not really, I'm afraid. The problem is analogous to mocking a static method in Java, which can be done through code generation so I assume that a similar approach would work (although I've not yet tried).

My primary motivation for working on this is to come up with a workable mocking solution for Android, which is not supported by any of the existing Java mocking frameworks (because Android's Dalvik VM doesn't support cglib) so I've deliberately avoided using this kind of technique.

I would be very happy to welcome contributions from anyone who wanted to have a crack at adding this kind of functionality though.

Paul Butcher

unread,
Feb 25, 2011, 4:35:40 PM2/25/11
to Kevin Wright, scala-user
On 25 Feb 2011, at 19:42, Kevin Wright wrote:
> Oh yes! That's looking so sexy I'd let it have my babies. At least, I would if the two I already have weren't so darned expensive!

Many thanks for the kind words, Kevin. It's still rough around the edges and has a way to go, but I hope that it's a reasonable start.

Bill Venners

unread,
Feb 25, 2011, 7:24:27 PM2/25/11
to Paul Butcher, Daniel Hinojosa, scala-user
Hi Daniel,

You probably are already aware of this, but if you control the object
then a good way to make it mockable is to have it extend a trait or
class that people can then make the mock from.

Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com

Daniel Hinojosa

unread,
Feb 26, 2011, 1:13:08 AM2/26/11
to Bill Venners, Paul Butcher, scala-user
I did read that in that awesome book you wrote. :)  My only issue is that sometimes I am using 3rd party code where they don't extend from the class or trait and then happiness fades into the wind.

ssanj

unread,
Feb 26, 2011, 2:43:27 AM2/26/11
to scala-user
Looks really good mate! Looking forward to using it.
> MSN: p...@paulbutcher.com
> AIM: paulrabutcher
> Skype: paulrabutcher
Reply all
Reply to author
Forward
0 new messages