Packages

  • package root
    Definition Classes
    root
  • package org
    Definition Classes
    root
  • package scalatest

    ScalaTest's main traits, classes, and other members, including members supporting ScalaTest's DSL for the Scala interpreter.

    ScalaTest's main traits, classes, and other members, including members supporting ScalaTest's DSL for the Scala interpreter.

    Definition Classes
    org
  • package easymock
    Definition Classes
    scalatest
  • 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 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.

    Definition Classes
    easymock
    Annotations
    @deprecated
    Deprecated

    EasyMockSugar has been moved from org.scalatest.easymock to org.scalatestplus.easymock. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.

  • MockObjects

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 whenExecuting method.

mocks

one or more mock objects that you intend to pass to whenExecuting

Source
EasyMockSugar.scala
Exceptions thrown

IllegalArgumentException if no mocks are passed

Linear Supertypes
Serializable, Serializable, Product, Equals, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. MockObjects
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new MockObjects(mocks: AnyRef*)

    mocks

    one or more mock objects that you intend to pass to whenExecuting

    Exceptions thrown

    IllegalArgumentException if no mocks are passed

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  8. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  9. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  10. val mocks: AnyRef*
  11. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  12. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  13. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  15. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  16. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  17. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyRef

Inherited from Any

Ungrouped