org.unitils.mock
Interface Mock<T>

All Known Subinterfaces:
PartialMock<T>
All Known Implementing Classes:
MockObject, PartialMockObject

public interface Mock<T>

Declares the contract for a controller object that enables defining the behavior of methods of a mock object, or for performing assert statements that verify that certain calls were effectively made. A method is also defined that provides access to the actual mock object.

If Unitils encounters a field declared as Mock, a MockObject is automatically instantiated and assigned to the declared field.


Method Summary
 T assertInvoked()
          Asserts that an invocation that matches the invocation following this call has been observed on this mock object during this test.
 T assertInvokedInSequence()
          Asserts that an invocation that matches the invocation following this call has been observed on this mock object during this test.
 T assertNotInvoked()
          Asserts that no invocation that matches the invocation following this call has been observed on this mock object during this test.
 T getMock()
          Gets the mock proxy instance.
 T oncePerforms(MockBehavior mockBehavior)
          Defines behavior for this mock so that will be performed when the invocation following this call matches the observed behavior.
 T onceRaises(Class<? extends Throwable> exceptionClass)
          Defines behavior for this mock so that it raises an instance of the given exception class when the invocation following this call matches the observed behavior.
 T onceRaises(Throwable exception)
          Defines behavior for this mock so that it raises an instance of the given exception class when the invocation following this call matches the observed behavior.
 T onceReturns(Object returnValue)
          Defines behavior for this mock so that it will return the given value when the invocation following this call matches the observed behavior.
 T performs(MockBehavior mockBehavior)
          Defines behavior for this mock so that will be performed when the invocation following this call matches the observed behavior.
 T raises(Class<? extends Throwable> exceptionClass)
          Defines behavior for this mock so that it raises an instance of the given exception class when the invocation following this call matches the observed behavior.
 T raises(Throwable exception)
          Defines behavior for this mock so that it raises the given exception when the invocation following this call matches the observed behavior.
 void resetBehavior()
          Removes all behavior defined for this mock.
 T returns(Object returnValue)
          Defines behavior for this mock so that it will return the given value when the invocation following this call matches the observed behavior.
 

Method Detail

getMock

T getMock()
Gets the mock proxy instance. This is the instance that can be used to perform the test. You could for example inject it in the tested object. It will then perform the defined behavior and record all observed method invocations so that assertions can be performed afterwards.

Returns:
The proxy instance, not null

returns

T returns(Object returnValue)
Defines behavior for this mock so that it will return the given value when the invocation following this call matches the observed behavior. E.g.

mock.returns("aValue").method1();

will return "aValue" when method1 is called.

Note that this behavior is executed each time a match is found. So "aValue" will be returned each time method1() is called. If you only want to return the value once, use the onceReturns(java.lang.Object) method.

Parameters:
returnValue - The value to return
Returns:
The proxy instance that will record the method call, not null

raises

T raises(Throwable exception)
Defines behavior for this mock so that it raises the given exception when the invocation following this call matches the observed behavior. E.g.

mock.raises(new MyException()).method1();

will throw the given exception when method1 is called.

Note that this behavior is executed each time a match is found. So the exception will be raised each time method1() is called. If you only want to raise the exception once, use the onceRaises(java.lang.Throwable) method.

Parameters:
exception - The exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

raises

T raises(Class<? extends Throwable> exceptionClass)
Defines behavior for this mock so that it raises an instance of the given exception class when the invocation following this call matches the observed behavior. E.g.

mock.raises(new MyException()).method1();

will throw an instance of the given exception class when method1 is called.

Note that this behavior is executed each time a match is found. So the exception will be raised each time method1() is called. If you only want to raise the exception once, use the onceRaises(java.lang.Throwable) method.

Parameters:
exceptionClass - The class of the exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

performs

T performs(MockBehavior mockBehavior)
Defines behavior for this mock so that will be performed when the invocation following this call matches the observed behavior. E.g.

mock.performs(new MyMockBehavior()).method1();

will execute the given mock behavior when method1 is called.

Note that this behavior is executed each time a match is found. So the behavior will be executed each time method1() is called. If you only want to execute the behavior once, use the oncePerforms(org.unitils.mock.mockbehavior.MockBehavior) method.

Parameters:
mockBehavior - The behavior to perform, not null
Returns:
The proxy instance that will record the method call, not null

onceReturns

T onceReturns(Object returnValue)
Defines behavior for this mock so that it will return the given value when the invocation following this call matches the observed behavior. E.g.

mock.onceReturns("aValue").method1();

will return "aValue" when method1 is called.

Note that this behavior is executed only once. If method1() is invoked a second time, a different behavior definition will be used (if defined) or a default value will be returned. If you want this definition to be able to be matched multiple times, use the method returns(java.lang.Object) instead.

Parameters:
returnValue - The value to return
Returns:
The proxy instance that will record the method call, not null

onceRaises

T onceRaises(Throwable exception)
Defines behavior for this mock so that it raises an instance of the given exception class when the invocation following this call matches the observed behavior. E.g.

mock.raises(new MyException()).method1();

will throw an instance of the given exception class when method1 is called.

Note that this behavior is executed only once. If method1() is invoked a second time, a different behavior definition will be used (if defined) or a default value will be returned. If you want this definition to be able to be matched multiple times, use the method raises(java.lang.Throwable) instead.

Parameters:
exception - The exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

onceRaises

T onceRaises(Class<? extends Throwable> exceptionClass)
Defines behavior for this mock so that it raises an instance of the given exception class when the invocation following this call matches the observed behavior. E.g.

mock.raises(new MyException()).method1();

will throw an instance of the given exception class when method1 is called.

Note that this behavior is executed only once. If method1() is invoked a second time, a different behavior definition will be used (if defined) or a default value will be returned. If you want this definition to be able to be matched multiple times, use the method raises(java.lang.Throwable) instead.

Parameters:
exceptionClass - The class of the exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

oncePerforms

T oncePerforms(MockBehavior mockBehavior)
Defines behavior for this mock so that will be performed when the invocation following this call matches the observed behavior. E.g.

mock.performs(new MyMockBehavior()).method1();

will execute the given mock behavior when method1 is called.

Note that this behavior is executed only once. If method1() is invoked a second time, a different behavior definition will be used (if defined) or a default value will be returned. If you want this definition to be able to be matched multiple times, use the method performs(org.unitils.mock.mockbehavior.MockBehavior) instead.

Parameters:
mockBehavior - The behavior to perform, not null
Returns:
The proxy instance that will record the method call, not null

assertInvoked

T assertInvoked()
Asserts that an invocation that matches the invocation following this call has been observed on this mock object during this test.

Returns:
The proxy instance that will record the method call, not null

assertInvokedInSequence

T assertInvokedInSequence()
Asserts that an invocation that matches the invocation following this call has been observed on this mock object during this test.

If this method is used multiple times during the current test, the sequence of the observed method calls has to be the same as the sequence of the calls to this method.

Returns:
The proxy instance that will record the method call, not null

assertNotInvoked

T assertNotInvoked()
Asserts that no invocation that matches the invocation following this call has been observed on this mock object during this test.

Returns:
The proxy instance that will record the method call, not null

resetBehavior

void resetBehavior()
Removes all behavior defined for this mock. This will only remove the behavior, not the observed invocations for this mock.



Copyright © 2011. All Rights Reserved.