package jmock
Type Members
- trait AsyncJMockCycleFixture extends AnyRef
Trait that will pass a new
JMockCycleinto any test that needs one.Trait that will pass a new
JMockCycleinto any test that needs one.This trait, which must be mixed into a
fixture.AsyncSuite, defines theFixturetype to beJMockCycleand defines awithFixturemethod that instantiates a newJMockCycleand passes it to the test function. - final class JMockCycle extends AnyRef
Class that wraps and manages the lifecycle of a single
org.jmock.Mockerycontext object, provides some basic syntax sugar for using JMock in Scala.Class that wraps and manages the lifecycle of a single
org.jmock.Mockerycontext object, provides some basic syntax sugar for using JMock in Scala.Using the JMock API directly, you first need a
Mockerycontext object:val context = new Mockery
JMockCycleuses jMock'sClassImposterizerto 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
Mockeryobject) 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
expectingmethod will create a newExpectationsobject, pass it into the function you provide, which sets the expectations. After the function returns, theexpectingmethod will pass theExpectationsobject to thecheckingmethod of its internalMockerycontext.The
expectingmethod passes an instance of classorg.scalatest.mock.JMockExpectationsto the function you pass intoexpectations.JMockExpectationsextendsorg.jmock.Expectationsand adds several overloadedwithArgmethods. ThesewithArgmethods simply invoke correspondingwithmethods on themselves. Becausewithis 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
JMockExpectationsobject, you can instead callwithArgwith 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
assertIsSatisfiedon theMockerycontext 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
whenExecutingmethod will execute the passed function, then invokeassertIsSatisfiedon its internalMockerycontext object.To summarize, here's what a typical test using
JMockCyclelooks 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
JMockCycleFixturetrait, which will pass a newJMockCycleinto each test that needs one. - trait JMockCycleFixture extends AnyRef
Trait that will pass a new
JMockCycleinto any test that needs one.Trait that will pass a new
JMockCycleinto any test that needs one.This trait, which must be mixed into a
fixture.Suite, defines theFixturetype to beJMockCycleand defines awithFixturemethod that instantiates a newJMockCycleand passes it to the test function. - final class JMockExpectations extends Expectations
Subclass of
org.jmock.Expectationsthat provideswithArgalternatives to thewithmethods defined in its superclass.Subclass of
org.jmock.Expectationsthat provideswithArgalternatives to thewithmethods defined in its superclass.JMockCycle'sexpectingmethod of passes an instance of this class to the function passed intoexpectations. BecauseJMockExpectationsextendsorg.jmock.Expectations, all of theExpectationsmethods are available to be invoked on instances of this class, in addition to several overloadedwithArgmethods defined in this class. ThesewithArgmethods simply invoke correspondingwithmethods onthis. Becausewithis 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
JMockExpectationsobject passed to aJMockCycle'sexecutingmethod, you can instead callwithArgwith no back ticks needed:oneOf (mockCollaborator).documentAdded(withArg("Document"))