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 also
org.mockito.IdiomaticMockito
org.mockito.ArgumentMatchersSugar
- Alphabetic
- By Inheritance
- Mockito
- ArgumentMatchersSugar
- MacroBasedMatchers
- NumericMatchers
- Tolerance
- FunctionMatchers
- NullMatchers
- StringThatMatchers
- ThatMatchers
- EqMatchers_VersionSpecific
- EqMatchers
- AnyMatchers
- IdiomaticMockito
- PostfixVerifications
- IdiomaticVerifications
- IdiomaticStubbing
- ScalacticSerialisableHack
- MockitoEnhancer
- MockCreator
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
implicit
class
DoSomethingOps[R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps0[R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps1[P0, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps10[P0, P1, P2, P3, P4, P5, P6, P7, P8, P9, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps2[P0, P1, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps3[P0, P1, P2, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps4[P0, P1, P2, P3, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps5[P0, P1, P2, P3, P4, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps6[P0, P1, P2, P3, P4, P5, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps7[P0, P1, P2, P3, P4, P5, P6, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps8[P0, P1, P2, P3, P4, P5, P6, P7, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
DoSomethingOps9[P0, P1, P2, P3, P4, P5, P6, P7, P8, R] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
final
class
PlusOrMinusWrapper[T] extends AnyRef
- Definition Classes
- Tolerance
-
implicit
class
StubbingOps[T] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
implicit
class
ThrowSomethingOps[E] extends AnyRef
- Definition Classes
- IdiomaticStubbing
-
type
Verification = Unit
- Definition Classes
- IdiomaticMockito → IdiomaticVerifications
-
implicit
class
VerificationsIntOps extends AnyRef
- Definition Classes
- PostfixVerifications
-
implicit
class
VerifyingOps[T] extends AnyRef
- Definition Classes
- PostfixVerifications
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
def
*[T](implicit arg0: AnyMatcher[T]): T
- Definition Classes
- MacroBasedMatchers
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
InOrder(mocks: AnyRef*)(verifications: (VerifyInOrder) ⇒ Verification): Verification
- Definition Classes
- PostfixVerifications
-
val
answered: Answered.type
- Definition Classes
- IdiomaticStubbing
-
def
any[T](implicit arg0: AnyMatcher[T]): T
- Definition Classes
- MacroBasedMatchers
-
def
anyBoolean: Boolean
- Definition Classes
- AnyMatchers
-
def
anyByte: Byte
- Definition Classes
- AnyMatchers
-
def
anyChar: Char
- Definition Classes
- AnyMatchers
-
def
anyDouble: Double
- Definition Classes
- AnyMatchers
-
def
anyFloat: Float
- Definition Classes
- AnyMatchers
-
def
anyInt: Int
- Definition Classes
- AnyMatchers
-
def
anyIterable[T]: Iterable[T]
- Definition Classes
- AnyMatchers
-
def
anyList[T]: List[T]
- Definition Classes
- AnyMatchers
-
def
anyLong: Long
- Definition Classes
- AnyMatchers
-
def
anyMap[K, V]: Map[K, V]
- Definition Classes
- AnyMatchers
-
def
anySeq[T]: Seq[T]
- Definition Classes
- AnyMatchers
-
def
anySet[T]: Set[T]
- Definition Classes
- AnyMatchers
-
def
anyShort: Short
- Definition Classes
- AnyMatchers
-
def
argMatching[T](pf: PartialFunction[Any, Unit]): T
- Definition Classes
- ThatMatchers
-
def
argThat[T](f: (T) ⇒ Boolean, desc: ⇒ String): T
- Definition Classes
- ThatMatchers
-
def
argThat[T](matcher: ArgumentMatcher[T]): T
- Definition Classes
- ThatMatchers
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
atLeast(t: Times): AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastEightTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastFiveTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastFourTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastNineTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastOnce: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastSevenTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastSixTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastTenTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastThreeTimes: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastThrice: AtLeast
- Definition Classes
- PostfixVerifications
-
val
atLeastTwice: AtLeast
- Definition Classes
- PostfixVerifications
-
def
atMost(t: Times): AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostEightTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostFiveTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostFourTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostNineTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostOnce: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostSevenTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostSixTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostTenTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostThreeTimes: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostThrice: AtMost
- Definition Classes
- PostfixVerifications
-
val
atMostTwice: AtMost
- Definition Classes
- PostfixVerifications
-
def
booleanThat(matcher: ArgumentMatcher[Boolean]): Boolean
- Definition Classes
- ThatMatchers
-
def
byteThat(matcher: ArgumentMatcher[Byte]): Byte
- Definition Classes
- ThatMatchers
-
val
called: Called.type
- Definition Classes
- IdiomaticStubbing
-
val
calledAgain: CalledAgain.type
- Definition Classes
- PostfixVerifications
-
def
charThat(matcher: ArgumentMatcher[Char]): Char
- Definition Classes
- ThatMatchers
-
def
clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
contains(substring: String): String
- Definition Classes
- StringThatMatchers
-
implicit
def
convertNumericToPlusOrMinusWrapper[T](pivot: T)(implicit arg0: Numeric[T]): PlusOrMinusWrapper[T]
- Definition Classes
- Tolerance
-
def
doubleThat(matcher: ArgumentMatcher[Double]): Double
- Definition Classes
- ThatMatchers
-
val
eightTimes: Times
- Definition Classes
- PostfixVerifications
-
def
endsWith(suffix: String): String
- Definition Classes
- StringThatMatchers
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
eqTo[T](value: T)(implicit arg0: Equality[T], arg1: ValueClassExtractor[T], arg2: Prettifier): T
- Definition Classes
- EqMatchers_VersionSpecific
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
val
fiveTimes: Times
- Definition Classes
- PostfixVerifications
-
def
floatThat(matcher: ArgumentMatcher[Float]): Float
- Definition Classes
- ThatMatchers
-
val
fourTimes: Times
- Definition Classes
- PostfixVerifications
-
def
function0[T](value: T): () ⇒ T
- Definition Classes
- FunctionMatchers
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
ignoreStubs(mocks: AnyRef*): Array[AnyRef]
- Definition Classes
- MockitoEnhancer
-
val
ignoringStubs: IgnoringStubs.type
- Definition Classes
- PostfixVerifications
-
def
intThat(matcher: ArgumentMatcher[Int]): Int
- Definition Classes
- ThatMatchers
-
implicit
val
invocationOps: (InvocationOnMock) ⇒ InvocationOnMockOps
- Definition Classes
- MockitoEnhancer
-
def
isA[T](implicit arg0: ClassTag[T]): T
- Definition Classes
- EqMatchers
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
longThat(matcher: ArgumentMatcher[Long]): Long
- Definition Classes
- ThatMatchers
-
def
matches(regex: String): String
- Definition Classes
- StringThatMatchers
-
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
-
def
mock[T <: AnyRef](mockSettings: MockSettings)(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], arg2: Prettifier): T
- Definition Classes
- MockitoEnhancer → MockCreator
-
def
mock[T <: AnyRef](defaultAnswer: DefaultAnswer)(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], arg2: Prettifier): T
- Definition Classes
- MockitoEnhancer → MockCreator
-
def
mock[T <: AnyRef](implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], defaultAnswer: DefaultAnswer, arg3: Prettifier): T
- Definition Classes
- MockitoEnhancer → MockCreator
-
def
mock[T <: AnyRef](defaultAnswer: Answer[_])(implicit arg0: ClassTag[T], arg1: scala.reflect.api.JavaUniverse.WeakTypeTag[T], arg2: Prettifier): T
- Definition Classes
- MockCreator
-
def
mockingDetails(toInspect: AnyRef): MockingDetails
- Definition Classes
- MockitoEnhancer
-
implicit
def
mockitoSerialisableEquality[T]: Equality[T]
- Definition Classes
- ScalacticSerialisableHack
-
val
n: N
- Definition Classes
- NumericMatchers
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
val
nineTimes: Times
- Definition Classes
- PostfixVerifications
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
val
on: On.type
- Definition Classes
- PostfixVerifications
-
val
once: Times
- Definition Classes
- PostfixVerifications
-
val
onlyHere: OnlyOn.type
- Definition Classes
- PostfixVerifications
-
val
realMethod: RealMethod.type
- Definition Classes
- IdiomaticStubbing
-
def
refEq[T](value: T, excludeFields: String*): T
- Definition Classes
- EqMatchers
-
def
reset(mocks: AnyRef*)(implicit arg0: Prettifier): Unit
- Definition Classes
- MockitoEnhancer
-
val
returned: Returned.type
- Definition Classes
- IdiomaticStubbing
-
def
same[T](value: T): T
- Definition Classes
- EqMatchers
-
val
sevenTimes: Times
- Definition Classes
- PostfixVerifications
-
def
shortThat(matcher: ArgumentMatcher[Short]): Short
- Definition Classes
- ThatMatchers
-
val
sixTimes: Times
- Definition Classes
- PostfixVerifications
-
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
-
def
spyLambda[T <: AnyRef](realObj: T)(implicit arg0: ClassTag[T]): T
- Definition Classes
- MockitoEnhancer → MockCreator
-
def
startsWith(prefix: String): String
- Definition Classes
- StringThatMatchers
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
val
tenTimes: Times
- Definition Classes
- PostfixVerifications
-
val
theRealMethod: RealMethod.type
- Definition Classes
- IdiomaticStubbing
-
val
threeTimes: Times
- Definition Classes
- PostfixVerifications
-
val
thrice: Times
- Definition Classes
- PostfixVerifications
-
val
thrown: Thrown.type
- Definition Classes
- IdiomaticStubbing
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
val
twice: Times
- Definition Classes
- PostfixVerifications
-
def
verification(v: ⇒ Any): Verification
- Definition Classes
- IdiomaticMockito → IdiomaticVerifications
-
def
verifyNoMoreInteractions(mocks: AnyRef*): Unit
- Definition Classes
- MockitoEnhancer
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
withSettings(implicit defaultAnswer: DefaultAnswer): MockSettings
- Definition Classes
- MockCreator
Deprecated Value Members
-
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
-
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
-
def
isNotNull[T]: T
- Definition Classes
- NullMatchers
- Annotations
- @deprecated
- Deprecated
(Since version 0.0.0) Using nulls in Scala? you naughty, naughty developer...
-
def
isNull[T]: T
- Definition Classes
- NullMatchers
- Annotations
- @deprecated
- Deprecated
(Since version 0.0.0) Using nulls in Scala? you naughty, naughty developer...