Trait/Object

com.twitter.util.mock

Mockito

Related Docs: object Mockito | package mock

Permalink

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. All

Type Members

  1. implicit class DoSomethingOps[R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  2. implicit class DoSomethingOps0[R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  3. implicit class DoSomethingOps1[P0, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  4. implicit class DoSomethingOps10[P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  5. implicit class DoSomethingOps2[P0, P1, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  6. implicit class DoSomethingOps3[P0, P1, P2, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  7. implicit class DoSomethingOps4[P0, P1, P2, P3, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  8. implicit class DoSomethingOps5[P0, P1, P2, P3, P4, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  9. implicit class DoSomethingOps6[P0, P1, P2, P3, P4, P5, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  10. implicit class DoSomethingOps7[P0, P1, P2, P3, P4, P5, P6, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  11. implicit class DoSomethingOps8[P0, P1, P2, P3, P4, P5, P6, P7, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  12. implicit class DoSomethingOps9[P0, P1, P2, P3, P4, P5, P6, P7, P8, R] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  13. final class PlusOrMinusWrapper[T] extends AnyRef

    Permalink
    Definition Classes
    Tolerance
  14. implicit class StubbingOps[T] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  15. implicit class ThrowSomethingOps[E] extends AnyRef

    Permalink
    Definition Classes
    IdiomaticStubbing
  16. type Verification = Unit

    Permalink
    Definition Classes
    IdiomaticMockito → IdiomaticVerifications
  17. implicit class VerificationsIntOps extends AnyRef

    Permalink
    Definition Classes
    PostfixVerifications
  18. implicit class VerifyingOps[T] extends AnyRef

    Permalink
    Definition Classes
    PostfixVerifications

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. def *[T](implicit arg0: AnyMatcher[T]): T

    Permalink
    Definition Classes
    MacroBasedMatchers
  4. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  5. def InOrder(mocks: AnyRef*)(verifications: (VerifyInOrder) ⇒ Verification): Verification

    Permalink
    Definition Classes
    PostfixVerifications
  6. val answered: Answered.type

    Permalink
    Definition Classes
    IdiomaticStubbing
  7. def any[T](implicit arg0: AnyMatcher[T]): T

    Permalink
    Definition Classes
    MacroBasedMatchers
  8. def anyBoolean: Boolean

    Permalink
    Definition Classes
    AnyMatchers
  9. def anyByte: Byte

    Permalink
    Definition Classes
    AnyMatchers
  10. def anyChar: Char

    Permalink
    Definition Classes
    AnyMatchers
  11. def anyDouble: Double

    Permalink
    Definition Classes
    AnyMatchers
  12. def anyFloat: Float

    Permalink
    Definition Classes
    AnyMatchers
  13. def anyInt: Int

    Permalink
    Definition Classes
    AnyMatchers
  14. def anyIterable[T]: Iterable[T]

    Permalink
    Definition Classes
    AnyMatchers
  15. def anyList[T]: List[T]

    Permalink
    Definition Classes
    AnyMatchers
  16. def anyLong: Long

    Permalink
    Definition Classes
    AnyMatchers
  17. def anyMap[K, V]: Map[K, V]

    Permalink
    Definition Classes
    AnyMatchers
  18. def anySeq[T]: Seq[T]

    Permalink
    Definition Classes
    AnyMatchers
  19. def anySet[T]: Set[T]

    Permalink
    Definition Classes
    AnyMatchers
  20. def anyShort: Short

    Permalink
    Definition Classes
    AnyMatchers
  21. def argMatching[T](pf: PartialFunction[Any, Unit]): T

    Permalink
    Definition Classes
    ThatMatchers
  22. def argThat[T](f: (T) ⇒ Boolean, desc: ⇒ String): T

    Permalink
    Definition Classes
    ThatMatchers
  23. def argThat[T](matcher: ArgumentMatcher[T]): T

    Permalink
    Definition Classes
    ThatMatchers
  24. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  25. def atLeast(t: Times): AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  26. val atLeastEightTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  27. val atLeastFiveTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  28. val atLeastFourTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  29. val atLeastNineTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  30. val atLeastOnce: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  31. val atLeastSevenTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  32. val atLeastSixTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  33. val atLeastTenTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  34. val atLeastThreeTimes: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  35. val atLeastThrice: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  36. val atLeastTwice: AtLeast

    Permalink
    Definition Classes
    PostfixVerifications
  37. def atMost(t: Times): AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  38. val atMostEightTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  39. val atMostFiveTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  40. val atMostFourTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  41. val atMostNineTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  42. val atMostOnce: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  43. val atMostSevenTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  44. val atMostSixTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  45. val atMostTenTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  46. val atMostThreeTimes: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  47. val atMostThrice: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  48. val atMostTwice: AtMost

    Permalink
    Definition Classes
    PostfixVerifications
  49. def booleanThat(matcher: ArgumentMatcher[Boolean]): Boolean

    Permalink
    Definition Classes
    ThatMatchers
  50. def byteThat(matcher: ArgumentMatcher[Byte]): Byte

    Permalink
    Definition Classes
    ThatMatchers
  51. val called: Called.type

    Permalink
    Definition Classes
    IdiomaticStubbing
  52. val calledAgain: CalledAgain.type

    Permalink
    Definition Classes
    PostfixVerifications
  53. def charThat(matcher: ArgumentMatcher[Char]): Char

    Permalink
    Definition Classes
    ThatMatchers
  54. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  55. def contains(substring: String): String

    Permalink
    Definition Classes
    StringThatMatchers
  56. implicit def convertNumericToPlusOrMinusWrapper[T](pivot: T)(implicit arg0: Numeric[T]): PlusOrMinusWrapper[T]

    Permalink
    Definition Classes
    Tolerance
  57. def doubleThat(matcher: ArgumentMatcher[Double]): Double

    Permalink
    Definition Classes
    ThatMatchers
  58. val eightTimes: Times

    Permalink
    Definition Classes
    PostfixVerifications
  59. def endsWith(suffix: String): String

    Permalink
    Definition Classes
    StringThatMatchers
  60. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  61. macro def eqTo[T](value: T): T

    Permalink
    Definition Classes
    EqMatchers_VersionSpecific
  62. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  63. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  64. val fiveTimes: Times

    Permalink
    Definition Classes
    PostfixVerifications
  65. def floatThat(matcher: ArgumentMatcher[Float]): Float

    Permalink
    Definition Classes
    ThatMatchers
  66. val fourTimes: Times

    Permalink
    Definition Classes
    PostfixVerifications
  67. def function0[T](value: T): () ⇒ T

    Permalink
    Definition Classes
    FunctionMatchers
  68. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  69. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  70. def ignoreStubs(mocks: AnyRef*): Array[AnyRef]

    Permalink
    Definition Classes
    MockitoEnhancer
  71. val ignoringStubs: IgnoringStubs.type

    Permalink
    Definition Classes
    PostfixVerifications
  72. def intThat(matcher: ArgumentMatcher[Int]): Int

    Permalink
    Definition Classes
    ThatMatchers
  73. implicit val invocationOps: (InvocationOnMock) ⇒ InvocationOnMockOps

    Permalink
    Definition Classes
    MockitoEnhancer
  74. def isA[T](implicit arg0: ClassTag[T]): T

    Permalink
    Definition Classes
    EqMatchers
  75. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  76. def longThat(matcher: ArgumentMatcher[Long]): Long

    Permalink
    Definition Classes
    ThatMatchers
  77. def matches(regex: String): String

    Permalink
    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

    Permalink
    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

    Permalink
    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

    Permalink
    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

    Permalink
    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

    Permalink
    Definition Classes
    MockCreator
  83. def mockingDetails(toInspect: AnyRef): MockingDetails

    Permalink
    Definition Classes
    MockitoEnhancer
  84. implicit def mockitoSerialisableEquality[T]: Equality[T]

    Permalink
    Definition Classes
    ScalacticSerialisableHack
  85. val n: N

    Permalink
    Definition Classes
    NumericMatchers
  86. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  87. val nineTimes: Times

    Permalink
    Definition Classes
    PostfixVerifications
  88. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  89. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  90. val on: On.type

    Permalink
    Definition Classes
    PostfixVerifications
  91. val once: Times

    Permalink
    Definition Classes
    PostfixVerifications
  92. val onlyHere: OnlyOn.type

    Permalink
    Definition Classes
    PostfixVerifications
  93. val realMethod: RealMethod.type

    Permalink
    Definition Classes
    IdiomaticStubbing
  94. def refEq[T](value: T, excludeFields: String*): T

    Permalink
    Definition Classes
    EqMatchers
  95. def reset(mocks: AnyRef*)(implicit arg0: Prettifier): Unit

    Permalink
    Definition Classes
    MockitoEnhancer
  96. val returned: Returned.type

    Permalink
    Definition Classes
    IdiomaticStubbing
  97. def same[T](value: T): T

    Permalink
    Definition Classes
    EqMatchers
  98. val sevenTimes: Times

    Permalink
    Definition Classes
    PostfixVerifications
  99. def shortThat(matcher: ArgumentMatcher[Short]): Short

    Permalink
    Definition Classes
    ThatMatchers
  100. val sixTimes: Times

    Permalink
    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

    Permalink
    Definition Classes
    MockitoEnhancer → MockCreator
  102. def spyLambda[T <: AnyRef](realObj: T)(implicit arg0: ClassTag[T]): T

    Permalink
    Definition Classes
    MockitoEnhancer → MockCreator
  103. def startsWith(prefix: String): String

    Permalink
    Definition Classes
    StringThatMatchers
  104. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  105. val tenTimes: Times

    Permalink
    Definition Classes
    PostfixVerifications
  106. val theRealMethod: RealMethod.type

    Permalink
    Definition Classes
    IdiomaticStubbing
  107. val threeTimes: Times

    Permalink
    Definition Classes
    PostfixVerifications
  108. val thrice: Times

    Permalink
    Definition Classes
    PostfixVerifications
  109. val thrown: Thrown.type

    Permalink
    Definition Classes
    IdiomaticStubbing
  110. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  111. val twice: Times

    Permalink
    Definition Classes
    PostfixVerifications
  112. def verification(v: ⇒ Any): Verification

    Permalink
    Definition Classes
    IdiomaticMockito → IdiomaticVerifications
  113. def verifyNoMoreInteractions(mocks: AnyRef*): Unit

    Permalink
    Definition Classes
    MockitoEnhancer
  114. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  115. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  116. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  117. def withSettings(implicit defaultAnswer: DefaultAnswer): MockSettings

    Permalink
    Definition Classes
    MockCreator

Deprecated Value Members

  1. def anyVal[T](implicit arg0: AnyMatcher[T]): T

    Permalink
    Definition Classes
    MacroBasedMatchers
    Annotations
    @deprecated
    Deprecated

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

  2. macro def eqToVal[T](value: T): T

    Permalink
    Definition Classes
    EqMatchers_VersionSpecific
    Annotations
    @deprecated
    Deprecated

    (Since version 1.0.2) Use 'eqTo' instead

  3. def isNotNull[T]: T

    Permalink
    Definition Classes
    NullMatchers
    Annotations
    @deprecated
    Deprecated

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

  4. def isNull[T]: T

    Permalink
    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