Packages

trait Mockito extends IdiomaticMockito with ArgumentMatchersSugar

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.

Usage

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
aMock.bar returns "mocked!"
aMock.bar returns "mocked!" andThen "mocked again!"
aMock.bar shouldCall realMethod
aMock.bar.shouldThrow[IllegalArgumentException]
aMock.bar throws new IllegalArgumentException
aMock.bar answers "mocked!"
aMock.bar(*) answers ((i: Int) => i * 10)

// "do-when" equivalents
"mocked!" willBe returned by aMock.bar
"mocked!" willBe answered by aMock.bar
((i: Int) => i * 10) willBe answered by aMock.bar(*)
theRealMethod willBe called by aMock.bar
new IllegalArgumentException willBe thrown by aMock.bar
aMock.bar.doesNothing() // doNothing().when(aMock).bar

// verifications
aMock wasNever called          // verifyZeroInteractions(aMock)
aMock.bar was called
aMock.bar(*) was called        // '*' is shorthand for 'any()' or 'any[T]'
aMock.bar(any[Int]) was called // same as above but with typed input matcher

aMock.bar wasCalled onlyHere
aMock.bar wasNever called

aMock.bar wasCalled twice
aMock.bar wasCalled 2.times

aMock.bar wasCalled fourTimes
aMock.bar wasCalled 4.times

aMock.bar wasCalled atLeastFiveTimes
aMock.bar wasCalled atLeast(fiveTimes)
aMock.bar wasCalled atLeast(5.times)

aMock.bar wasCalled atMostSixTimes
aMock.bar wasCalled atMost(sixTimes)
aMock.bar wasCalled atMost(6.times)

