org.specs.mock.JMocker

trait JMocker

trait JMocker

 extends JMockerExampleLifeCycle with HamcrestMatchers with JMockActions with ExpectationsListener


The JMocker trait is used to give access to the mocking functionalities of the JMock library


Companion: JMocker

Source: JMocker.scala(31)

Direct Known Subclasses

JMocker,

Nested Classes

CapturingParam, ClassToMock, ExpectBlock, HamcrestMatcherAdapter, InSequenceThen, IntCallConstraint, JMockAction, RangeCallConstraint, StateConstraint,

 Fields

addExpectation abstract def addExpectation Example ExpectationsListener
anyBoolean def anyBoolean
shortcut for expectations [details]
`with`(new IsAnything[Boolean])
Boolean
anyByte def anyByte
shortcut for expectations [details]
`with`(new IsAnything[Byte])
Byte
anyChar def anyChar
shortcut for expectations [details]
`with`(new IsAnything[Char])
Char
anyDouble def anyDouble
shortcut for expectations [details]
`with`(new IsAnything[Double])
Double
anyFloat def anyFloat
shortcut for expectations [details]
`with`(new IsAnything[Float])
Float
anyInt def anyInt
shortcut for expectations [details]
`with`(new IsAnything[Int])
Int
anyLong def anyLong
shortcut for expectations [details]
`with`(new IsAnything[Long])
Long
anyShort def anyShort
shortcut for expectations [details]
`with`(new IsAnything[Short])
Short
anyString def anyString
shortcut for expectations [details]
`with`(new IsAnything[String])
String
context var context
the context holds the mocks, the expectations so is able to get the calls and check them
Mockery JMockerContext
createMockery def createMockery Mockery Imposterizer
example protected[this] var example Option[Example] ExampleLifeCycle
expectations var expectations
call expectations with optional constraints on the number of calls, on parameters, return values, [details]
..
Expectations JMockerContext
isSequential def isSequential Boolean ExampleLifeCycle
newMockery def newMockery Mockery Imposterizer
sequential protected var sequential Boolean ExampleLifeCycle
until def until Boolean ExampleLifeCycle

 Methods

