package jmock
Type Members
-
trait
AsyncJMockCycleFixture extends AnyRef
Trait that will pass a new
JMockCycle
into any test that needs one.Trait that will pass a new
JMockCycle
into any test that needs one.This trait, which must be mixed into a
fixture.AsyncSuite
, defines theFixture
type to beJMockCycle
and defines awithFixture
method that instantiates a newJMockCycle
and passes it to the test function. -
final
class
JMockCycle extends AnyRef
Class that wraps and manages the lifecycle of a single
org.jmock.Mockery
context object, provides some basic syntax sugar for using JMock in Scala.Class that wraps and manages the lifecycle of a single
org.jmock.Mockery
context object, provides some basic syntax sugar for using JMock in Scala.Using the JMock API directly, you first need a
Mockery
context object:val context = new Mockery
JMockCycle
uses jMock'sClassImposterizer
to support mocking of classes, so the following line would also be needed if you wanted that functionality as well:context.setImposteriser(ClassImposteriser.INSTANCE)
When using this class, you would instead create an instance of this class (which will create and wrap a
Mockery
object) and import its members, like this:val cycle = new JMockCycle import cycle._
Using the JMock API directly, you would create a mock object like this:
val mockCollaborator = context.mock(classOf[Collaborator])
Having imported the members of an instance of this class, you can shorten that to:
val mockCollaborator = mock[Collaborator]
After creating mocks, you set expectations on them, using syntax like this:
context.checking( new Expectations() { oneOf (mockCollaborator).documentAdded("Document") exactly(3).of (mockCollaborator).documentChanged("Document") } )
Having imported the members of an instance of this class, you can shorten this step to:
expecting { e => import e._ oneOf (mockCollaborator).documentAdded("Document") exactly(3).of (mockCollaborator).documentChanged("Document") }
The
expecting
method will create a newExpectations
object, pass it into the function you provide, which sets the expectations. After the function returns, theexpecting
method will pass theExpectations
object to thechecking
method of its internalMockery
context.The
expecting
method passes an instance of classorg.scalatest.mock.JMockExpectations
to the function you pass intoexpectations
.JMockExpectations
extendsorg.jmock.Expectations
and adds several overloadedwithArg
methods. ThesewithArg
methods simply invoke correspondingwith
methods on themselves. Becausewith
is a keyword in Scala, to invoke these directly you must surround them in back ticks, like this:oneOf (mockCollaborator).documentAdded(`with`("Document"))
By importing the members of the passed
JMockExpectations
object, you can instead callwithArg
with no back ticks needed:oneOf (mockCollaborator).documentAdded(withArg("Document"))
Once you've set expectations on the mock objects, when using the JMock API directly, you use the mock, then invoke
assertIsSatisfied
on theMockery
context to make sure the mock was used in accordance with the expectations you set on it. Here's how that looks:classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) context.assertIsSatisfied()
This class enables you to use the following, more declarative syntax instead:
whenExecuting { classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) }
The
whenExecuting
method will execute the passed function, then invokeassertIsSatisfied
on its internalMockery
context object.To summarize, here's what a typical test using
JMockCycle
looks like:val cycle = new JMockCycle import cycle._
val mockCollaborator = mock[Collaborator]
expecting { e => import e._ oneOf (mockCollaborator).documentAdded("Document") exactly(3).of (mockCollaborator).documentChanged("Document") }
whenExecuting { classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) classUnderTest.addDocument("Document", new Array[Byte](0)) }ScalaTest also provides a
JMockCycleFixture
trait, which will pass a newJMockCycle
into each test that needs one. -
trait
JMockCycleFixture extends AnyRef
Trait that will pass a new
JMockCycle
into any test that needs one.Trait that will pass a new
JMockCycle
into any test that needs one.This trait, which must be mixed into a
fixture.Suite
, defines theFixture
type to beJMockCycle
and defines awithFixture
method that instantiates a newJMockCycle
and passes it to the test function. -
final
class
JMockExpectations extends jmock.Expectations
Subclass of
org.jmock.Expectations
that provideswithArg
alternatives to thewith
methods defined in its superclass.Subclass of
org.jmock.Expectations
that provideswithArg
alternatives to thewith
methods defined in its superclass.JMockCycle
'sexpecting
method of passes an instance of this class to the function passed intoexpectations
. BecauseJMockExpectations
extendsorg.jmock.Expectations
, all of theExpectations
methods are available to be invoked on instances of this class, in addition to several overloadedwithArg
methods defined in this class. ThesewithArg
methods simply invoke correspondingwith
methods onthis
. Becausewith
is a keyword in Scala, to invoke these directly you must surround them in back ticks, like this:oneOf (mockCollaborator).documentAdded(`with`("Document"))
By importing the members of the
JMockExpectations
object passed to aJMockCycle
'sexecuting
method, you can instead callwithArg
with no back ticks needed:oneOf (mockCollaborator).documentAdded(withArg("Document"))