EasyMock

edit
EasyMock
Stable release
3.4[1] / September 6, 2015 (2015-09-06)
Written inJava
Operating systemCross-platform
TypeUnit testing tool
Websiteeasymock.org

EasyMock is an open source mocking framework which is used for effective unit testing of Java Applications. Mocking is a way to test the functionality of a class in isolation. Mocking does not require a database connection or file server read to test a functionality. Mock objects do the mocking of the real service. Mock objects are nothing but proxy for actual implementations. A mock object returns a dummy data corresponding to some dummy input passed to it.

Consider a case of Stock Service which returns the price details of a stock. During development, the actual stock service cannot be used to get real-time data. Hence a dummy implementation of the stock service is required which can be done using EasyMock.

EasyMock is used to mock interfaces as well as classes with minimal amount of code (usually one line[2]) so that a dummy functionality can be added to a mock interface/object that can be used in unit testing.

Benefits of EasyMock

edit
  • Less amount of test code – No need to write mock objects on your own.
  • Simulating error conditions - A test case which involves disabling the network can be written easily in EasyMock rather than going through the labor-intensive task of physically removing the ethernet cable.
  • Spying on behaviour of a class - By placing assertions inside the mock code, you can verify that the code under test is passing the correct arguments to its collaborators at the right time. A mock can let you see and test private parts of a class without exposing them through otherwise unnecessary public methods
  • Removing large dependencies from a test - EasyMock helps in making the tests more unitary. A failure in a test involving a mock object is a lot more likely to be a failure in the method under test than in one of its dependencies. This helps isolate the problem and makes debugging simpler.
  • Save Costs - A lot of times developers connect to different services which cost money to use. By using EasyMock, the functionalities of these services can be mocked while developing new features which can help reduce cost.

Example

edit

A simple Java Employee class:

public class Employee {
	private String name;
	private int emp_id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEmp_id() {
		return emp_id;
	}
	public void setEmp_id(int emp_id) {
		this.emp_id = emp_id;
	}
}

A class to use Employee object:

public class CreateEmployee {
	public Employee createNewEmp(Employee emp) {
		System.out.println("Creating new employee by name " + emp.getName() + "and ID " + emp.getEmp_id());
		return emp;
	}
}

Testing the createNewEmp method using EasyMock:

import static org.junit.Assert.assertEquals;
import org.easymock.EasyMock;
import org.junit.Test;

public class CreateEmployeeTest {
	
	@Test
	public void testEmployee(){
		//Creating Employee mock object
		Employee employeeMock = EasyMock.createMock(Employee.class);
		EasyMock.expect(employeeMock.getName()).andReturn("Alex").times(2);
		EasyMock.expect(employeeMock.getEmp_id()).andReturn(1234).times(2);
	    //Activating mock
		EasyMock.replay(employeeMock);
	
		//Testing
		CreateEmployee empcr=new CreateEmployee();
		Employee emp=new Employee();
		emp=empcr.createNewEmp(employeeMock);
		System.out.println(emp);
		assertEquals("Alex",emp.getName());
		assertEquals(1234,emp.getEmp_id());
	}
}

Mocks and Reality

edit

EasyMock doesn't provide anything which cannot be done by manually writing mock classes, which would also reduce the number of dependencies. However EasyMock is advantageous in the case of mocking larger interfaces and can help writing shorter and more readable code in a shorter period of time.

Mocking in general have to be treated with caution. It is possible to mock so much that a particular test case always passes even though the code's functionality is incorrect. Sometimes bugs exist in dependent libraries and in the interactions between one method and the method it calls, and thereby mocking such dependencies will not reveal all the bugs one would ideally find. Ideally mocking should not be the first choice and if there is a possibility to use the actual dependency, it is preferred. If a particular dependency or class cannot be tested reliably or automatically, mocking turns out be a good alternative for testing[2].

Requirements

edit
  • EasyMock requires Java 1.5.0 and above. [3]
  • Objenesis(2.0+), a Java library to instantiate a new object of a particular class.
  • Maven

See also

edit
edit

References

edit
  1. ^ EasyMock Releases
  2. ^ a b "Easier testing with EasyMock". www.ibm.com. Retrieved 2015-09-12.
  3. ^ "EasyMock Requirements".