org.unitils.easymock
Class EasyMockModule

java.lang.Object
  extended by org.unitils.easymock.EasyMockModule
All Implemented Interfaces:
Module

public class EasyMockModule
extends Object
implements Module

Module for testing with mock objects using EasyMock.

Mock creation is simplified by automatically inserting EasyMock generated mocks for fields annotated with the Mock annotation.

All methods annotated with AfterCreateMock will be called when a mock object was created. This provides you with a hook method for custom handling of the mock (e.g. adding the mocks to a service locator repository). A method can only be called if it has following signature void myMethod(Object mock, String name, Class type).

Mocks can also be created explicitly todo javadoc

Switching to the replay state and verifying expectations of all mocks (including the mocks created with the createMock() method can be done by calling the replay() and verify() methods.

Author:
Filip Neven, Tim Ducheyne

Nested Class Summary
protected  class EasyMockModule.EasyMockTestListener
          Test listener that handles the mock creation and injection.
 
Field Summary
static String PROPKEY_AUTO_VERIFY_AFTER_TEST_ENABLED
           
 
Constructor Summary
EasyMockModule()
           
 
Method Summary
 void afterInit()
          No after initialization needed for this module
protected  void callAfterCreateMockMethods(Object testObject, Object mockObject, String name, Class<?> type)
          Calls all AfterCreateMock annotated methods on the test, passing the given mock.
protected  void createAndInjectMocksIntoTest(Object testObject)
           
protected  void createAndInjectRegularMocksIntoTest(Object testObject)
          Creates and sets a mock for all RegularMock annotated fields.
<T> T
createMock(Class<T> mockType, InvocationOrder invocationOrder, Calls calls, Order order, Dates dates, Defaults defaults)
          todo javadoc

Creates an EasyMock mock instance of the given type (class/interface).

<T> T
createRegularMock(Class<T> mockType, InvocationOrder invocationOrder, Calls calls)
          Creates an EasyMock mock object of the given type.
 TestListener getTestListener()
          Creates the listener for plugging in the behavior of this module into the test runs.
 void init(Properties configuration)
          Initializes the module
 void replay()
          Replays all mock controls.
 void reset()
          Resets all mock controls.
 void verify()
          This method makes sure MocksControl.verify() method is called for every mock mock object that was injected to a field annotated with Mock, or directly created by calling createRegularMock(Class, InvocationOrder, Calls) or createMock(Class, InvocationOrder, Calls, Order, Dates, Defaults).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPKEY_AUTO_VERIFY_AFTER_TEST_ENABLED

public static final String PROPKEY_AUTO_VERIFY_AFTER_TEST_ENABLED
See Also:
Constant Field Values
Constructor Detail

EasyMockModule

public EasyMockModule()
Method Detail

init

public void init(Properties configuration)
Initializes the module

Specified by:
init in interface Module
Parameters:
configuration - The config, not null

afterInit

public void afterInit()
No after initialization needed for this module

Specified by:
afterInit in interface Module

getTestListener

public TestListener getTestListener()
Creates the listener for plugging in the behavior of this module into the test runs.

Specified by:
getTestListener in interface Module
Returns:
the listener

createRegularMock

public <T> T createRegularMock(Class<T> mockType,
                               InvocationOrder invocationOrder,
                               Calls calls)
Creates an EasyMock mock object of the given type.

An instance of the mock control is stored, so that it can be set to the replay/verify state when replay() or verify() is called.

Type Parameters:
T - the type of the mock
Parameters:
mockType - the class type for the mock, not null
invocationOrder - the order setting, not null
calls - the calls setting, not null
Returns:
a mock for the given class or interface, not null

createMock

public <T> T createMock(Class<T> mockType,
                        InvocationOrder invocationOrder,
                        Calls calls,
                        Order order,
                        Dates dates,
                        Defaults defaults)
todo javadoc

Creates an EasyMock mock instance of the given type (class/interface). The type of mock is determined as follows:

If returns is set to LENIENT, a nice mock is created, else a default mock is created If arguments is lenient a lenient control is create, else an EasyMock control is created If order is set to strict, invocation order checking is enabled

Type Parameters:
T - the type of the mock
Parameters:
mockType - the type of the mock, not null
invocationOrder - the order setting, not null
calls - the calls setting, not null
order - todo
dates - todo
defaults - todo
Returns:
a mockcontrol for the given class or interface, not null

replay

public void replay()
Replays all mock controls.


reset

public void reset()
Resets all mock controls.


verify

public void verify()
This method makes sure MocksControl.verify() method is called for every mock mock object that was injected to a field annotated with Mock, or directly created by calling createRegularMock(Class, InvocationOrder, Calls) or createMock(Class, InvocationOrder, Calls, Order, Dates, Defaults).

If there are mocks that weren't already switched to the replay state using MocksControl.replay()} or by calling EasyMockUnitils.replay(), this method is called first.


createAndInjectRegularMocksIntoTest

protected void createAndInjectRegularMocksIntoTest(Object testObject)
Creates and sets a mock for all RegularMock annotated fields.

The todo javadoc method is called for creating the mocks. Ones the mock is created, all methods annotated with AfterCreateMock will be called passing the created mock.

Parameters:
testObject - the test, not null

createAndInjectMocksIntoTest

protected void createAndInjectMocksIntoTest(Object testObject)

callAfterCreateMockMethods

protected void callAfterCreateMockMethods(Object testObject,
                                          Object mockObject,
                                          String name,
                                          Class<?> type)
Calls all AfterCreateMock annotated methods on the test, passing the given mock. These annotated methods must have following signature void myMethod(Object mock, String name, Class type). If this is not the case, a runtime exception is called.

Parameters:
testObject - the test, not null
mockObject - the mock, not null
name - the field(=mock) name, not null
type - the field(=mock) type


Copyright © 2011. All Rights Reserved.