undefined

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Previous: EasyMock Next: Test Hooks - Part II

Test Hooks - Part I

For example, if we want to record the timestamp at which a reminder is inserted in the database, we need to control what value is sent to the database at the time of insertion. The below example shows an incomplete way of verifying the behavior:

import org.easymock.EasyMock;
import org.testng.annotations.Test;
interface DataLayer {
void insertReminder(
final Reminder reminder, long creationTimestampInMillis);
}
class Reminder {
// Reminder fields go here
}
class Orchestrator {
private final DataLayer dataLayer;
public Orchestrator(final DataLayer dataLayer) {
this.dataLayer = dataLayer;
}
public void addReminder(final Reminder reminder) {
// Validation and pre-processing code goes here
dataLayer.insertReminder(
reminder,
System.currentTimeMillis());
// ...
}
}
public class OrchestratorTest {
@Test
public void testAddReminder() throws Exception {
final Reminder reminderToAdd = new Reminder();
final DataLayer dataLayerMock =
EasyMock.createStrictMock(DataLayer.class);
final Orchestrator orchestrator =
new Orchestrator(dataLayerMock);
dataLayerMock.insertReminder(
EasyMock.same(reminderToAdd), EasyMock.anyLong());
EasyMock.replay(dataLayerMock);
orchestrator.addReminder(reminderToAdd);
EasyMock.verify(dataLayerMock);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content