Trait that provides some basic syntax sugar for EasyMock.
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's ClassImposterizer 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 new Expectations object, pass it into
the function you provide, which sets the expectations. After the function returns, the expecting
method will pass the Expectations object to the checking
method of its internal Mockery context.
The expecting method passes an instance of class
org.scalatest.mock.JMockExpectations to the function you pass into
expectations. JMockExpectations extends org.jmock.Expectations and
adds several overloaded withArg methods. These withArg methods simply
invoke corresponding with methods on themselves. Because with 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 call withArg 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 the Mockery 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
invoke assertIsSatisfied on its internal Mockery
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 new JMockCycle into each test that needs one.
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 the
Fixture type to be JMockCycle and defines a
withFixture method that instantiates a new JMockCycle
and passes it to the test function.
Subclass of org.jmock.Expectations that provides withArg
alternatives to the with methods defined in its superclass.
Subclass of org.jmock.Expectations that provides withArg
alternatives to the with methods defined in its superclass.
JMockCycle's expecting method of passes an instance of this class
to the function passed into expectations. Because JMockExpectations
extends org.jmock.Expectations, all of the Expectations methods are
available to be invoked on instances of this class, in addition to
several overloaded withArg methods defined in this class. These withArg methods simply
invoke corresponding with methods on this. Because with 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
a JMockCycle's executing method, you can
instead call withArg with no back ticks needed:
oneOf (mockCollaborator).documentAdded(withArg("Document"))
Trait that provides some basic syntax sugar for Mockito.
Trait that provides some basic syntax sugar for Mockito.
Using the Mockito API directly, you create a mock with:
val mockCollaborator = mock(classOf[Collaborator])
Using this trait, you can shorten that to:
val mockCollaborator = mock[Collaborator]
This trait also provides shorthands for the three other (non-deprecated) overloaded mock methods,
which allow you to pass in a default answer, a name, or settings.
Companion object that facilitates the importing of EasyMockSugar members as
an alternative to mixing it in.
Companion object that facilitates the importing of EasyMockSugar members as
an alternative to mixing it in. One use case is to import EasyMockSugar members so you can use
them in the Scala interpreter.
Companion object that facilitates the importing of MockitoSugar members as
an alternative to mixing it in.
Companion object that facilitates the importing of MockitoSugar members as
an alternative to mixing it in. One use case is to import MockitoSugar members so you can use
them in the Scala interpreter.
Trait that provides some basic syntax sugar for EasyMock.
Using the EasyMock API directly, you create a mock with:
With this trait, you can shorten that to:
After creating mocks, you set expectations on them, using syntax like this:
mockCollaborator.documentAdded("Document") mockCollaborator.documentChanged("Document") expectLastCall().times(3)If you wish to highlight which statements are setting expectations on the mock (versus which ones are actually using the mock), you can place them in an
expectingclause, provided by this trait, like this:expecting { mockCollaborator.documentAdded("Document") mockCollaborator.documentChanged("Document") lastCall.times(3) }Using an
expectingclause is optional, because it does nothing but visually indicate which statements are setting expectations on mocks. (Note: this trait also provides thelastCallmethod, which just callsexpectLastCall.)Once you've set expectations on the mock objects, you must invoke
replayon the mocks to indicate you are done setting expectations, and will start using the mock. After using the mock, you must invokeverifyto check to make sure the mock was used in accordance with the expectations you set on it. Here's how that looks when you use the EasyMock API directly:replay(mockCollaborator) 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)) verify(mockCollaborator)This trait enables you to use the following, more declarative syntax instead:
whenExecuting(mockCollaborator) { 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 pass themockCollaboratortoreplay, execute the passed function (your code that uses the mock), and callverify, passing in themockCollaborator. If you want to use multiple mocks, you can pass multiple mocks towhenExecuting.To summarize, here's what a typical test using
EasyMockSugarlooks like:val mockCollaborator = mock[Collaborator] expecting { mockCollaborator.documentAdded("Document") mockCollaborator.documentChanged("Document") lastCall.times(3) } whenExecuting(mockCollaborator) { 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)) }An alternative approach is to place your mock objects in a
MockObjectsholder object referenced from an implicitval, then use the overloaded variant ofwhenExecutingthat takes an implicitMockObjectsparameter. Here's how that would look:implicit val mocks = MockObjects(mock[Collaborator]) expecting { mockCollaborator.documentAdded("Document") mockCollaborator.documentChanged("Document") lastCall.times(3) } 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)) }Note: As of ScalaTest 1.3, this trait supports EasyMock 3, with no dependencies on EasyMock class extension.