org.scalatest.mock

JMockCycle

class JMockCycle extends AnyRef

Class that wraps and manages the lifecycle of a single org.jmock.Mockery context object, provides some basic syntax sugar for using JMockin 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 expectingmethod will pass the Expectations object to the checkingmethod of its internal Mockery context.

The expecting method passes an instance of classorg.scalatest.mock.JMockExpectations to the function you pass intoexpectations. 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 invokeassertIsSatisfied 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 Mockerycontext 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.

attributes: final
    authors:
  1. Bill Venners

Inherited
  1. Hide All
  2. Show all
  1. AnyRef
  2. Any
Visibility
  1. Public
  2. All

Instance constructors

  1. new JMockCycle()

Value Members

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

  2. def !=(arg0: Any): Boolean

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

  3. def ##(): Int

  4. def $asInstanceOf[T0](): T0

  5. def $isInstanceOf[T0](): Boolean

  6. def ==(arg0: AnyRef): Boolean

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

  7. def ==(arg0: Any): Boolean

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

  8. def asInstanceOf[T0]: T0

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

  9. def clone(): AnyRef

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

  10. def eq(arg0: AnyRef): Boolean

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

  11. def equals(arg0: Any): Boolean

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

  12. def expecting(fun: (JMockExpectations) ⇒ Unit): Unit

    Sets expectations on mock objects.

  13. 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.

  14. def getClass(): java.lang.Class[_ <: java.lang.Object]

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

  15. def hashCode(): Int

    Returns a hash code value for the object.

  16. def isInstanceOf[T0]: Boolean

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

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

    Invokes the mock method on this JMockCycle's internalMockery context object, passing in a class instance for the specified type parameter.

  18. def ne(arg0: AnyRef): Boolean

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

  19. def notify(): Unit

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

  20. def notifyAll(): Unit

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

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

  22. def toString(): String

    Returns a string representation of the object.

  23. def wait(): Unit

  24. def wait(arg0: Long, arg1: Int): Unit

  25. def wait(arg0: Long): Unit

  26. def whenExecuting(fun: ⇒ Unit): Unit

    Executes code using mocks with expectations set.