!= final def !=(arg0 : Object) Boolean AnyRef
!= final def !=(arg0 : Any) Boolean Any
== final def ==(arg0 : Any) Boolean Any
== final def ==(arg0 : Object) Boolean AnyRef
RangeToCallConstraint implicit def RangeToCallConstraint(r : Range)
Adds call constraint method to a range, to express the between constraint
RangeCallConstraint
a def a[T]
always match the parameter
T
a def a[T](implicit m : Manifest[T])
shortcut for expectations [details]
`with`(new IsInstanceOf[T])
T
aNonNull def aNonNull[T](implicit m : Manifest[T])
shortcut for expectations [details]
`with`(new IsNot(IsNull[T]))
T
aNull def aNull[T](implicit m : Manifest[T])
shortcut for expectations [details]
`with`(new IsNull[T])
T
after implicit def after(v : => Any)
this class allows an expectation to declare that another expectation should follow
InSequenceThen
afterCall implicit def afterCall(v : Any)
allows any block of expectations to be followed by state actions and expectations
StateConstraint
afterExample def afterExample(ex : Example) Unit ExampleLifeCycle
afterTest override def afterTest(ex : Example)
After a test the context is verified [details]
If some more expectations are not met an ExpectationError is thrown In that case, it is transformed to a failure exception
Unit JMockerExampleLifeCycle
allowing def allowing[T](mockObjectMatcher : Matcher[T])
allowing any calls to mocks
MethodClause
allowing def allowing[T](mockObject : T)
allowing any calls to the mock
T
allowingMatch def allowingMatch(methodName : String)
allowing any calls to any mock with method names like the passed parameter, returning default values
ParametersClause
allowingMatch def allowingMatch[T](mock : T, methodName : String)
allowing any calls to a mock with method names like the passed parameter, returning default values
ParametersClause
an def an[T]
always match the parameter
T
an def an[T](implicit m : Manifest[T])
shortcut for expectations [details]
`with`(new IsInstanceOf[T])
T
any def any[T](implicit m : Manifest[T])
shortcut for expectations [details]
`with`(new IsAnything[T])
T
anyToExpectationCounter implicit def anyToExpectationCounter[T](a : => T)
Adds an isExpectation method to any block of code (mock expectation, scalacheck property) to better count the number of expectations
ExpectationCounter[T] ExpectationsListener
anything def anything[T]
shortcut for new IsAnything[T]
IsAnything[T] HamcrestMatchers
as def as[T](name : String)(expects : (T) => Any)(implicit m : Manifest[T])
mocks a class and add expectations, specifying the mock with a name [details]
return
- s the mocked object and the evaluation of the block of expectations
(T, (T) => Any)
as def as[T](expects : (T) => Any)(implicit m : Manifest[T])
mocks a class and add expectations [details]
Usage willReturn(as[MyInterface]{m: MyInterface => one(m).method })
return
- s the mocked object and the evaluation of the block of expectations
(T, (T) => Any)
as def as[T](expects : (T) => Any*)(implicit m : Manifest[T])
mocks a class several times and add expectations for each mock [details]
Usage willReturn(as[MyInterface]), {m1: MyInterface => one(m1).method }, {m2: MyInterface => one(m2).method }, ) However, it is shorter to use willReturnIterable: one(workspace).projects willReturnIterable(as[Project], {p: Project => one(p).name willReturn "p1" }, {p: Project => one(p).name willReturn "p2" })
return
- s the mocked object and the evaluation of the block of expectations
List[(T, (T) => Any)]
asInstanceOf final def asInstanceOf[T0] T0 Any
atLeast def atLeast(count : Int)
expecting atLeast count calls to the mock
ReceiverClause
atMost def atMost(count : Int)
expecting at Most calls to the mock
ReceiverClause
beforeExample override def beforeExample(ex : Example)
before any example, a new context and new expectations should be created
Unit JMockerExampleLifeCycle
beforeTest def beforeTest(ex : Example) Unit ExampleLifeCycle
between def between(minCount : Int, maxCount : Int)
expecting between minCount and maxCount (inclusive) calls to the mock
ReceiverClause
capturingParam def capturingParam[T]
factory method to create a new capturing parameter
CapturingParam[T]
checkContext def checkContext
This method can be used to check the context [details]
It will always restart the context, even if there is an ExpectationError In that case, it is transformed to a failure exception
Unit JMockerContext
classToMock implicit def classToMock[T](c : Class[T])
Extends Class objects with the one-liner expects Usage: classOf[List[Int]] [details]
expect { one(_).size } { l => l.size }
ClassToMock[T]
clone protected def clone Object AnyRef
doAll def doAll(actions : Action*)
action executing several other actions
DoAllAction JMockActions
eq final def eq(arg0 : Object) Boolean AnyRef
equal def equal[T](value : T)
shortcut for expectations [details]
`with`(new IsEqual[T](value))
T
equals def equals(arg0 : Any) Boolean AnyRef
exactly def exactly(count : Int)
expecting exactly count calls to the mock
ReceiverClause
executeTest override def executeTest(ex : Example, t : => Any)
An expectation error may be thrown during the execution of a test In that case, it is transformed to a failure exception
Any JMockerExampleLifeCycle
expect def expect[T](f : (T) => Any)(implicit m : Manifest[T])
One liner function for expectations [details]

Usage: expect[List[Int]] { one(_).size } { l => l.size }

