org.fest.mocks
Class EasyMockTemplate

java.lang.Object
  extended by org.fest.mocks.EasyMockTemplate

public abstract class EasyMockTemplate
extends Object

Understands a template for usage of EasyMock mocks.

Here is an small example that uses EasyMock to verify that EmployeeBO uses a EmployeeDAO to store employee information in the database:

 
 @Test public void shouldAddNewEmployee() {
   mockEmployeeDao.insert(employee);
   replay(mockEmployeeDao);
   employeeBo.addNewEmployee(employee);
   verify(mockEmployeeDao);    
 }
 
 

In this example, it is easy to distinguish the expectations made on the mock object (mockEmployeeDao). But in a more complex scenario, that distinction could be more difficult to spot. We can use the EasyMockTemplate to separate the code to be tested from the mock expectations:

 
 @Test public void shouldAddNewEmployee() {
   EasyMockTemplate t = new EasyMockTemplate(mockEmployeeDao) {
     @Override protected void expectations() {
       mockEmployeeDao.insert(employee);
     }

     @Override protected void codeToTest() {
       employeeBo.addNewEmployee(employee);
     }
   };
   t.run();    
 }
 
 

The benefits of EasyMockTemplate are:

Author:
Alex Ruiz

Constructor Summary
EasyMockTemplate(Object... mocks)
          Constructor.
 
Method Summary
protected  void cleanUp()
          Cleans up any resources if necessary.
protected abstract  void codeToTest()
          Executes the code that is under test.
protected abstract  void expectations()
          Sets the expectations on the mock objects.
protected  List<Object> mocks()
          Returns all the mocks managed by this template.
 void run()
          Encapsulates EasyMock's behavior pattern.
protected  void setUp()
          Sets up the test fixture if necessary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EasyMockTemplate

public EasyMockTemplate(Object... mocks)
Constructor.

Parameters:
mocks - the mocks for this template to manage.
Throws:
IllegalArgumentException - if the list of mock objects is null or empty.
IllegalArgumentException - if any of the given mocks is null.
IllegalArgumentException - if any of the given mocks is not a mock.
Method Detail

run

public final void run()
Encapsulates EasyMock's behavior pattern.
  1. Set up expectations on the mock objects
  2. Set the state of the mock controls to "replay"
  3. Execute the code to test
  4. Verify that the expectations were met
Steps 2 and 4 are considered invariant behavior while steps 1 and 3 should be implemented by subclasses of this template.

Throws:
UnexpectedError - wrapping any checked exception thrown during the execution of this method.

mocks

protected final List<Object> mocks()
Returns all the mocks managed by this template.

Returns:
all the mocks managed by this template.

expectations

protected abstract void expectations()
                              throws Throwable
Sets the expectations on the mock objects.

Throws:
Throwable - any error thrown by the implementation of this method.

codeToTest

protected abstract void codeToTest()
                            throws Throwable
Executes the code that is under test.

Throws:
Throwable - any error thrown by the implementation of this method.

setUp

protected void setUp()
              throws Throwable
Sets up the test fixture if necessary.

Throws:
Throwable - any error thrown by the implementation of this method.

cleanUp

protected void cleanUp()
                throws Throwable
Cleans up any resources if necessary.

Throws:
Throwable - any error thrown by the implementation of this method.


Copyright © 2007-2011 FEST (Fixtures for Easy Software Testing). All Rights Reserved.