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

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

public class PartialMockObject<T>
extends MockObject<T>
implements PartialMock<T>

Implementation of a PartialMock. For a partial mock, if a method is called that is not mocked, the original behavior will be called.

Author:
Filip Neven, Tim Ducheyne, Kenny Claes

Field Summary
 
Fields inherited from class org.unitils.mock.core.MockObject
alwaysMatchingBehaviorDefiningInvocations, chainedMocksPerName, matchingInvocationBuilderThreadLocal, mockedType, mockProxy, name, oneTimeMatchingBehaviorDefiningInvocations, scenarioThreadLocal
 
Constructor Summary
PartialMockObject(Class<?> mockedType, Object testObject)
          Creates a mock of the given type with un-capitalized type name + Mock as name, e.g. myServiceMock.
PartialMockObject(Object mockPrototype, Object testObject)
          Creates a mock of the same type as the given mock prototype with un-capitalized type name + Mock as name, e.g. myServiceMock.
PartialMockObject(String name, Class<?> mockedType, Object testObject)
          Creates a mock of the given type for the given scenario.
PartialMockObject(String name, Object mockedInstance, Object testObject)
          Creates a mock of the same type as the given mock prototype with the given name.
 
Method Summary
protected  MockProxy<T> createMockProxy()
           
 T stub()
          Stubs out (removes) the behavior of the method when the invocation following this call matches the observed behavior.
 
Methods inherited from class org.unitils.mock.core.MockObject
assertInvoked, assertInvokedInSequence, assertNotInvoked, createAlwaysMatchingBehaviorDefiningInvocations, createAlwaysMatchingBehaviorDefiningMatchingInvocationHandler, createAssertInvokedInSequenceVerifyingMatchingInvocationHandler, createAssertInvokedVerifyingMatchingInvocationHandler, createAssertNotInvokedVerifyingMatchingInvocationHandler, createChainedMock, createMatchingInvocationBuilder, createOneTimeMatchingBehaviorDefiningInvocations, createOneTimeMatchingBehaviorDefiningMatchingInvocationHandler, createScenario, getCurrentScenario, getMatchingInvocationBuilder, getMock, getMockedType, getName, getObjectToInject, getObjectToInjectType, getScenario, oncePerforms, onceRaises, onceRaises, onceReturns, performs, raises, raises, resetBehavior, returns, startMatchingInvocation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.unitils.mock.Mock
assertInvoked, assertInvokedInSequence, assertNotInvoked, getMock, oncePerforms, onceRaises, onceRaises, onceReturns, performs, raises, raises, resetBehavior, returns
 

Constructor Detail

PartialMockObject

public PartialMockObject(Object mockPrototype,
                         Object testObject)
Creates a mock of the same type as the given mock prototype with un-capitalized type name + Mock as name, e.g. myServiceMock. All instance fields of the given prototype will then be copied to the mock instance. This way you can have a pre-initialized instance of the mock (e.g. when there is no default constructor). If the type mocked instance does not correspond to the declared type, a ClassCastException will occur when the mock is used.

Parameters:
mockPrototype - The instance that will be wrapped with a proxy, use the raw type when mocking generic types, not null
testObject - The test object, not null

PartialMockObject

public PartialMockObject(String name,
                         Object mockedInstance,
                         Object testObject)
Creates a mock of the same type as the given mock prototype with the given name. All instance fields of the given prototype will then be copied to the mock instance. This way you can have a pre-initialized instance of the mock (e.g. when there is no default constructor). If the type mocked instance 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
mockedInstance - The instance that will be wrapped with a proxy, use the raw type when mocking generic types, not null
testObject - The test object, not null

PartialMockObject

public PartialMockObject(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

PartialMockObject

public PartialMockObject(String name,
                         Class<?> mockedType,
                         Object testObject)
Creates a mock of the given type for the given scenario. 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

stub

public T stub()
Stubs out (removes) the behavior of the method when the invocation following this call matches the observed behavior. E.g.

mock.stub().method1();

will not invoke the actual behavior of method1.

If the method has a return type, a default value will be returned.

Note: stubbed methods can still be asserted afterwards: e.g.

mock.assertInvoked().method1();

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

createMockProxy

protected MockProxy<T> createMockProxy()
Overrides:
createMockProxy in class MockObject<T>


Copyright © 2011. All Rights Reserved.