package
scalamock
Type Members
-
class
ArgumentMatcher extends (Product) ⇒ Boolean
-
case class
Call(target: FakeFunction, arguments: Product) extends Product with Serializable
-
abstract
class
CallHandler[R] extends Handler
-
-
-
-
-
-
-
-
class
CallHandler7[T1, T2, T3, T4, T5, T6, T7, R] extends CallHandler[R]
-
class
CallHandler8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends CallHandler[R]
-
class
CallHandler9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends CallHandler[R]
-
trait
Defaultable[T] extends AnyRef
-
-
abstract
class
FakeFunction extends AnyRef
-
-
-
-
-
-
abstract
class
FakeFunction5[T1, T2, T3, T4, T5, R] extends FakeFunction with (T1, T2, T3, T4, T5) ⇒ R with NiceToString
-
abstract
class
FakeFunction6[T1, T2, T3, T4, T5, T6, R] extends FakeFunction with (T1, T2, T3, T4, T5, T6) ⇒ R with NiceToString
-
abstract
class
FakeFunction7[T1, T2, T3, T4, T5, T6, T7, R] extends FakeFunction with (T1, T2, T3, T4, T5, T6, T7) ⇒ R with NiceToString
-
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
-
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
-
-
class
FunctionAdapter1[T1, R] extends (Product) ⇒ R
-
class
FunctionAdapter2[T1, T2, R] extends (Product) ⇒ R
-
class
FunctionAdapter3[T1, T2, T3, R] extends (Product) ⇒ R
-
class
FunctionAdapter4[T1, T2, T3, T4, R] extends (Product) ⇒ R
-
class
FunctionAdapter5[T1, T2, T3, T4, T5, R] extends (Product) ⇒ R
-
class
FunctionAdapter6[T1, T2, T3, T4, T5, T6, R] extends (Product) ⇒ R
-
class
FunctionAdapter7[T1, T2, T3, T4, T5, T6, T7, R] extends (Product) ⇒ R
-
class
FunctionAdapter8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends (Product) ⇒ R
-
class
FunctionAdapter9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends (Product) ⇒ R
-
-
class
MatchAny extends Equals
-
-
trait
Mock extends AnyRef
-
-
-
-
-
-
-
-
-
-
class
MockFunction7[T1, T2, T3, T4, T5, T6, T7, R] extends FakeFunction7[T1, T2, T3, T4, T5, T6, T7, R] with MockFunction
-
class
MockFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends FakeFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R] with MockFunction
-
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
-
-
-
-
-
-
-
class
StubFunction4[T1, T2, T3, T4, R] extends FakeFunction4[T1, T2, T3, T4, R]
-
class
StubFunction5[T1, T2, T3, T4, T5, R] extends FakeFunction5[T1, T2, T3, T4, T5, R]
-
class
StubFunction6[T1, T2, T3, T4, T5, T6, R] extends FakeFunction6[T1, T2, T3, T4, T5, T6, R]
-
class
StubFunction7[T1, T2, T3, T4, T5, T6, T7, R] extends FakeFunction7[T1, T2, T3, T4, T5, T6, T7, R]
-
class
StubFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R] extends FakeFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R]
-
class
StubFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] extends FakeFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]
-
trait
Verify extends AnyRef
Inherited from AnyRef
Inherited from Any
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 singleInt
argument and returning aString
:A mock object that supports this style is created with
mock
. For example, to create a mock that implements theTurtle
trait:val m = mock[Turtle]
Expectations can then be set using
expects
: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: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 usingverify
: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:
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:
but will not match:
Repeated parameters
Repeated parameters are represented as a
Seq
. For example, given:you can set an expectation with:
Predicate matching
More complicated argument matching can be implemented by using
where
to pass a predicate:Return values
By default mocks and stubs return
null
. You can return a computed return value (or throw a computed exception) withonCall
:Overloaded, curried and polymorphic methods
Overloaded, curried and polymorphic methods can be mocked by specifying either argument types or type parameters. For example:
Exceptions
Instead of a return value, mocks and stubs can be instructed to throw:
Call count
By default, mocks and stubs expect exactly one call. Alternative constraints can be set with
repeat
:There are various aliases for common expectations and styles:
For a full list, see org.scalamock.CallHandler.
Ordering
By default, expectations can be satisfied in any order. For example:
A specific sequence can be enforced with
inSequence
: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:
To specify that there is no constraint on ordering, use
inAnyOrder
(there is an implicitinAnyOrder
at the top level). Calls toinSequence
andinAnyOrder
can be arbitrarily nested. For example: