My pages Projects Communities java.net
 


mockit
Class Expectations

java.lang.Object
  extended by mockit.Expectations

public class Expectations
extends java.lang.Object

A set of expected method/constructor invocations on one or more mock field instances.

See Also:
Expectations()

Constructor Summary
protected Expectations()
          Same as Expectations(Object, boolean) with this instance as the one with the mock fields, and without automatic endRecording.
protected Expectations(boolean autoEndRecording)
          Same as Expectations(Object, boolean) with this instance as the one with the mock fields.
protected Expectations(java.lang.Object instanceWithMockFields)
          Same as Expectations(Object, boolean) without automatic endRecording.
protected Expectations(java.lang.Object instanceWithMockFields, boolean autoEndRecording)
          Initializes this set of expectations, entering the record phase.
 
Method Summary
static void assertSatisfied()
          Same as endReplay() for the active set of expectations, if any.
 Expectations endRecording()
          Ends the recording of expected invocations.
 void endReplay()
          Ends the replay phase, verifying that all recorded invocations actually happened during test execution.
protected  void invoke(java.lang.Class<?> mockClass, java.lang.String methodName, java.lang.Object... methodArgs)
          Specifies an expected invocation to a given static method, with a given list of arguments.
protected  void invoke(java.lang.Object mock, java.lang.String methodName, java.lang.Object... methodArgs)
          Specifies an expected invocation to a given instance method, with a given list of arguments.
protected
<T,ET extends T>
void
invokeReturning(T invocationReturningValue, ET expectedReturnValue)
          Specifies in a type-safe way the return value for the last invoked method.
static void restoreFieldTypeDefinitions()
          Restores the original definitions for all the mock field types currently still redefined, if any.
protected  void returns(java.lang.Object value)
          Specifies that the last invoked method will return a given value.
protected  void setField(java.lang.Class<?> mockClass, java.lang.String fieldName, java.lang.Object fieldValue)
          Sets the value of a static field on a given class.
protected  void setField(java.lang.Object mock, java.lang.String fieldName, java.lang.Object fieldValue)
          Sets the value of a field on a given object (usually a mock).
protected  void throwsError(java.lang.Error error)
          Specifies that the last invoked method/constructor will throw an error.
protected  void throwsException(java.lang.Exception exception)
          Specifies that the last invoked method/constructor will throw an exception.
protected
<T> T
with(org.hamcrest.Matcher<T> argumentMatcher)
           
protected
<T> T
withAny(T arg)
           
protected
<T> T
withEqual(T arg)
          When called as argument for an expected method/constructor call in the recording phase, creates a new matcher that will check if the given value is equal to the corresponding call argument in the replay phase.
protected
<T> T
withInstanceOf(T object)
          Same as withEqual(Object), but checking that a call argument is an instance of the same class as the given object.
protected
<T extends java.lang.CharSequence>
T
withMatch(T regex)
           
protected
<T> T
withNotEqual(T arg)
           
protected
<T> T
withNotNull()
           
protected
<T> T
withNull()
           
protected
<T extends java.lang.CharSequence>
T
withPrefix(T text)
           
protected
<T> T
withSameInstance(T object)
           
protected
<T extends java.lang.CharSequence>
T
withSubstring(T text)
           
protected
<T extends java.lang.CharSequence>
T
withSuffix(T text)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Expectations

protected Expectations()
Same as Expectations(Object, boolean) with this instance as the one with the mock fields, and without automatic endRecording.

Expectations

protected Expectations(boolean autoEndRecording)
Same as Expectations(Object, boolean) with this instance as the one with the mock fields.

Expectations

protected Expectations(java.lang.Object instanceWithMockFields)
Same as Expectations(Object, boolean) without automatic endRecording.

Expectations

protected Expectations(java.lang.Object instanceWithMockFields,
                       boolean autoEndRecording)
Initializes this set of expectations, entering the record phase. The tasks below are performed in order.
Redefines the appropriate concrete class for each mock field. Creates and assigns a new instance for each non-final mock field. Starts recording invocations to the mock field concrete classes. Each and every such call is recorded until the record phase is explicitelly or automatically ended.
Parameters:
instanceWithMockFields - the instance which defines the mock fields to be used for this set of expectations
autoEndRecording - indicates whether an automatic endRecording() call should be made at the point of the first replay invocation after the initialization completes
Method Detail

assertSatisfied

public static void assertSatisfied()
Same as endReplay() for the active set of expectations, if any.

endRecording

public final Expectations endRecording()
Ends the recording of expected invocations. After this, the mock fields will be in the replay phase, when any invocation is checked against the previously recorded equivalent invocation.
Returns:
this same object

endReplay

