Class EasyMockTemplate

java.lang.Object
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:

  • Clear separation of mock expectations and code to test
  • Less code duplication (we don't have to call replay and verify anymore)

  • Field Details

    • mocks

      private final List<Object> mocks
      Mock objects managed by this template
  • Constructor Details

  • Method Details

    • checkAndReturnMock

      private Object checkAndReturnMock(Object mock)
    • 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.