org.unitils.mock.core
Class MockObject<T>

java.lang.Object
  extended by org.unitils.mock.core.MockObject<T>
All Implemented Interfaces:
ObjectToInjectHolder<T>, MockFactory, Mock<T>
Direct Known Subclasses:
PartialMockObject

public class MockObject<T>
extends Object
implements Mock<T>, MockFactory, ObjectToInjectHolder<T>

Implementation of a Mock.

Author:
Filip Neven, Tim Ducheyne, Kenny Claes

Field Summary
protected  BehaviorDefiningInvocations alwaysMatchingBehaviorDefiningInvocations
           
protected  Map<String,Mock<?>> chainedMocksPerName
           
protected static ThreadLocal<MatchingInvocationBuilder> matchingInvocationBuilderThreadLocal
           
protected  Class<T> mockedType
           
protected  MockProxy<T> mockProxy
           
protected  String name
           
protected  BehaviorDefiningInvocations oneTimeMatchingBehaviorDefiningInvocations
           
protected static ThreadLocal<Scenario> scenarioThreadLocal
           
 
Constructor Summary
MockObject(Class<?> mockedType, Object testObject)
          Creates a mock of the given type with un-capitalized type name + Mock as name, e.g. myServiceMock.
MockObject(String name, Class<?> mockedType, Object testObject)
          Creates a mock of the given type.
 
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.
protected  BehaviorDefiningInvocations createAlwaysMatchingBehaviorDefiningInvocations()
           
protected  MatchingInvocationHandler createAlwaysMatchingBehaviorDefiningMatchingInvocationHandler(MockBehavior mockBehavior)
           
protected  MatchingInvocationHandler createAssertInvokedInSequenceVerifyingMatchingInvocationHandler()
           
protected  MatchingInvocationHandler createAssertInvokedVerifyingMatchingInvocationHandler()
           
protected  MatchingInvocationHandler createAssertNotInvokedVerifyingMatchingInvocationHandler()
           
<M> Mock<M>
createChainedMock(String name, Class<M> mockedType)
           
protected  MatchingInvocationBuilder createMatchingInvocationBuilder()
           
protected  MockProxy<T> createMockProxy()
           
protected  BehaviorDefiningInvocations createOneTimeMatchingBehaviorDefiningInvocations()
           
protected  MatchingInvocationHandler createOneTimeMatchingBehaviorDefiningMatchingInvocationHandler(MockBehavior mockBehavior)
           
protected  Scenario createScenario(Object testObject)
           
static Scenario getCurrentScenario()
           
protected  MatchingInvocationBuilder getMatchingInvocationBuilder()
           
 T getMock()
          Returns the mock proxy instance.
 Class<?> getMockedType()
           
 String getName()
           
 T getObjectToInject()
          Returns the mock proxy instance.
 Type getObjectToInjectType(Field field)
          Gets the type of the object to inject.
protected  Scenario getScenario(Object testObject)
           
 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 the given exception 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 the given exception 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.
protected  T startMatchingInvocation(MatchingInvocationHandler matchingInvocationHandler)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

protected String name

mockedType

protected Class<T> mockedType

mockProxy

protected MockProxy<T> mockProxy

oneTimeMatchingBehaviorDefiningInvocations

protected BehaviorDefiningInvocations oneTimeMatchingBehaviorDefiningInvocations

alwaysMatchingBehaviorDefiningInvocations

protected BehaviorDefiningInvocations alwaysMatchingBehaviorDefiningInvocations

chainedMocksPerName

protected Map<String,Mock<?>> chainedMocksPerName

scenarioThreadLocal

protected static ThreadLocal<Scenario> scenarioThreadLocal

matchingInvocationBuilderThreadLocal

protected static ThreadLocal<MatchingInvocationBuilder> matchingInvocationBuilderThreadLocal
Constructor Detail

MockObject

public MockObject(Class<?> mockedType,
                  Object testObject)
Creates a mock of the given type with un-capitalized type name + Mock as name, e.g. myServiceMock. There is no .class literal for generic types. Therefore you need to pass the raw type when mocking generic types. E.g. Mock<List<String>> myMock = new MockObject("myMock", List.class, this); If the mocked type does not correspond to the declared type, a ClassCastException will occur when the mock is used.

Parameters:
mockedType - The mock type that will be proxied, use the raw type when mocking generic types, not null
testObject - The test object, not null

MockObject

public MockObject(String name,
                  Class<?> mockedType,
                  Object testObject)
Creates a mock of the given type. There is no .class literal for generic types. Therefore you need to pass the raw type when mocking generic types. E.g. Mock<List<String>> myMock = new MockObject("myMock", List.class, this); If the mocked type does not correspond to the declared type, a ClassCastException will occur when the mock is used. If no name is given the un-capitalized type name + Mock is used, e.g. myServiceMock

Parameters:
name - The name of the mock, e.g. the field-name, null for the default
mockedType - The mock type that will be proxied, use the raw type when mocking generic types, not null
testObject - The test object, not null
Method Detail

