trait EasyMockSugar extends AnyRef
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 expecting
clause, provided by this trait, like this:
expecting {
mockCollaborator.documentAdded("Document")
mockCollaborator.documentChanged("Document")
lastCall.times(3)
}
Using an expecting clause is optional, because it does nothing but visually indicate
which statements are setting expectations on mocks. (Note: this trait also provides the lastCall
method, which just calls expectLastCall.)
Once you've set expectations on the mock objects, you must invoke replay on
the mocks to indicate you are done setting expectations, and will start using the mock.
After using the mock, you must invoke verify to 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 whenExecuting method will pass the mockCollaborator to
replay, execute the passed function (your code that uses the mock), and
call verify, passing in the mockCollaborator. If you want to
use multiple mocks, you can pass multiple mocks to whenExecuting.
To summarize, here's what a typical test using EasyMockSugar looks 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 MockObjects holder object referenced
from an implicit val, then use the overloaded variant of whenExecuting that
takes an implicit MockObjects parameter. 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.
- Source
- EasyMockSugar.scala
- Alphabetic
- By Inheritance
- EasyMockSugar
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- case class MockObjects(mocks: AnyRef*) extends Product with Serializable
Holder class for a collection of mocks that can be passed implicitly to one form of the overloaded
whenExecutingmethod.Holder class for a collection of mocks that can be passed implicitly to one form of the overloaded
whenExecutingmethod.- mocks
one or more mock objects that you intend to pass to
whenExecuting
- Exceptions thrown
IllegalArgumentExceptionif no mocks are passed
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- implicit def call[T](value: T): IExpectationSetters[T]
Implicit conversion that invokes the
expectmethod on theEasyMockcompanion object (i.e., the staticexpectmethod in Java classorg.easymock.EasyMock).Implicit conversion that invokes the
expectmethod on theEasyMockcompanion object (i.e., the staticexpectmethod in Java classorg.easymock.EasyMock).In a ScalaTest
Suite, theexpectmethod defined inAssertions, and inherited bySuite, interferes with theexpectmethod if imported fromEasyMock. You can invoke it by qualifying it, i.e.,EasyMock.expect, or by changing its name on import, like this:import org.easymock.EasyMock.{expect => easyMockExpect, _}But if you mix in this trait, you can just invoke
callinstead.You can use this method, for example, to chain expectations like this:
expecting { call(mock.getName).andReturn("Ben Franklin") }Note: the name of this methods is
call, notexpectCallbecause "expect" appears in the surroundingexpectingclause provided by this trait.Moreover, because this method is marked
implicit, you will usually be able to simply leave it off. So long as the result of the method call you are expecting doesn't have a method that satisfies the subsequent invocation (such asandReturnin this example), the Scala compiler will invokecallfor you implicitly. Here's how that looks:expecting { mock.getName.andReturn("Ben Franklin") }- value
- the result of invoking a method on mock prior to invoking
replay.
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def expecting(unused: Any): Unit
Provides a visual clue to readers of the code that a set of statements are expectations being set on mocks.
Provides a visual clue to readers of the code that a set of statements are expectations being set on mocks.
Using the EasyMock API directly, you set expectations on a mock object with syntax like:
mockCollaborator.documentAdded("Document") mockCollaborator.documentChanged("Document") expectLastCall().times(3)
This
expectingmethod can make it more obvious which portion of your test code is devoted to setting expectations on mock objects. For example:expecting { mockCollaborator.documentAdded("Document") mockCollaborator.documentChanged("Document") lastCall.times(3) }Using an
expectingclause is optional, because it does nothing besides visually indicate which statements are setting expectations on mocks. Note: this trait also provides thelastCallmethod, which just callsexpectLastCall. This allows you to avoid writing "expect" twice. Also, the reasonexpectingdoesn't take a by-name parameter, execute that, then callreplayis because you would then need to pass your mock object or objects intoexpecting. Since you already need to pass the mocks intowhenExecutingso thatverifycan be invoked on them, it yields more concise client code to havewhenExecutinginvokereplayon the mocks first rather than havingexpectinginvokereplaylast. - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def lastCall[T]: IExpectationSetters[T]
Invokes the
expectLastCallmethod on theEasyMockcompanion object (i.e., the staticexpectmethod in Java classorg.easymock.EasyMock).Invokes the
expectLastCallmethod on theEasyMockcompanion object (i.e., the staticexpectmethod in Java classorg.easymock.EasyMock).This method is provided simply to allow you to avoid repeating "expect" inside an
expectingclause. Here's an example that uses theexpectLastCalldirectly to express the expectation that thegetNamemethod will be invoked three times on a mock, each time returning"Ben Franklin":expecting { mock.getName.andReturn("Ben Franklin") expectLastCall.times(3) }Using this method, you can compress this to:
expecting { mock.getName.andReturn("Ben Franklin") lastCall.times(3) } - def mock[T <: AnyRef](implicit classTag: ClassTag[T]): T
Invokes the
createMockmethod on theEasyMockcompanion object (i.e., the staticcreateMockmethod in Java classorg.easymock.classextension.EasyMock).Invokes the
createMockmethod on theEasyMockcompanion object (i.e., the staticcreateMockmethod in Java classorg.easymock.classextension.EasyMock).Using the EasyMock API directly, you create a mock with:
val mockCollaborator = createMock(classOf[Collaborator])
Using this method, you can shorten that to:
val mockCollaborator = mock[Collaborator]
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def niceMock[T <: AnyRef](implicit classTag: ClassTag[T]): T
Invokes the
createNiceMockmethod on theEasyMockcompanion object (i.e., the staticcreateNiceMockmethod in Java classorg.easymock.classextension.EasyMock).Invokes the
createNiceMockmethod on theEasyMockcompanion object (i.e., the staticcreateNiceMockmethod in Java classorg.easymock.classextension.EasyMock).Using the EasyMock API directly, you create a nice mock with:
val mockCollaborator = createNiceMock(classOf[Collaborator])
Using this trait, you can shorten that to:
val mockCollaborator = niceMock[Collaborator]
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def strictMock[T <: AnyRef](implicit classTag: ClassTag[T]): T
Invokes the
createStrictMockmethod on theEasyMockcompanion object (i.e., the staticcreateStrictMockmethod in Java classorg.easymock.classextension.EasyMock).Invokes the
createStrictMockmethod on theEasyMockcompanion object (i.e., the staticcreateStrictMockmethod in Java classorg.easymock.classextension.EasyMock).Using the EasyMock API directly, you create a strict mock with:
val mockCollaborator = createStrictMock(classOf[Collaborator])
Using this trait, you can shorten that to:
val mockCollaborator = strictMock[Collaborator]
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- def whenExecuting(fun: => Unit)(implicit mocks: MockObjects): Unit
Invokes
replayon the mock object or objects passed via an implicit parameter, executes the passed function, then invokesverifyon the passed mock object or objects.Invokes
replayon the mock object or objects passed via an implicit parameter, executes the passed function, then invokesverifyon the passed mock object or objects.Once you've set expectations on some mock objects, you must invoke
replayon the mocks to indicate you are done setting expectations, and will start using the mocks. After using the mocks, you must invokeverifyto check to make sure the mocks were used in accordance with the expectations you set on it. Here's how that looks when you use the EasyMock API directly:replay(mock) 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(mock)
This method enables you to use the following, more declarative syntax instead:
implicit val mocks = MockObjects(mockCollaborator)
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)) }If you are working with multiple mock objects at once, you simply pass them all to
MockObjects, like this:implicit val mocks = MockObjects(mock1, mock2, mock3)
The
whenExecutingmethod will first invokeEasyMock.replyonce for each mock you supplied, execute the passed function, then invokeEasyMock.verifyonce for each mock you supplied. If an exception is thrown by the passed function,whenExecutingwill complete abruptly with that same exception without executing verify on any of the mocks. - def whenExecuting(mocks: AnyRef*)(fun: => Unit): Unit
Invokes
replayon the passed mock object or objects, executes the passed function, then invokesverifyon the passed mock object or objects.Invokes
replayon the passed mock object or objects, executes the passed function, then invokesverifyon the passed mock object or objects.Once you've set expectations on some mock objects, you must invoke
replayon the mocks to indicate you are done setting expectations, and will start using the mocks. After using the mocks, you must invokeverifyto check to make sure the mocks were used in accordance with the expectations you set on it. Here's how that looks when you use the EasyMock API directly:replay(mock) 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(mock)
This method 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)) }If you are working with multiple mock objects at once, you simply pass them all to
whenExecuting, like this:whenExecuting(mock1, mock2, mock3) { // ... }The
whenExecutingmethod will first invokeEasyMock.replyonce for each mock you supplied, execute the passed function, then invokeEasyMock.verifyonce for each mock you supplied. If an exception is thrown by the passed function,whenExecutingwill complete abruptly with that same exception without executing verify on any of the mocks.- mocks
one or more mock objects to invoke
replaybefore using andverifyafter using.
- Exceptions thrown
IllegalArgumentExceptionif no mocks are passed
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated @Deprecated
- Deprecated