public abstract class EasyMockTemplate extends Object
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 and Description |
---|
EasyMockTemplate(Object... mocks)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
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.
|
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.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.Copyright © 2007-2012 FEST (Fixtures for Easy Software Testing). All Rights Reserved.