package easymock
- Alphabetic
- Public
- Protected
Type Members
- trait EasyMockSugar extends AnyRef
Trait that provides some basic syntax sugar for EasyMock.
Trait that provides some basic syntax sugar for EasyMock.
Using the EasyMock API directly, you create a mock with:
val mockCollaborator = createMock(classOf[Collaborator])
With this trait, you can shorten that to:
val mockCollaborator = mock[Collaborator]
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.
Value Members
- object EasyMockSugar extends EasyMockSugar
Companion object that facilitates the importing of
EasyMockSugarmembers as an alternative to mixing it in.Companion object that facilitates the importing of
EasyMockSugarmembers as an alternative to mixing it in. One use case is to importEasyMockSugarmembers so you can use them in the Scala interpreter.