package mock

  1. Alphabetic
  1. Public
  2. Protected

Type Members

  1. trait Mockito extends IdiomaticMockito with ArgumentMatchersSugar

    Helper for Mockito Scala sugar with idiomatic stubbing.

    Helper for Mockito Scala sugar with idiomatic stubbing. Java users are encouraged to use org.mockito.Mockito directly.

    Note that the Specs2 smartMock[] or mock[].smart is the default behavior for Mockito Scala.


    This trait uses org.mockito.IdiomaticMockito which is heavily influenced by ScalaTest Matchers.

    To use, mix in the com.twitter.util.mock.Mockito trait where desired.

    Create a new mock

    trait Foo {
      def bar: String
      def bar(v: Int): Int
     class MyTest extends AnyFunSuite with Mockito {
        val aMock = mock[Foo]

    Expect behavior

    // "when" equivalents returns "mocked!" returns "mocked!" andThen "mocked again!" shouldCall realMethod[IllegalArgumentException] throws new IllegalArgumentException answers "mocked!"*) answers ((i: Int) => i * 10)
    // "do-when" equivalents
    "mocked!" willBe returned by
    "mocked!" willBe answered by
    ((i: Int) => i * 10) willBe answered by*)
    theRealMethod willBe called by
    new IllegalArgumentException willBe thrown by // doNothing().when(aMock).bar
    // verifications
    aMock wasNever called          // verifyZeroInteractions(aMock) was called*) was called        // '*' is shorthand for 'any()' or 'any[T]'[Int]) was called // same as above but with typed input matcher wasCalled onlyHere wasNever called wasCalled twice wasCalled 2.times wasCalled fourTimes wasCalled 4.times wasCalled atLeastFiveTimes wasCalled atLeast(fiveTimes) wasCalled atLeast(5.times) wasCalled atMostSixTimes wasCalled atMost(sixTimes) wasCalled atMost(6.times) wasCalled (atLeastSixTimes within 2.seconds) // verify(aMock, timeout(2000).atLeast(6)).bar
    aMock wasNever calledAgain // verifyNoMoreInteractions(aMock)
    InOrder(mock1, mock2) { implicit order =>
      mock2.someMethod() was called
      mock1.anotherMethod() was called

    Note the 'dead code' warning that can happen when using 'any' or '*' matchers.

    Mixing and matching matchers

    Using the idiomatic syntax also allows for mixing argument matchers with real values. E.g., you are no longer forced to use argument matchers for all parameters as soon as you use one. E.g.,

    trait Foo {
      def bar(v: Int, v2: Int, v3: Int = 42): Int
    class MyTest extends AnyFunSuite with Mockito {
      val aMock = mock[Foo]
 , 2) returns "mocked!", *) returns "mocked!", any[Int]) returns "mocked!"*, *) returns "mocked!"[Int], any[Int]) returns "mocked!"*, *, 3) returns "mocked!"[Int], any[Int], 3) returns "mocked!"
      "mocked!" willBe returned by, 2)
      "mocked!" willBe returned by, *)
      "mocked!" willBe returned by, any[Int])
      "mocked!" willBe returned by*, *)
      "mocked!" willBe returned by[Int], any[Int])
      "mocked!" willBe returned by*, *, 3)
      "mocked!" willBe returned by[Int], any[Int], 3)
 , 2) was called, *) was called, any[Int]) was called*, *) was called[Int], any[Int]) was called*, *, 3) was called[Int], any[Int], 3) was called

    See Mix-and-Match for more information including a caveat around curried functions with default arguments.

    Numeric Matchers

    Numeric comparisons are possible for argument matching, e.g.,

    aMock.method(n > 4.99) was called
    aMock.method(n >= 5) was called
    aMock.method(n < 5.1) was called
    aMock.method(n <= 5) was called

    See Numeric Matchers.


    Most matches will deal with varargs out of the box, just note when using the 'eqTo' matcher to apply it to all the arguments as one (not individually).

    See Varargs.

    More Information

    See the IdiomaticMockito documentation for more specific information and the Mockito Scala Getting Started documentation for general information.

    see org.mockito.IdiomaticMockito see org.mockito.ArgumentMatchersSugar

Value Members

  1. object Mockito extends Mockito

    Simple object to allow the usage of the trait without mixing it in