org

scalamock

package scalamock

ScalaMock: Native Scala Mocking

To use ScalaMock, mix the relevant MockFactory trait into your test class. For ScalaTest, use org.scalamock.scalatest.MockFactory and for Specs2 use org.scalamock.specs2.MockFactory.

At present, ScalaMock can only mock traits, Java interfaces, and non-final classes that define a default constructor. A future version will be able to mock any class, and singleton/companion objects.

ScalaMock supports two different mocking styles - expectations first and record then verify. These styles can be mixed within a single test.

Expectations-First Style

In the expectations-first style, expectations are set on mock objects before exercising the system under test. If these expectations are not met, the test fails.

A mock function that supports this style is created with mockFunction. For example, to create a mock function taking a single Int argument and returning a String:

val m = mockFunction[Int, String]

A mock object that supports this style is created with mock. For example, to create a mock that implements the Turtle trait:

val m = mock[Turtle]

Expectations can then be set using expects:

(m.setPosition _).expects(10.0, 10.0)
(m.forward _).expects(5.0)
(m.getPosition _).expects().returning(15.0, 10.0)

drawLine(m, (10.0, 10.0), (15.0, 10.0))

Record-then-Verify (Mockito) Style

In the record then verify style, expectations are verified after the system under test has executed.

A stub function that supports this style is created with stubFunction. For example:

val m = stubFunction[Int, String]

A stub object that supports this style is created with stub. For example:

val m = stub[Turtle]

Return values that are used by the system under test can be set up by using when. Calls are verified using verify:

(m.getPosition _).when().returns(15.0, 10.0)

drawLine(m, (10.0, 10.0), (15.0, 10.0))

(m.setPosition _).verify(10.0, 10.0)
(m.forward _).verify(5.0)

Argument matching

ScalaMock supports two types of generalised matching: wildcards and epsilon matching.

Wildcards

Wildcard values are specified with an * (asterisk). For example:

m expects ("this", *)

will match any of the following:

m("this", 42)
m("this", 1.0)
m("this", null)
Epsilon matching

Epsilon matching is useful when dealing with floating point values. An epsilon match is specified with the ~ (tilde) operator:

m expects (~42.0)

will match:

m(42.0)
m(42.0001)
m(41.9999)

but will not match:

m(43.0)
m(42.1)
Repeated parameters

Repeated parameters are represented as a Seq. For example, given:

def takesRepeatedParameter(x: Int, ys: String*)

you can set an expectation with:

(m.takesRepeatedParameter _).expects(42, Seq("red", "green", "blue"))
Predicate matching

More complicated argument matching can be implemented by using where to pass a predicate:

m = mockFunction[Double, Double, Unit]
m expects (where { _ < _ })
Return values

By default mocks and stubs return null. You can return a computed return value (or throw a computed exception) with onCall:

val mockIncrement = mockFunction[Int, Int]
m expects (*) onCall { _ + 1 }
Overloaded, curried and polymorphic methods

Overloaded, curried and polymorphic methods can be mocked by specifying either argument types or type parameters. For example:

trait Foo {
def overloaded(x: Int): String
def overloaded(x: String): String
def overloaded[T](x: T): String
def curried(x: Int)(y: Double): String
def polymorphic[T](x: List[T]): String
}
val m = mock[Foo]
(m.overloaded(_: Int)).expects(10)
(m.overloaded(_: String)).expects("foo")
(m.overloaded[Double] _).expects(1.23)
(m.curried(_: Int)(_: Double)).expects(10, 1.23)
(m.polymorphic(_: List[Int])).expects(List(1, 2, 3))
(m.polymorphic[String] _).expects("foo")
Exceptions

Instead of a return value, mocks and stubs can be instructed to throw:

m expects ("this", "that") throws new RuntimeException("what's that?")
Call count

By default, mocks and stubs expect exactly one call. Alternative constraints can be set with repeat:

m1.expects(42).returns(42).repeat(3 to 7)
m2 expects (3) repeat 10

There are various aliases for common expectations and styles:

m1.expects("this", "that").once
m2.expects().returns("foo").noMoreThanTwice
m3.expects(42).repeated(3).times

For a full list, see org.scalamock.CallHandler.

Ordering

By default, expectations can be satisfied in any order. For example:

m expects (1)
m expects (2)
m(2)
m(1)

A specific sequence can be enforced with inSequence:

inSequence {
  m expects (1)
  m expects (2)
}
m(2) // throws ExpectationException
m(1)

Multiple sequences can be specified. As long as the calls within each sequence happen in the correct order, calls within different sequences can be interleaved. For example:

inSequence {
  m expects (1)
  m expects (2)
}
inSequence {
  m expects (3)
  m expects (4)
}

m(3)
m(1)
m(2)
m(4)

To specify that there is no constraint on ordering, use inAnyOrder (there is an implicit inAnyOrder at the top level). Calls to inSequence and inAnyOrder can be arbitrarily nested. For example:

(m.a _).expects()
inSequence {
  (m.b _).expects()
  inAnyOrder {
    (m.c _).expects()
    inSequence {
      (m.d _).expects()
      (m.e _).expects()
    }
    (m.f _).expects()
  }
  (m.g _).expects()
}

Threads

ScalaMock will work with tests that are run in parallel (Specs2 runs tests in parallel by default, and ScalaTest does so with ParallelTestExecution).

