Package org.fest.mocks
Class EasyMockTemplate
java.lang.Object
org.fest.mocks.EasyMockTemplate
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
andverify
anymore)
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate Object
checkAndReturnMock
(Object mock) protected void
cleanUp()
Cleans up any resources if necessary.protected abstract void
Executes the code that is under test.protected abstract void
Sets the expectations on the mock objects.mocks()
Returns all the mocks managed by this template.final void
run()
Encapsulates EasyMock's behavior pattern.protected void
setUp()
Sets up the test fixture if necessary.
-
Field Details
-
mocks
Mock objects managed by this template
-
-
Constructor Details
-
EasyMockTemplate
Constructor.- Parameters:
mocks
- the mocks for this template to manage.- Throws:
IllegalArgumentException
- if the list of mock objects isnull
or empty.IllegalArgumentException
- if any of the given mocks isnull
.IllegalArgumentException
- if any of the given mocks is not a mock.
-
-
Method Details
-
checkAndReturnMock
-
run
public final void run()Encapsulates EasyMock's behavior pattern.- Set up expectations on the mock objects
- Set the state of the mock controls to "replay"
- Execute the code to test
- Verify that the expectations were met
- Throws:
UnexpectedError
- wrapping any checked exception thrown during the execution of this method.
-
mocks
Returns all the mocks managed by this template.- Returns:
- all the mocks managed by this template.
-
expectations
Sets the expectations on the mock objects.- Throws:
Throwable
- any error thrown by the implementation of this method.
-
codeToTest
Executes the code that is under test.- Throws:
Throwable
- any error thrown by the implementation of this method.
-
setUp
Sets up the test fixture if necessary.- Throws:
Throwable
- any error thrown by the implementation of this method.
-
cleanUp
Cleans up any resources if necessary.- Throws:
Throwable
- any error thrown by the implementation of this method.
-