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
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!
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.
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.
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