getCurrentScenario

public static Scenario getCurrentScenario()

getObjectToInject

public T getObjectToInject()
Returns the mock proxy instance. This is the object that must be injected if the field that it holds is annotated with @InjectInto or one of it's equivalents.

Specified by:
getObjectToInject in interface ObjectToInjectHolder<T>
Returns:
The mock proxy instance, not null

getObjectToInjectType

public Type getObjectToInjectType(Field field)
Description copied from interface: ObjectToInjectHolder
Gets the type of the object to inject. If this object was declared as an instance field, the field will be given as an argument. This can give more type information when generic types are being used.

Specified by:
getObjectToInjectType in interface ObjectToInjectHolder<T>
Parameters:
field - The field that declared this mock object, null if there is no field (or not known)
Returns:
The type of the object to inject (i.e. the mocked type), not null.

getMock

public T getMock()
Returns 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.

Specified by:
getMock in interface Mock<T>
Returns:
The proxy instance, not null

getMockedType

public Class<?> getMockedType()
Returns:
the type of the mock, not null

returns

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

Specified by:
returns in interface Mock<T>
Parameters:
returnValue - The value to return
Returns:
The proxy instance that will record the method call, not null

raises

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

Specified by:
raises in interface Mock<T>
Parameters:
exception - The exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

raises

public T raises(Class<? extends Throwable> exceptionClass)
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(MyException.class).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.

Specified by:
raises in interface Mock<T>
Parameters:
exceptionClass - The type of exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

performs

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

Specified by:
performs in interface Mock<T>
Parameters:
mockBehavior - The behavior to perform, not null
Returns:
The proxy instance that will record the method call, not null

onceReturns

public 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.returns("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.

Specified by:
onceReturns in interface Mock<T>
Parameters:
returnValue - The value to return
Returns:
The proxy instance that will record the method call, not null

onceRaises

public T onceRaises(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 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.

Specified by:
onceRaises in interface Mock<T>
Parameters:
exception - The exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

onceRaises

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

Specified by:
onceRaises in interface Mock<T>
Parameters:
exceptionClass - The type of exception to raise, not null
Returns:
The proxy instance that will record the method call, not null

oncePerforms

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

Specified by:
oncePerforms in interface Mock<T>
Parameters:
mockBehavior - The behavior to perform, not null
Returns:
The proxy instance that will record the method call, not null

assertInvoked

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

Specified by:
assertInvoked in interface Mock<T>
Returns:
The proxy instance that will record the method call, not null

assertInvokedInSequence

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

Specified by:
assertInvokedInSequence in interface Mock<T>
Returns:
The proxy instance that will record the method call, not null

assertNotInvoked

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

Specified by:
assertNotInvoked in interface Mock<T>
Returns:
The proxy instance that will record the method call, not null

resetBehavior

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

Specified by:
resetBehavior in interface Mock<T>

createChainedMock

public <M> Mock<M> createChainedMock(String name,
                                     Class<M> mockedType)
Specified by:
createChainedMock in interface MockFactory

getName

public String getName()

startMatchingInvocation

protected T startMatchingInvocation(MatchingInvocationHandler matchingInvocationHandler)

getMatchingInvocationBuilder

protected MatchingInvocationBuilder getMatchingInvocationBuilder()

getScenario

protected Scenario getScenario(Object testObject)

createMockProxy

protected MockProxy<T> createMockProxy()

createOneTimeMatchingBehaviorDefiningMatchingInvocationHandler

protected MatchingInvocationHandler createOneTimeMatchingBehaviorDefiningMatchingInvocationHandler(MockBehavior mockBehavior)

createAlwaysMatchingBehaviorDefiningMatchingInvocationHandler

protected MatchingInvocationHandler createAlwaysMatchingBehaviorDefiningMatchingInvocationHandler(MockBehavior mockBehavior)

createOneTimeMatchingBehaviorDefiningInvocations

protected BehaviorDefiningInvocations createOneTimeMatchingBehaviorDefiningInvocations()

createAlwaysMatchingBehaviorDefiningInvocations

protected BehaviorDefiningInvocations createAlwaysMatchingBehaviorDefiningInvocations()

createAssertInvokedVerifyingMatchingInvocationHandler

protected MatchingInvocationHandler createAssertInvokedVerifyingMatchingInvocationHandler()

createAssertInvokedInSequenceVerifyingMatchingInvocationHandler

protected MatchingInvocationHandler createAssertInvokedInSequenceVerifyingMatchingInvocationHandler()

createAssertNotInvokedVerifyingMatchingInvocationHandler

protected MatchingInvocationHandler createAssertNotInvokedVerifyingMatchingInvocationHandler()

createScenario

protected Scenario createScenario(Object testObject)

createMatchingInvocationBuilder

protected MatchingInvocationBuilder createMatchingInvocationBuilder()


Copyright © 2011. All Rights Reserved.