|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.fest.mocks.EasyMockTemplate
public abstract class 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:
replay and verify anymore)
| 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 |
|---|
public EasyMockTemplate(Object... mocks)
mocks - the mocks for this template to manage.
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 |
|---|
public final void run()
UnexpectedError - wrapping any checked exception thrown during the execution of this method.protected final List<Object> mocks()
protected abstract void expectations()
throws Throwable
Throwable - any error thrown by the implementation of this method.
protected abstract void codeToTest()
throws Throwable
Throwable - any error thrown by the implementation of this method.
protected void setUp()
throws Throwable
Throwable - any error thrown by the implementation of this method.
protected void cleanUp()
throws Throwable
Throwable - any error thrown by the implementation of this method.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||