o != arg0
is the same as !(o == (arg0))
.
o != arg0
is the same as !(o == (arg0))
.
the object to compare against this object for dis-equality.
false
if the receiver object is equivalent to the argument; true
otherwise.
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)
.
the object to compare against this object for equality.
true
if the receiver object is equivalent to the argument; false
otherwise.
o == arg0
is the same as o.equals(arg0)
.
o == arg0
is the same as o.equals(arg0)
.
the object to compare against this object for equality.
true
if the receiver object is equivalent to the argument; false
otherwise.
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.
the receiver object.
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.
a copy of the receiver object.
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
).
the object to compare against this object for reference equality.
true
if the argument is a reference to the receiver object; false
otherwise.
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)
).
the object to compare against this object for equality.
true
if the receiver object is equivalent to the argument; false
otherwise.
Sets expectations on mock objects.
Sets expectations on mock objects.
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 expecting
method will pass the Expectations
object to the checking
method of its internal Mockery
context.
This method passes an instance of class org.scalatest.mock.JMockExpectations
to the
passed function. 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"))
a function that sets expectations on the passed JMockExpectations
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.
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 finalize
and non-local returns and exceptions, are all platform dependent.
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.
a representation that corresponds to the dynamic class of the receiver object.
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.
the hash code value for the object.
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.
true
if the receiver object is an instance of erasure of type T0
; false
otherwise.
Invokes the mock
method on this JMockCycle
's internalMockery
context object, passing in a class instance for the
specified type parameter.
Invokes the mock
method on this JMockCycle
's internalMockery
context object, passing in a class instance for the
specified type parameter.
Using the JMock API directly, you create a mock with:
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]
o.ne(arg0)
is the same as !(o.eq(arg0))
.
o.ne(arg0)
is the same as !(o.eq(arg0))
.
the object to compare against this object for reference dis-equality.
false
if the argument is not a reference to the receiver object; true
otherwise.
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.
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.
Returns a string representation of the object.
Returns a string representation of the object.
The default representation is platform dependent.
a string representation of the object.
Executes code using mocks with expectations set.
Executes code using mocks with expectations set.
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 Mockery
context object.
the code to execute under previously set expectations
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: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:
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:Using the JMock API directly, you would create a mock object like this:
Having imported the members of an instance of this class, you can shorten that to:
After creating mocks, you set expectations on them, using syntax like this:
Having imported the members of an instance of this class, you can shorten this step to:
The
expecting
method will create a newExpectations
object, pass it into the function you provide, which sets the expectations. After the function returns, theexpecting
method will pass theExpectations
object to thechecking
method of its internalMockery
context.The
expecting
method passes an instance of classorg.scalatest.mock.JMockExpectations
to the function you pass intoexpectations
.JMockExpectations
extendsorg.jmock.Expectations
and adds several overloadedwithArg
methods. ThesewithArg
methods simply invoke correspondingwith
methods on themselves. Becausewith
is a keyword in Scala, to invoke these directly you must surround them in back ticks, like this:By importing the members of the passed
JMockExpectations
object, you can instead callwithArg
with no back ticks needed:Once you've set expectations on the mock objects, when using the JMock API directly, you use the mock, then invoke
assertIsSatisfied
on theMockery
context to make sure the mock was used in accordance with the expectations you set on it. Here's how that looks:This class enables you to use the following, more declarative syntax instead:
The
whenExecuting
method will execute the passed function, then invokeassertIsSatisfied
on its internalMockery
context object.To summarize, here's what a typical test using
JMockCycle
looks like:ScalaTest also provides a
JMockCycleFixture
trait, which will pass a newJMockCycle
into each test that needs one.authors:
Bill Venners