Paul Butcher's Blog, page 2
June 24, 2012
ScalaMock 3.0-M1
ScalaMock 3.0-M1 (scaladoc) for Scala 2.10.0-M4 is now released. It supports:
Mock functions, traits and classes
Both expectation-first and record-then-verify (Mockito-style) mocking
ScalaTest and Specs2
To use with sbt:
libraryDependencies =
"org.scalamock" %% "scalamock-scalatest-support" % "3.0-M1"
For background information, see ScalaMock 3.0 Preview Release.
It’s been tested against overloaded, curried and polymorphic methods, by-name parameters, path-dependent types and type projections. There may still be corner cases that aren’t handled correctly, please report a bug if you find one.
June 4, 2012
ScalaMock 3.0 Preview Release
Update: ScalaMock 3.0-M4 for Scala 2.10.0-RC1 is now released.
The current version of ScalaMock makes use of a compiler plugin to generate typesafe mock objects. This works, but has a number of problems, not least of which is the complicated nature of the build system required (meaning that it currently only works with sbt). The recent preview release of Scala 2.10 has opened up a new approach via its support for macros.
I’ve just released a preview of ScalaMock 3.0, a “from the ground up” rewrite using macros. It’s not yet as capable as ScalaMock 2.x (it can only mock traits – there’s no support for mocking classes, singleton/companion objects or object creation yet) but it’s certainly complete enough to be useful, I hope.
One significant (and frequently requested!) enhancement over ScalaMock 2.x is support for Mockito-style “record then verify” mocking as well as the more traditional “setup expectations” style.
The source is available on GitHub, and a snapshot release (Scala 2.10.0-M3 only) is available on Sonatype:
libraryDependencies =
"org.scalamock" %% "scalamock-scalatest-support" % "3.0-SNAPSHOT"
You can see examples of it in use in GitHub.
Here’s an example of a test written using the “setup expectations” style:
class ControllerTest extends FunSuite with MockFactory {
test("draw line") {
val mockTurtle = mock[Turtle]
val controller = new Controller(mockTurtle)
inSequence {
inAnyOrder {
(mockTurtle.penUp _) expects ()
(mockTurtle.getPosition _) expects () returning (0.0, 0.0)
(mockTurtle.getAngle _) expects () returning 0.0
}
(mockTurtle.turn _) expects ~(Pi / 4)
(mockTurtle.forward _) expects ~sqrt(2.0)
(mockTurtle.getAngle _) expects () returning Pi / 4
(mockTurtle.turn _) expects ~(-Pi / 4)
(mockTurtle.penDown _) expects ()
(mockTurtle.forward _) expects 1.0
}
controller.drawLine((1.0, 1.0), (2.0, 1.0))
}
}
and here’s the same example rewritten to use the Mockito-style record and then verify approach:
class ControllerTest extends FunSuite with MockFactory {
import scala.language.postfixOps
test("draw line") {
val mockTurtle = stub[Turtle]
val controller = new Controller(mockTurtle)
inSequence {
inAnyOrder {
(mockTurtle.getPosition _) when () returns (0.0, 0.0)
(mockTurtle.getAngle _) when () returns 0.0 once
}
(mockTurtle.getAngle _) when () returns Pi / 4
}
controller.drawLine((1.0, 1.0), (2.0, 1.0))
inSequence {
(mockTurtle.turn _) verify ~(Pi / 4)
(mockTurtle.forward _) verify ~sqrt(2.0)
(mockTurtle.turn _) verify ~(-Pi / 4)
(mockTurtle.penDown _) verify ()
(mockTurtle.forward _) verify 1.0
}
}
}
Right now, it supports:
Mock functions (including higher-order functions)
Polymorphic traits
Polymorphic methods
Curried methods
Overloaded methods
Here’s my todo list:
Sort out the documentation
Take advantage of macro types (type providers) when available in a subsequent Scala release. This will enable a slightly nicer syntax for method mocking:
mockObject.expects.method(arguments)
instead of:
(mockObject.method _) expects (arguments)
Mocking classes (as well as traits)
Mocking singleton/companion objects
Mocking object creation
I’d be very grateful for any feedback, in particular bug reports.
May 26, 2012
An open letter to an IT recruitment consultant
Dear IT Recruitment Consultant,
For the purposes of this letter, I’m going to assume that you are one of the tiny minority of members of your profession who have some integrity and ability. Unfortunately for you, you have chosen a career with a deservedly dreadful reputation. Most IT recruitment consultants are no better than a combination of pimp and double glazing salesman.
But you’re not one of this majority. You can have a conversation with me where I don’t need to explain everything to you in words of one syllable. Repeatedly. And you have a unique source of amazing candidates unavailable to any other recruitment consultant and who would be just perfect for me.
So how do you get to talk to me?
Well this is where it gets difficult I’m afraid. You know that you’re one of this tiny minority, but I don’t. And unfortunately all of you claim that you’re different from all the rest. How do I tell the difference between someone who really does have a brain and the vast majority who just claim to?
Well, I’m sorry, but there is exactly one way to achieve this, and that’s to be good at your job. It’s a long, slow path, but eventually someone I trust will recommend that I talk to you. And if I need to, I will.
What you absolutely do not do is call me. Or e-mail me. Or try to link to me on LinkedIn (especially not by lying and claiming to be my friend or an ex-colleague in order to avoid paying LinkedIn). Or try to get in contact in any other way whatsoever.
My problem is not that I don’t know enough recruitment consultants. Nobody who has spent more than a few years in IT can possibly fail to know many (many!) recruitment consultants. I have found a small set that I trust, respect, and will work with again (you see – I do know that some of you are different).
I don’t want to be introduced to any more. There are too damn many of you, and most of you aren’t worth even the 60 seconds of my time it takes to tell you to go away. Not that it ever takes 60 seconds, because you always have to followup and tell me why I’m wrong about you and you’re different from all the rest. And ignoring you doesn’t work either, because you’ll just try again. And again. And again.
I will never do business with any recruitment consultant who cold calls. Ever.
So please stop? Please?


