org.scalatest.mock

EasyMockSugar

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 expectingclause, 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 lastCallmethod, 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 toreplay, 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.

linear super types: AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. EasyMockSugar
  2. AnyRef
  3. Any
Visibility
  1. Public
  2. All
Impl.
  1. Concrete
  2. Abstract

Type Members

  1. case class MockObjects (mocks: AnyRef*) extends Product

    Holder class for a collection of mocks that can be passed implicitly to one form of the overloaded whenExecuting method.

Value Members

  1. def != (arg0: AnyRef) : Boolean

    attributes: final
    definition classes: AnyRef
  2. def != (arg0: Any) : Boolean

    o != arg0 is the same as !(o == (arg0)).

    o != arg0 is the same as !(o == (arg0)).

    arg0

    the object to compare against this object for dis-equality.

    returns

    false if the receiver object is equivalent to the argument; true otherwise.

    attributes: final
    definition classes: Any
  3. def ## () : Int

    attributes: final
    definition classes: AnyRef → Any
  4. def $asInstanceOf [T0] () : T0

    attributes: final
    definition classes: AnyRef
  5. def $isInstanceOf [T0] () : Boolean

    attributes: final
    definition classes: AnyRef
  6. def == (arg0: AnyRef) : Boolean

    o == arg0 is the same as if (o eq null) arg0 eq null else o.equals(arg0).

    o == arg0 is the same as if (o eq null) arg0 eq null else o.equals(arg0).

    arg0

    the object to compare against this object for equality.

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    attributes: final
    definition classes: AnyRef
  7. def == (arg0: Any) : Boolean

    o == arg0 is the same as o.equals(arg0).

    o == arg0 is the same as o.equals(arg0).

    arg0

    the object to compare against this object for equality.

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    attributes: final
    definition classes: Any
  8. def asInstanceOf [T0] : T0

    This method is used to cast the receiver object to be of type T0.

    This method is used to cast the receiver object to be of type T0.

    Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression1.asInstanceOf[String] will throw a ClassCastException at runtime, while the expressionList(1).asInstanceOf[List[String]] will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested typed.

    returns

    the receiver object.

    attributes: final
    definition classes: Any
  9. implicit def call [T] (value: T) : IExpectationSetters[T]

    Implicit conversion that invokes the expect method on the EasyMock companion object (i.

    Implicit conversion that invokes the expect method on the EasyMock companion object (i.e., the static expect method in Java class org.easymock.EasyMock).

    In a ScalaTest Suite, the expect method defined in Assertions, and inherited by Suite, interferes with the expect method if imported from EasyMock. 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 call instead.

    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, not expectCall because "expect" appears in the surrounding expecting clause 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 as andReturn in this example), the Scala compiler will invoke call for 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.

    attributes: implicit
  10. def clone () : AnyRef

    This method creates and returns a copy of the receiver object.

    This method creates and returns a copy of the receiver object.

    The default implementation of the clone method is platform dependent.

    returns

    a copy of the receiver object.

    attributes: protected
    definition classes: AnyRef
  11. def eq (arg0: AnyRef) : Boolean

    This method is used to test whether the argument (arg0) is a reference to the receiver object (this).

    This method is used to test whether the argument (arg0) is a reference to the receiver object (this).

    The eq method implements an [http://en.wikipedia.org/wiki/Equivalence_relation equivalence relation] on non-null instances of AnyRef: * It is reflexive: for any non-null instance x of type AnyRef, x.eq(x) returns true. * It is symmetric: for any non-null instances x and y of type AnyRef, x.eq(y) returns true if and only if y.eq(x) returns true. * It is transitive: for any non-null instances x, y, and z of type AnyRef if x.eq(y) returns true and y.eq(z) returns true, then x.eq(z) returns true.

    Additionally, the eq method has three other properties. * It is consistent: for any non-null instances x and y of type AnyRef, multiple invocations of x.eq(y) consistently returns true or consistently returns false. * For any non-null instance x of type AnyRef, x.eq(null) and null.eq(x) returns false. * null.eq(null) returns true.

    When overriding the equals or hashCode methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).

    arg0

    the object to compare against this object for reference equality.

    returns

    true if the argument is a reference to the receiver object; false otherwise.

    attributes: final
    definition classes: AnyRef
  12. def equals (arg0: Any) : Boolean

    This method is used to compare the receiver object (this) with the argument object (arg0) for equivalence.

    This method is used to compare the receiver object (this) with the argument object (arg0) for equivalence.

    The default implementations of this method is an [http://en.wikipedia.org/wiki/Equivalence_relation equivalence relation]: * It is reflexive: for any instance x of type Any, x.equals(x) should return true. * It is symmetric: for any instances x and y of type Any, x.equals(y) should return true if and only if y.equals(x) returns true. * It is transitive: for any instances x, y, and z of type AnyRef if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

    If you override this method, you should verify that your implementation remains an equivalence relation. Additionally, when overriding this method it is often necessary to override hashCode to ensure that objects that are "equal" (o1.equals(o2) returns true) hash to the same scala.Int (o1.hashCode.equals(o2.hashCode)).

    arg0

    the object to compare against this object for equality.

    returns

    true if the receiver object is equivalent to the argument; false otherwise.

    definition classes: AnyRef → Any
  13. 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 expecting method 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 expecting clause is optional, because it does nothing besides visually indicate which statements are setting expectations on mocks. Note: this trait also provides the lastCallmethod, which just calls expectLastCall. This allows you to avoid writing "expect" twice. Also, the reason expecting doesn't take a by-name parameter, execute that, then callreplay is because you would then need to pass your mock object or objects intoexpecting. Since you already need to pass the mocks into whenExecuting so that verify can be invoked on them, it yields more concise client code to havewhenExecuting invoke replay on the mocks first rather than havingexpecting invoke replay last.

  14. def finalize () : Unit

    This method is called by the garbage collector on the receiver object when garbage collection determines that there are no more references to the object.

    This method is called by the garbage collector on the receiver object when garbage collection determines that there are no more references to the object.

    The details of when and if the finalize method are invoked, as well as the interaction between finalizeand non-local returns and exceptions, are all platform dependent.

    attributes: protected
    definition classes: AnyRef
  15. def getClass () : java.lang.Class[_]

    Returns a representation that corresponds to the dynamic class of the receiver object.

    Returns a representation that corresponds to the dynamic class of the receiver object.

    The nature of the representation is platform dependent.

    returns

    a representation that corresponds to the dynamic class of the receiver object.

    attributes: final
    definition classes: AnyRef
  16. def hashCode () : Int

    Returns a hash code value for the object.

    Returns a hash code value for the object.

    The default hashing algorithm is platform dependent.

    Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

    returns

    the hash code value for the object.

    definition classes: AnyRef → Any
  17. def isInstanceOf [T0] : Boolean

    This method is used to test whether the dynamic type of the receiver object is T0.

    This method is used to test whether the dynamic type of the receiver object is T0.

    Note that the test result of the test is modulo Scala's erasure semantics. Therefore the expression1.isInstanceOf[String] will return false, while the expression List(1).isInstanceOf[List[String]] will return true. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested typed.

    returns

    true if the receiver object is an instance of erasure of type T0; false otherwise.

    attributes: final
    definition classes: Any
  18. def lastCall [T] : IExpectationSetters[T]

    Invokes the expectLastCall method on the EasyMock companion object (i.

    Invokes the expectLastCall method on the EasyMock companion object (i.e., the static expect method in Java class org.easymock.EasyMock).

    This method is provided simply to allow you to avoid repeating "expect" inside anexpecting clause. Here's an example that uses the expectLastCall directly to express the expectation that the getName method 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)
    }

  19. def mock [T <: AnyRef] (implicit manifest: Manifest[T]) : T

    Invokes the createMock method on the EasyMock companion object (i.

    Invokes the createMock method on the EasyMock companion object (i.e., the static createMock method in Java class org.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]

  20. def ne (arg0: AnyRef) : Boolean

    o.ne(arg0) is the same as !(o.eq(arg0)).

    o.ne(arg0) is the same as !(o.eq(arg0)).

    arg0

    the object to compare against this object for reference dis-equality.

    returns

    false if the argument is not a reference to the receiver object; true otherwise.

    attributes: final
    definition classes: AnyRef
  21. def niceMock [T <: AnyRef] (implicit manifest: Manifest[T]) : T

    Invokes the createNiceMock method on the EasyMock companion object (i.

    Invokes the createNiceMock method on the EasyMock companion object (i.e., the static createNiceMock method in Java class org.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]

  22. def notify () : Unit

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Wakes up a single thread that is waiting on the receiver object's monitor.

    attributes: final
    definition classes: AnyRef
  23. def notifyAll () : Unit

    Wakes up all threads that are waiting on the receiver object's monitor.

    Wakes up all threads that are waiting on the receiver object's monitor.

    attributes: final
    definition classes: AnyRef
  24. def strictMock [T <: AnyRef] (implicit manifest: Manifest[T]) : T

    Invokes the createStrictMock method on the EasyMock companion object (i.

    Invokes the createStrictMock method on the EasyMock companion object (i.e., the static createStrictMock method in Java class org.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]

  25. def synchronized [T0] (arg0: T0) : T0

    attributes: final
    definition classes: AnyRef
  26. def toString () : String

    Returns a string representation of the object.

    Returns a string representation of the object.

    The default representation is platform dependent.

    returns

    a string representation of the object.

    definition classes: AnyRef → Any
  27. def wait () : Unit

    attributes: final
    definition classes: AnyRef
  28. def wait (arg0: Long, arg1: Int) : Unit

    attributes: final
    definition classes: AnyRef
  29. def wait (arg0: Long) : Unit

    attributes: final
    definition classes: AnyRef
  30. def whenExecuting (fun: ⇒ Unit)(implicit mocks: MockObjects) : Unit

    Invokes replay on the mock object or objects passed via an implicit parameter, executes the passed function, then invokes verify on the passed mock object or objects.

    Invokes replay on the mock object or objects passed via an implicit parameter, executes the passed function, then invokes verify on the passed mock object or objects.

    Once you've set expectations on some mock objects, you must invoke replay on the mocks to indicate you are done setting expectations, and will start using the mocks. After using the mocks, you must invoke verify to 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 whenExecuting method will first invoke EasyMock.replyonce for each mock you supplied, execute the passed function, then invoke EasyMock.verify once for each mock you supplied. If an exception is thrown by the passed function, whenExecuting will complete abruptly with that same exception without executing verify on any of the mocks.

  31. def whenExecuting (mocks: AnyRef*)(fun: ⇒ Unit) : Unit

    Invokes replay on the passed mock object or objects, executes the passed function, then invokesverify on the passed mock object or objects.

    Invokes replay on the passed mock object or objects, executes the passed function, then invokesverify on the passed mock object or objects.

    Once you've set expectations on some mock objects, you must invoke replay on the mocks to indicate you are done setting expectations, and will start using the mocks. After using the mocks, you must invoke verify to 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 whenExecuting method will first invoke EasyMock.replyonce for each mock you supplied, execute the passed function, then invoke EasyMock.verify once for each mock you supplied. If an exception is thrown by the passed function, whenExecuting will complete abruptly with that same exception without executing verify on any of the mocks.

    mocks

    one or more mock objects to invoke replay before using and verify after using.

Inherited from AnyRef

Inherited from Any