You can call mocks from other threads within tests, but any such calls must be complete before the test completes - it's an error to call a mock afterwards.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. scalamock
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. class ArgumentMatcher extends (Product) ⇒ Boolean

  2. case class Call(target: FakeFunction, arguments: Product) extends Product with Serializable

  3. class CallHandler[R] extends Handler

  4. class CallHandler0[R] extends CallHandler[R]

  5. class CallHandler1[T1, R] extends CallHandler[R]

  6. class CallHandler2[T1, T2, R] extends CallHandler[R]

  7. class CallHandler3[T1, T2, T3, R] extends CallHandler[R]

  8. class CallHandler4[T1, T2, T3, T4, R] extends CallHandler[R]

  9. class CallHandler5[T1, T2, T3, T4, T5, R] extends CallHandler[R]

  10. class CallHandler6[T1, T2, T3, T4, T5, T6, R] extends CallHandler[R]

  11. class CallHandler7[T1, T2, T3, T4, T5, T6, T7, R] extends CallHandler[R]

  12. class CallHandler8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends CallHandler[R]

  13. class CallHandler9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends CallHandler[R]

  14. trait Defaultable[T] extends AnyRef

  15. class EpsilonMockParameter extends MockParameter[Double]

  16. abstract class FakeFunction extends AnyRef

  17. abstract class FakeFunction0[R] extends FakeFunction with () ⇒ R with NiceToString

  18. abstract class FakeFunction1[T1, R] extends FakeFunction with (T1) ⇒ R with NiceToString

  19. abstract class FakeFunction2[T1, T2, R] extends FakeFunction with (T1, T2) ⇒ R with NiceToString

  20. abstract class FakeFunction3[T1, T2, T3, R] extends FakeFunction with (T1, T2, T3) ⇒ R with NiceToString

  21. abstract class FakeFunction4[T1, T2, T3, T4, R] extends FakeFunction with (T1, T2, T3, T4) ⇒ R with NiceToString

  22. abstract class FakeFunction5[T1, T2, T3, T4, T5, R] extends FakeFunction with (T1, T2, T3, T4, T5) ⇒ R with NiceToString

  23. abstract class FakeFunction6[T1, T2, T3, T4, T5, T6, R] extends FakeFunction with (T1, T2, T3, T4, T5, T6) ⇒ R with NiceToString

  24. abstract class FakeFunction7[T1, T2, T3, T4, T5, T6, T7, R] extends FakeFunction with (T1, T2, T3, T4, T5, T6, T7) ⇒ R with NiceToString

  25. abstract class FakeFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends FakeFunction with (T1, T2, T3, T4, T5, T6, T7, T8) ⇒ R with NiceToString

  26. abstract class FakeFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends FakeFunction with (T1, T2, T3, T4, T5, T6, T7, T8, T9) ⇒ R with NiceToString

  27. class FunctionAdapter0[R] extends (Product) ⇒ R

  28. class FunctionAdapter1[T1, R] extends (Product) ⇒ R

  29. class FunctionAdapter2[T1, T2, R] extends (Product) ⇒ R

  30. class FunctionAdapter3[T1, T2, T3, R] extends (Product) ⇒ R

  31. class FunctionAdapter4[T1, T2, T3, T4, R] extends (Product) ⇒ R

  32. class FunctionAdapter5[T1, T2, T3, T4, T5, R] extends (Product) ⇒ R

  33. class FunctionAdapter6[T1, T2, T3, T4, T5, T6, R] extends (Product) ⇒ R

  34. class FunctionAdapter7[T1, T2, T3, T4, T5, T6, T7, R] extends (Product) ⇒ R

  35. class FunctionAdapter8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends (Product) ⇒ R

  36. class FunctionAdapter9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends (Product) ⇒ R

  37. trait LowPriorityDefaultable extends AnyRef

  38. class MatchAny extends Equals

  39. class MatchEpsilon extends Equals

  40. trait Mock extends AnyRef

  41. trait MockFactoryBase extends Mock

  42. trait MockFunction extends AnyRef

  43. class MockFunction0[R] extends FakeFunction0[R] with MockFunction

  44. class MockFunction1[T1, R] extends FakeFunction1[T1, R] with MockFunction

  45. class MockFunction2[T1, T2, R] extends FakeFunction2[T1, T2, R] with MockFunction

  46. class MockFunction3[T1, T2, T3, R] extends FakeFunction3[T1, T2, T3, R] with MockFunction

  47. class MockFunction4[T1, T2, T3, T4, R] extends FakeFunction4[T1, T2, T3, T4, R] with MockFunction

  48. class MockFunction5[T1, T2, T3, T4, T5, R] extends FakeFunction5[T1, T2, T3, T4, T5, R] with MockFunction

  49. class MockFunction6[T1, T2, T3, T4, T5, T6, R] extends FakeFunction6[T1, T2, T3, T4, T5, T6, R] with MockFunction

  50. class MockFunction7[T1, T2, T3, T4, T5, T6, T7, R] extends FakeFunction7[T1, T2, T3, T4, T5, T6, T7, R] with MockFunction

  51. class MockFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends FakeFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R] with MockFunction

  52. class MockFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends FakeFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] with MockFunction

  53. class MockParameter[T] extends AnyRef

  54. trait NiceToString extends AnyRef

  55. class StubFunction0[R] extends FakeFunction0[R]

  56. class StubFunction1[T1, R] extends FakeFunction1[T1, R]

  57. class StubFunction2[T1, T2, R] extends FakeFunction2[T1, T2, R]

  58. class StubFunction3[T1, T2, T3, R] extends FakeFunction3[T1, T2, T3, R]

  59. class StubFunction4[T1, T2, T3, T4, R] extends FakeFunction4[T1, T2, T3, T4, R]

  60. class StubFunction5[T1, T2, T3, T4, T5, R] extends FakeFunction5[T1, T2, T3, T4, T5, R]

  61. class StubFunction6[T1, T2, T3, T4, T5, T6, R] extends FakeFunction6[T1, T2, T3, T4, T5, T6, R]

  62. class StubFunction7[T1, T2, T3, T4, T5, T6, T7, R] extends FakeFunction7[T1, T2, T3, T4, T5, T6, T7, R]

  63. class StubFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends FakeFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R]

  64. class StubFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends FakeFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]

  65. trait Verify extends AnyRef

Value Members

  1. object CallHandler

  2. object Defaultable extends LowPriorityDefaultable

  3. object MatchEpsilon

  4. object MockImpl

  5. package proxy

Inherited from AnyRef

Inherited from Any

Ungrouped