FEST-Mocks is a Java library which mission is to minimize potential shortcomings of Mock Objects.
EasyMockTemplate
One of the shortcomings of using mocks is introduction of clutter and duplication in our code. Take a look at this example using EasyMock:
@Test public void shouldAddNewEmployee() {
mockEmployeeDAO.insert(employee);
replay(mockEmployeeDAO);
employeeBO.addNewEmployee(employee);
verify(mockEmployeeDAO);
}
@Test public void shouldUpdateEmployee() {
mockEmployeeDAO.update(employee);
replay(mockEmployeeDAO);
employeeBO.updateEmployee(employee);
verify(mockEmployeeDAO);
}
The problems with the code listing above are the following:
1. There is no clear separation of mock expectations and code to test
2. Calls to replay and verify are duplicated
3. It is easy to forget to call replay and verify in every test method, which will result in failing tests
A solution to this problem is FEST's EasyMockTemplate:
@Test public void shouldUpdateEmployee() {
new EasyMockTemplate(mockEmployeeDao) {
@Override protected void expectations() {
mockEmployeeDAO.update(employee);
}
@Override protected void codeToTest() {
employeeBO.updateEmployee(employee);
}
}.run();
}
- We have eliminated code duplication (calls replay and verify)
- We have a clear separation of mock expectations and code to test
- We no longer have to call replay and verify
Product's homepage
Requirements:
· Java SE 5.0 or later