aMock.bar 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]

  aMock.bar(1, 2) returns "mocked!"
  aMock.bar(1, *) returns "mocked!"
  aMock.bar(1, any[Int]) returns "mocked!"
  aMock.bar(*, *) returns "mocked!"
  aMock.bar(any[Int], any[Int]) returns "mocked!"
  aMock.bar(*, *, 3) returns "mocked!"
  aMock.bar(any[Int], any[Int], 3) returns "mocked!"

  "mocked!" willBe returned by aMock.bar(1, 2)
  "mocked!" willBe returned by aMock.bar(1, *)
  "mocked!" willBe returned by aMock.bar(1, any[Int])
  "mocked!" willBe returned by aMock.bar(*, *)
  "mocked!" willBe returned by aMock.bar(any[Int], any[Int])
  "mocked!" willBe returned by aMock.bar(*, *, 3)
  "mocked!" willBe returned by aMock.bar(any[Int], any[Int], 3)

  aMock.bar(1, 2) was called
  aMock.bar(1, *) was called
  aMock.bar(1, any[Int]) was called
  aMock.bar(*, *) was called
  aMock.bar(any[Int], any[Int]) was called
  aMock.bar(*, *, 3) was called
  aMock.bar(any[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(5)

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.

Vargargs

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

Linear Supertypes
ArgumentMatchersSugar, MacroBasedMatchers, NumericMatchers, Tolerance, FunctionMatchers, NullMatchers, StringThatMatchers, ThatMatchers, EqMatchers_VersionSpecific, EqMatchers, AnyMatchers, IdiomaticMockito, PostfixVerifications, IdiomaticVerifications, IdiomaticStubbing, ScalacticSerialisableHack, MockitoEnhancer, MockCreator, AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Mockito
  2. ArgumentMatchersSugar
  3. MacroBasedMatchers
  4. NumericMatchers
  5. Tolerance
  6. FunctionMatchers
  7. NullMatchers
  8. StringThatMatchers
  9. ThatMatchers
  10. EqMatchers_VersionSpecific
  11. EqMatchers
  12. AnyMatchers
  13. IdiomaticMockito
  14. PostfixVerifications
  15. IdiomaticVerifications
  16. IdiomaticStubbing
  17. ScalacticSerialisableHack
  18. MockitoEnhancer
  19. MockCreator
  20. AnyRef
  21. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Type Members

  1. implicit class DoSomethingOps[R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  2. implicit class DoSomethingOps0[R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  3. implicit class DoSomethingOps1[P0, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  4. implicit class DoSomethingOps10[P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  5. implicit class DoSomethingOps2[P0, P1, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  6. implicit class DoSomethingOps3[P0, P1, P2, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  7. implicit class DoSomethingOps4[P0, P1, P2, P3, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  8. implicit class DoSomethingOps5[P0, P1, P2, P3, P4, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  9. implicit class DoSomethingOps6[P0, P1, P2, P3, P4, P5, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  10. implicit class DoSomethingOps7[P0, P1, P2, P3, P4, P5, P6, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  11. implicit class DoSomethingOps8[P0, P1, P2, P3, P4, P5, P6, P7, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  12. implicit class DoSomethingOps9[P0, P1, P2, P3, P4, P5, P6, P7, P8, R] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  13. final class PlusOrMinusWrapper[T] extends AnyRef
    Definition Classes
    Tolerance
  14. implicit class StubbingOps[T] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  15. implicit class ThrowSomethingOps[E] extends AnyRef
    Definition Classes
    IdiomaticStubbing
  16. type Verification = Unit
    Definition Classes
    IdiomaticMockito → IdiomaticVerifications
  17. implicit class VerificationsIntOps extends AnyRef
    Definition Classes
    PostfixVerifications
  18. implicit class VerifyingOps[T] extends AnyRef
    Definition Classes
    PostfixVerifications

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. def *[T](implicit arg0: AnyMatcher[T]): T
    Definition Classes
    MacroBasedMatchers
  4. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  5. def InOrder(mocks: AnyRef*)(verifications: (VerifyInOrder) => Verification): Verification
    Definition Classes
    PostfixVerifications
  6. val answered: Answered.type
    Definition Classes
    IdiomaticStubbing
  7. def any[T](implicit arg0: AnyMatcher[T]): T
    Definition Classes
    MacroBasedMatchers
  8. def anyBoolean: Boolean
    Definition Classes
    AnyMatchers
  9. def anyByte: Byte
    Definition Classes
    AnyMatchers
  10. def anyChar: Char
    Definition Classes
    AnyMatchers
  11. def anyDouble: Double
    Definition Classes
    AnyMatchers
  12. def anyFloat: Float
    Definition Classes
    AnyMatchers
  13. def anyInt: Int
    Definition Classes
    AnyMatchers
  14. def anyIterable[T]: Iterable[T]
    Definition Classes
    AnyMatchers
  15. def anyList[T]: List[T]
    Definition Classes
    AnyMatchers
  16. def anyLong: Long
    Definition Classes
    AnyMatchers
  17. def anyMap[K, V]: Map[K, V]
    Definition Classes
    AnyMatchers
  18. def anySeq[T]: Seq[T]
    Definition Classes
    AnyMatchers
  19. def anySet[T]: Set[T]
    Definition Classes
    AnyMatchers
  20. def anyShort: Short
    Definition Classes
    AnyMatchers
  21. def argMatching[T](pf: PartialFunction[Any, Unit]): T
    Definition Classes
    ThatMatchers
  22. def argThat[T](f: (T) => Boolean, desc: => String): T
    Definition Classes
    ThatMatchers
  23. def argThat[T](matcher: ArgumentMatcher[T]): T
    Definition Classes
    ThatMatchers
  24. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  25. def atLeast(t: Times): AtLeast
    Definition Classes
    PostfixVerifications
  26. val atLeastEightTimes: AtLeast
    Definition Classes
    PostfixVerifications
  27. val atLeastFiveTimes: AtLeast
    Definition Classes
    PostfixVerifications
  28. val atLeastFourTimes: AtLeast
    Definition Classes
    PostfixVerifications
  29. val atLeastNineTimes: AtLeast
    Definition Classes
    PostfixVerifications
  30. val atLeastOnce: AtLeast
    Definition Classes
    PostfixVerifications
  31. val atLeastSevenTimes: AtLeast
    Definition Classes
    PostfixVerifications
  32. val atLeastSixTimes: AtLeast
    Definition Classes
    PostfixVerifications
  33. val atLeastTenTimes: AtLeast
    Definition Classes
    PostfixVerifications
  34. val atLeastThreeTimes: AtLeast
    Definition Classes
    PostfixVerifications
  35. val atLeastThrice: AtLeast
    Definition Classes
    PostfixVerifications
  36. val atLeastTwice: AtLeast
    Definition Classes
    PostfixVerifications
  37. def atMost(t: Times): AtMost
    Definition Classes
    PostfixVerifications
  38. val atMostEightTimes: AtMost
    Definition Classes
    PostfixVerifications
  39. val atMostFiveTimes: AtMost
    Definition Classes
    PostfixVerifications
  40. val atMostFourTimes: AtMost
    Definition Classes
    PostfixVerifications
  41. val atMostNineTimes: AtMost
    Definition Classes
    PostfixVerifications
  42. val atMostOnce: AtMost
    Definition Classes
    PostfixVerifications
  43. val atMostSevenTimes: AtMost
    Definition Classes
    PostfixVerifications
  44. val atMostSixTimes: AtMost
    Definition Classes
    PostfixVerifications
  45. val atMostTenTimes: AtMost
    Definition Classes
    PostfixVerifications
  46. val atMostThreeTimes: AtMost
    Definition Classes
    PostfixVerifications
  47. val atMostThrice: AtMost
    Definition Classes
    PostfixVerifications
  48. val atMostTwice: AtMost
    Definition Classes
    PostfixVerifications
  49. def booleanThat(matcher: ArgumentMatcher[Boolean]): Boolean
    Definition Classes
    ThatMatchers
  50. def byteThat(matcher: ArgumentMatcher[Byte]): Byte
    Definition Classes
    ThatMatchers
  51. val called: Called.type
    Definition Classes
    IdiomaticStubbing
  52. val calledAgain: CalledAgain.type
    Definition Classes
    PostfixVerifications
  53. def charThat(matcher: ArgumentMatcher[Char]): Char
    Definition Classes
    ThatMatchers
  54. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  55. def contains(substring: String): String
    Definition Classes
    StringThatMatchers
  56. implicit def convertNumericToPlusOrMinusWrapper[T](pivot: T)(implicit arg0: Numeric[T]): PlusOrMinusWrapper[T]
    Definition Classes
    Tolerance
  57. def doubleThat(matcher: ArgumentMatcher[Double]): Double
    Definition Classes
    ThatMatchers
  58. val eightTimes: Times
    Definition Classes
    PostfixVerifications
  59. def endsWith(suffix: String): String
    Definition Classes
    StringThatMatchers
  60. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  61. def eqTo[T](value: T)(implicit arg0: Equality[T], arg1: ValueClassExtractor[T], arg2: Prettifier): T
    Definition Classes
    EqMatchers_VersionSpecific
  62. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  63. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  64. val fiveTimes: Times
    Definition Classes
    PostfixVerifications
  65. def floatThat(matcher: ArgumentMatcher[Float]): Float
    Definition Classes
    ThatMatchers
  66. val fourTimes: Times
    Definition Classes
    PostfixVerifications
  67. def function0[T](value: T): () => T
    Definition Classes
    FunctionMatchers
  68. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  69. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  70. def ignoreStubs(mocks: AnyRef*): Array[AnyRef]
    Definition Classes
    MockitoEnhancer
  71. val ignoringStubs: IgnoringStubs.type
    Definition Classes
    PostfixVerifications
  72. def intThat(matcher: ArgumentMatcher[Int]): Int
    Definition Classes
    ThatMatchers
  73. implicit val invocationOps: (InvocationOnMock) => InvocationOnMockOps
    Definition Classes
    MockitoEnhancer
  74. def isA[T](implicit arg0: ClassTag[T]): T
    Definition Classes
    EqMatchers
  75. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  76. def longThat(matcher: ArgumentMatcher[Long]): Long
    Definition Classes
    ThatMatchers
  77. def matches(regex: String): String
    Definition Classes
    StringThatMatchers
  78. def mock[T <: AnyRef](name: String)(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], defaultAnswer: DefaultAnswer, arg3: Prettifier): T
    Definition Classes
    MockitoEnhancer → MockCreator
  79. def mock[T <: AnyRef](mockSettings: MockSettings)(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], arg2: Prettifier): T
    Definition Classes
    MockitoEnhancer → MockCreator
  80. def mock[T <: AnyRef](defaultAnswer: DefaultAnswer)(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], arg2: Prettifier): T
    Definition Classes
    MockitoEnhancer → MockCreator
  81. def mock[T <: AnyRef](implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], defaultAnswer: DefaultAnswer, arg3: Prettifier): T
    Definition Classes
    MockitoEnhancer → MockCreator
  82. def mock[T <: AnyRef](defaultAnswer: Answer[_])(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], arg2: Prettifier): T
    Definition Classes
    MockCreator
  83. def mockingDetails(toInspect: AnyRef): MockingDetails
    Definition Classes
    MockitoEnhancer
  84. implicit def mockitoSerialisableEquality[T]: Equality[T]
    Definition Classes
    ScalacticSerialisableHack
  85. val n: N
    Definition Classes
    NumericMatchers
  86. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  87. val nineTimes: Times
    Definition Classes
    PostfixVerifications
  88. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  89. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  90. val on: On.type
    Definition Classes
    PostfixVerifications
  91. val once: Times
    Definition Classes
    PostfixVerifications
  92. val onlyHere: OnlyOn.type
    Definition Classes
    PostfixVerifications
  93. val realMethod: RealMethod.type
    Definition Classes
    IdiomaticStubbing
  94. def refEq[T](value: T, excludeFields: String*): T
    Definition Classes
    EqMatchers
  95. def reset(mocks: AnyRef*)(implicit arg0: Prettifier): Unit
    Definition Classes
    MockitoEnhancer
  96. val returned: Returned.type
    Definition Classes
    IdiomaticStubbing
  97. def same[T](value: T): T
    Definition Classes
    EqMatchers
  98. val sevenTimes: Times
    Definition Classes
    PostfixVerifications
  99. def shortThat(matcher: ArgumentMatcher[Short]): Short
    Definition Classes
    ThatMatchers
  100. val sixTimes: Times
    Definition Classes
    PostfixVerifications
  101. def spy[T <: AnyRef](realObj: T, lenient: Boolean)(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], arg2: Prettifier): T
    Definition Classes
    MockitoEnhancer → MockCreator
  102. def spyLambda[T <: AnyRef](realObj: T)(implicit arg0: ClassTag[T]): T
    Definition Classes
    MockitoEnhancer → MockCreator
  103. def startsWith(prefix: String): String
    Definition Classes
    StringThatMatchers
  104. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  105. val tenTimes: Times
    Definition Classes
    PostfixVerifications
  106. val theRealMethod: RealMethod.type
    Definition Classes
    IdiomaticStubbing
  107. val threeTimes: Times
    Definition Classes
    PostfixVerifications
  108. val thrice: Times
    Definition Classes
    PostfixVerifications
  109. val thrown: Thrown.type
    Definition Classes
    IdiomaticStubbing
  110. def toString(): String
    Definition Classes
    AnyRef → Any
  111. val twice: Times
    Definition Classes
    PostfixVerifications
  112. def verification(v: => Any): Verification
    Definition Classes
    IdiomaticMockito → IdiomaticVerifications
  113. def verifyNoMoreInteractions(mocks: AnyRef*): Unit
    Definition Classes
    MockitoEnhancer
  114. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  115. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  116. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  117. def withSettings(implicit defaultAnswer: DefaultAnswer): MockSettings
    Definition Classes
    MockCreator

Deprecated Value Members

  1. def anyVal[T](implicit arg0: AnyMatcher[T]): T
    Definition Classes
    MacroBasedMatchers
    Annotations
    @deprecated
    Deprecated

    (Since version 1.0.2) Use 'any[T]' or '*[T]' instead

  2. def eqToVal[T](value: T)(implicit arg0: Equality[T], arg1: ValueClassExtractor[T], arg2: Prettifier): T
    Definition Classes
    EqMatchers_VersionSpecific
    Annotations
    @deprecated
    Deprecated

    (Since version 1.0.2) Use 'eqTo' instead

  3. def isNotNull[T]: T
    Definition Classes
    NullMatchers
    Annotations
    @deprecated
    Deprecated

    (Since version 0.0.0) Using nulls in Scala? you naughty, naughty developer...

  4. def isNull[T]: T
    Definition Classes
    NullMatchers
    Annotations
    @deprecated
    Deprecated

    (Since version 0.0.0) Using nulls in Scala? you naughty, naughty developer...

Inherited from ArgumentMatchersSugar

Inherited from MacroBasedMatchers

Inherited from NumericMatchers

Inherited from Tolerance

Inherited from FunctionMatchers

Inherited from NullMatchers

Inherited from StringThatMatchers

Inherited from ThatMatchers

Inherited from EqMatchers_VersionSpecific

Inherited from EqMatchers

Inherited from AnyMatchers

Inherited from IdiomaticMockito

Inherited from PostfixVerifications

Inherited from IdiomaticVerifications

Inherited from IdiomaticStubbing

Inherited from ScalacticSerialisableHack

Inherited from MockitoEnhancer

Inherited from MockCreator

Inherited from AnyRef

Inherited from Any

Ungrouped