EasyMock

Posted: December 1, 2011 in Unit Testing

Before proceeding any further, let me plain unit testing in terms of its phases

Test Setup : Define and Create all the Data and Objects you need for the tests
Expectations : Say what methods and parameters you expect to be executed during the test
Test : The actual behavior/method call you want to test
Assertions : Statements that make sure the outcome of the test were successful
Test Tear down : Destroy any side effects that occurred during the test

jUnit is a Unit Testing Framework and provides all but the Expectations phase of testing.

The concept of mocking is what added the new phase of Expectations, and since jUnit saw most of it’s major development prior to the mocking movement, those features were not incorporated into the core, and a set of tools to fill that gap in the java space have opened up. Those libraries include

EasyMock
jMock
jMockIt

All of these libraries are compliments to any of the above Unit Testing frameworks I listed, including jUnit. They add the ability to define mock objects. Mock objects get “expectations” assigned to them, which are then asserted in the Assertions phase. Each Mock library accomplishes this slightly differently, but the major models are

Record Replay – EasyMock
Expectations – jMock, jMockIt

Sample EASYMOCK Program to calculate employee salary and bonus

Salary class has to find out the employee salary and bonus but the business logic resides in FinanceDepartment.java

public interface IFinanceDepartment {

public long calculateSalary(long days);

public long calculateBonus(long employeeRating);

}

public class FinanceDepartment implements IFinanceDepartment {

@Override
public long calculateBonus(long employeeRating) {
return employeeRating * 5000;
}

@Override
public long calculateSalary(long days) {
return days * 45;
}

}

public class  Salary {

private IFinanceDepartment financeDepartment;

public IFinanceDepartment getFinanceDepartment() {
return financeDepartment;
}

public void setFinanceDepartment(IFinanceDepartment financeDepartment) {
this.financeDepartment = financeDepartment;
}

public Long calculateSalary(Long days) {
return financeDepartment.calculateSalary(days);
}

public Long calculateBonus(Long employeeRating) {
return financeDepartment.calculateBonus(employeeRating);
}

}

Now let’s write a EasyMock Test Case

import junit.framework.TestCase;

import org.easymock.EasyMock;
import org.junit.BeforeClass;
import org.junit.Test;

public class SalaryEasyMockTest extends TestCase {

private IFinanceDepartment financeDepartment;

private Salary salary;

@BeforeClass
public void setUp() throws Exception {
financeDepartment = EasyMock.createMock(IFinanceDepartment.class);
salary = new Salary();
salary.setFinanceDepartment(financeDepartment);
}

Limitations of EasyMock

  • Cannot mock static methods
  • Cannot mock private methods
  • Cannot mock final methods
  • Cannot mock concrete classes
  • Cannot mock Constructors
Advertisements
Comments
  1. Henri says:

    Hi, I’m the lead developer on EasyMock. I just want to modify the limitations you mention. EasyMock can absolutely mock concrete classes. It can also mock contructors (not calling them or calling a specific one with the wanted parameters).

    For static, private and final methods and final classes, you need to use PowerMock which is a layer above EasyMock.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s