1. EasyMock

1. EasyMock

The class under test depends on another class (the collaborator) which will be replaced by a mock implementation. In order to control the behavior of the mock implementation, we use an instance of MockControl.

There are three stages in using Easymock:

  1. Train the mock object
  2. After training, use the class under test. The mock instance's methods get used in the same way as during training.
  3. Some method invocations are immediately recognized as wrong (wrong order, different method, wrong arguments etc...) and generate an AssertionFailedException. Call the control's verify() method. This will throw AssertionFailedExceptions when some different conditions are not met (insufficient method calls etc.)
 
eventTrackerControl = MockControl.createControl(EventTrackerSpecialized.class);
eventTracker = (EventTrackerSpecialized)eventTrackerControl.getMock();
instance = new IdentifierFactoryImpl(null, eventTracker);
					
  
//train eventTracker usage
eventTracker.getCurrentReferencesFor(user1);        
Map map1 = new HashMap();
map1.put("a", new SearchReference("101"));
map1.put("b", new SearchReference("102"));
eventTrackerControl.setReturnValue(map1);
	
//end training, start using
eventTrackerControl.replay();

SearchReference reference = instance.createNewSearchReference("a", user1, "ddd");
assertNull(reference);

//verify actual behavior vs expected
eventTrackerControl.verify();