ExpectBlock[T]
expect def expect(block : => Any)
the code block being evaluated should contain some expectations being added to the expectations variable Then the expectations are "freezed" by calling the checking method on context and the actual system behaviour can be exercised
Any
finalize protected def finalize Unit AnyRef
getClass final def getClass Class[Object] AnyRef
hashCode def hashCode Int AnyRef
ignoring def ignoring[T](mockObject : T)
ignoring any calls to the mock, returning default values
T
ignoring def ignoring[T](mockObjectMatcher : Matcher[T])
ignoring any calls to the mock, returning default values
MethodClause
ignoringMatch def ignoringMatch(methodName : String)
ignoring any calls to the mock with method names like the passed parameter, returning default values
ParametersClause
ignoringMatch def ignoringMatch[T](mock : T, methodName : String)
ignoring any calls to a mock with method names like the passed parameter, returning default values
ParametersClause
inSequence def inSequence(sequence : Sequence)
adds a constraint to the expectations to declare that an expected call must happen in sequence
Unit
intToCallConstraint implicit def intToCallConstraint(i : Int)
Adds call constraint method to integers to express the exactly, atLeast, atMost
IntCallConstraint
isExpectation def isExpectation[T](a : => T)
Declares a block of code to count as an expectation
T ExpectationsListener
isInstanceOf final def isInstanceOf[T0] Boolean Any
mock def mock[T](implicit m : Manifest[T])
the mock method is used to create a mock object Usage:
val mocked = mock[InterfaceToMock]

Classes can be mocked too, but the ClassImposterizer trait has to be added to the extensions [details]
return
- s the mocked object
T
mockAs def mockAs[T](name : String)(implicit m : Manifest[T])
mocks a class and give the resulting mock a name [details]
jMock expects mocks of the same class to have different names
return
- s the mocked object
T
ne final def ne(arg0 : Object) Boolean AnyRef
never def never[T](mockObject : T)
forbidding any calls to the mock
T
notify final def notify Unit AnyRef
notifyAll final def notifyAll Unit AnyRef
onConsecutiveCalls def onConsecutiveCalls(actions : Action*)
[details]
return
- s a sequence of actions where the first action is executed after the first call, the second action after the second call, and so on
ActionSequence JMockActions
one def one[T](m : T)
expecting exactly one call to the mock
T
restart def restart
creates a new context and a new Expectations object
Unit JMockerContext
returnValue def returnValue[T](result : T)
action returning a value
ReturnValueAction JMockActions
same def same[T](value : T)
shortcut for expectations [details]
`with`(new IsSame[T](value))
T
setSequential def setSequential Unit ExampleLifeCycle
state def state(name : String)
[details]
return
- s a state with name name, creating a new one if necessary
States
synchronized final def synchronized[T0](arg0 : T0) T0 AnyRef
then def then(state : State)
specifies that after a call, a State object will be in a specific state
Unit
throwEx def throwEx[T <: Throwable](t : T)
action throwing an exception of type T
ThrowAction JMockActions
toAction implicit def toAction[T](v : T)
allow the return value of the method to be more precisely specified with a JMock action, like a returnValue action
JMockAction[T]
toString def toString String AnyRef
wait final def wait(arg0 : Long, arg1 : Int) Unit AnyRef
wait final def wait Unit AnyRef
wait final def wait(arg0 : Long) Unit AnyRef
when def when(predicate : StatePredicate)
specifies that a call can only occur following the specific condition on a state
Unit
will def will[T](m : Matcher[T])
this method is used to avoid the use of the reserved Scala keyword 'with' [details]
This is also consistent with the way to specify returned values
T
will def will(action : Action)
set up a jMock action to be executed
Unit
will implicit def will[T](m : Matcher[T])
[details]
return
- s a specs matcher adapted into Hamcrest matcher
T
with def with[T](matcher : Matcher[T])
constraining the latest call expectation with a Hamcrest matcher
T
withName def withName(nameRegex : String)
[details]
return
- s a MethodNameMatcher to use in conjunction with allowing(mock)
MethodNameMatcher HamcrestMatchers
Copyright (c) 2007-2009. All Rights Reserved.