public final void endReplay()
Ends the replay phase, verifying that all recorded invocations actually happened during test execution. If there is no active set of expectations, does nothing. After this, no more calls to this object or to any mock field should be made. Remember however to call restoreFieldTypeDefinitions() in your test tear down.
Throws:
java.lang.IllegalStateException - if there is an active set of expectations which however still is in the recording phase
java.lang.AssertionError - if any expected invocation for the active set of expectations didn't happen until now

invoke

protected final void invoke(java.lang.Class<?> mockClass,
                            java.lang.String methodName,
                            java.lang.Object... methodArgs)
Specifies an expected invocation to a given static method, with a given list of arguments. This is useful when the next expected method is not accessible (private, for example) from the test, and therefore can not be called normally.
Parameters:
mockClass - the class on which the invocation is to be done; must not be null
methodName - the name of the expected static method
methodArgs - zero or more non-null expected parameter values for the invocation; if a null value needs to be passed, the Class object for the parameter type must be passed instead

invoke

protected final void invoke(java.lang.Object mock,
                            java.lang.String methodName,
                            java.lang.Object... methodArgs)
Specifies an expected invocation to a given instance method, with a given list of arguments. This is useful when the next expected method is not accessible (private, for example) from the test, and therefore can not be called normally.
Parameters:
mock - the mock field instance on which the invocation is to be done; must not be null
methodName - the name of the expected method
methodArgs - zero or more non-null expected parameter values for the invocation; if a null value needs to be passed, the Class object for the parameter type must be passed instead

invokeReturning

protected final <T,ET extends T> void invokeReturning(T invocationReturningValue,
                                                      ET expectedReturnValue)
Specifies in a type-safe way the return value for the last invoked method.
Parameters:
invocationReturningValue - the value actually returned by an invocation of the desired method; the value itself is ignored, being only used to determine the method's return type
expectedReturnValue - the value to be returned when the method is replayed
Throws:
java.lang.IllegalStateException - if not currently recording invocations

restoreFieldTypeDefinitions

public static void restoreFieldTypeDefinitions()
Restores the original definitions for all the mock field types currently still redefined, if any. This is the same as calling Mockit.restoreAllOriginalDefinitions().

returns

protected final void returns(java.lang.Object value)
Specifies that the last invoked method will return a given value.
Parameters:
value - the value to be returned when the method is replayed; must be compatible with the method's return type
Throws:
java.lang.IllegalStateException - if not currently recording invocations

setField

protected final void setField(java.lang.Class<?> mockClass,
                              java.lang.String fieldName,
                              java.lang.Object fieldValue)
Sets the value of a static field on a given class. This may be useful when a mock object has a field not accessible from the test (private, for example), and there is some method under test which needs to read the correct field value.
Parameters:
mockClass - the class on which the static field is defined
fieldName - the name of the field to set
fieldValue - the value to set the field to

setField

protected final void setField(java.lang.Object mock,
                              java.lang.String fieldName,
                              java.lang.Object fieldValue)
Sets the value of a field on a given object (usually a mock). This may be useful when a mock object has a field not accessible from the test (private, for example), and there is some method under test which needs to read the correct field value.
Parameters:
mock - the instance on which to set the field value
fieldName - the name of the field to set
fieldValue - the value to set the field to

throwsError

protected final void throwsError(java.lang.Error error)
Specifies that the last invoked method/constructor will throw an error.
Parameters:
error - the error that will be thrown when the invocation is replayed
Throws:
java.lang.IllegalStateException - if not currently recording invocations

throwsException

protected final void throwsException(java.lang.Exception exception)
Specifies that the last invoked method/constructor will throw an exception.
Parameters:
exception - the exception that will be thrown when the invocation is replayed
Throws:
java.lang.IllegalStateException - if not currently recording invocations

with

protected final <T> T with(org.hamcrest.Matcher<T> argumentMatcher)

withAny

protected final <T> T withAny(T arg)

withEqual

protected final <T> T withEqual(T arg)
When called as argument for an expected method/constructor call in the recording phase, creates a new matcher that will check if the given value is equal to the corresponding call argument in the replay phase. The matcher is added to the end of the list of argument matchers for the expectation being recorded.
Parameters:
arg - the expected argument value
Returns:
the input argument

withInstanceOf

protected final <T> T withInstanceOf(T object)
Same as withEqual(Object), but checking that a call argument is an instance of the same class as the given object.

withMatch

protected final <T extends java.lang.CharSequence> T withMatch(T regex)

withNotEqual

protected final <T> T withNotEqual(T arg)

withNotNull

protected final <T> T withNotNull()

withNull

protected final <T> T withNull()

withPrefix

protected final <T extends java.lang.CharSequence> T withPrefix(T text)

withSameInstance

protected final <T> T withSameInstance(T object)

withSubstring

protected final <T extends java.lang.CharSequence> T withSubstring(T text)

withSuffix

protected final <T extends java.lang.CharSequence> T withSuffix(T text)



You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser