Fork me on GitHub

ScalaMock

Native Scala mocking
This page describes ScalaMock 2, which is no longer under development. The current development version is ScalaMock 3.

Mocking objects

def testTurtle {
  val t = mock[Turtle]

  t.expects.penDown()
  t.expects.turn(90.0)
  t.expects.forward(10.0)
  t.expects.getPosition returning (0.0, 10.0)
  
  drawLine(t)
}

Mocking functions

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

Full worked example.

Features

As well as traits (interfaces) and functions, ScalaMock can also mock:

Downloading

Download from Sonatype.

To use ScalaMock in sbt 0.11 with ScalaTest add the following to your project file:

resolvers += "Sonatype OSS Releases" at 
  "http://oss.sonatype.org/content/repositories/releases/"

libraryDependencies +=
  "org.scalamock" %% "scalamock-scalatest-support" % "latest.integration"

The source is hosted on GitHub.

Documentation

Scaladoc.

If you're migrating from Borachio, read this.