Blog‎ > ‎


posted Oct 12, 2011, 9:51 AM by Unknown user   [ updated Dec 15, 2011, 12:05 PM by Victor Reina ]
What’s a Mock Object?

Unit testing works as a testing of software units in an isolation environment. Units don’t work by themselves in isolation; so, to test a unit in isolation, we need to put the collaborators in the test. To replace a collaborator, we have a Mock Object.

Some people could confuse a Stub with a Mock Object. They’re not the same. A Stub is a style of testing which uses state verification, which means that we examine the state of the SUT (System Under Test) to determine if the exercised method worked correctly. A Mock Object uses behavior verification, where we instead check to see if the order made the correct calls by telling the mock what to expect and asking the mock to verify itself during verification. Mock Objects simulate parts of the behavior of domain code, and are able to check whether they are used as defined. Domain classes can be tested in isolation by simulating their collaborators with Mock Objects.

EasyMock overview

EasyMock is an open source framework software project which provides and generates on-the-fly Mock Objects for Java interfaces. It uses Java’s proxy mechanism. EasyMock works greatly with Testing due to its unique style of recording expectations, which won’t affect the Mock Objects.

EasyMock has been the first dynamic Mock Object generator, relieving users of hand-writing Mock Objects, or generating code for them. EasyMock generates two objects: the EasyMock Mock Object and the EasyMock Mock Control.

Installing EasyMock

  1. Download EasyMock at:
  2. Unzip the EasyMock zip file.
  3. Add the EasyMock jar file (easymock-3.0.jar) to your classpath.
    1. To do this in the easiest way (Windows), we open the Start Menu and search for “Environment variables”  

- Then, search for the CLASSPATH variable in the “User variables” tab and click Edit.


-        Set the directory from your EasyMock jar file and then click “OK”

Using EasyMock

To get a Mock Object, we need to:

  1. Create a Mock Object for the interface we would like to simulate,
    1. Identify the classes/methods which are the collaborators. We can mock the classes by first mocking the Interface.
    2. createMock method is used to create the mock object.
    3. stmtMock = createMock (Statement.class);
  2. Record the expected behavior. You first train the mock by making the expected method calls on it.
    1. expect(stmtMock.executeQuery(“SELECT * FROM survey”)).andReturn(rsMock);
  3. Switch the Mock Object to replay state. It tells EasyMock to stop recording. After that, it calls on the object to return the set values. If it gets a call it does not expect, it throws an Exception to fail fast.
There are no short examples to show how a Mock Object works, but let’s try one. First we create a new class CalculationManager and we give it some methods.

public class CalculationManager {

private IMultiplicationService multiplicatorService;

public IMultiplicationService getMultiplicatorService() {
    return multiplicatorService;

public void setMultiplicatorService(IMultiplicationService multiplicatorService) {
    this.multiplicatorService = multiplicatorService;

public Integer calculatePrice(Integer price, Integer amount, Integer reduction) {
    return multiplicatorService.multiply(price, amount) - reduction;

Then we create an interface IMultiplicationService declaring some variables.

public interface IMultiplicationService {
public Integer multiply(Integer number1, Integer number2);

Then we create the test file, using Mock Objects to call specific methods and giving behavior to the Mock Object

import org.easymock.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class EasyMockTest {

private IMultiplicationService multiplicatorServiceMock;
private CalculationManager calculationManager;

public void setUp() throws Exception {
    multiplicatorServiceMock = EasyMock.createMock(IMultiplicationService.class);
   calculationManager = new CalculationManager();

public void testCalculationManager() {
    Integer oneProductPrice = 12;
    Integer amount = 3;
       Integer reduction = 6;

    //inject behavior of MultiplicatorService
            .multiply(oneProductPrice, amount))
            .andReturn(oneProductPrice * amount);

    //Setting up the mock

    //Object to be tested
    Integer result =
            calculationManager.calculatePrice(oneProductPrice, amount, reduction);
    Assert.assertEquals(result, oneProductPrice * amount - reduction);




There’s nothing much I can say about community.  It was created by Tammo Freese and is now maintained by Henri Tremblay. The current version is EasyMock 3.0 and it’s available to download for free.

There’s just a single page giving documentation and we can find the issue tracker at:

Also, we can access to the mailing list at:  . It has exactly 563 members and it was founded in February 5, 2002.


In my opinion, EasyMock is a useful tool to help the unit testing. It can be changed perfectly for a collaborator. It’s not hard to learn and it has a lot of great uses.