FEST-Mocks is a Java library which mission is to minimize potential shortcomings of Mock Objects.
More information about the potential shortcomings of mocks can be found at:
FEST-Mocks requires Java SE 5.0 or later and can be used with either JUnit or TestNG.
It can be downloaded here. For Maven 2 users, the project's repository can be found at http://fest.googlecode.com/svn/trunk/fest/m2/repository/ (groupId: fest, artifactId: fest-mocks).
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:
replay and verify are duplicatedreplay 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(); }
replay and
verify)replay and
verify