My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

EasyMock

Introduction

Test-driven development is a critical component of software development. If code isn't tested, it's broken. All code must be tested, and ideally the tests should be written before the model code is.
EasyMock provides Mock Objects for interfaces (and objects through the class extension) by generating them on the fly using Java's proxy mechanism. Due to EasyMock's unique style of recording expectations, most refactoring will not affect the Mock Objects. So EasyMock is a perfect fit for Test-Driven Development.
A Mock Object is a test-oriented replacement for a collaborator. It is configured to simulate the object that it replaces in a simple way. In contrast to a stub, a Mock Object also verifies whether it is used as expected.
EasyMock is an open source mock object library for the Java programming language that helps you quickly and easily creates mock objects for all these purposes. Through the magic of dynamic proxies, EasyMock enables you to create a basic implementation of any interface with just one line of code. By adding the EasyMock class extension, you can create mocks for classes too. These mocks can be configured for any purpose, ranging from simple dummy arguments for filling out a method signature to multi-invocation spies that verify a long sequence of method calls..

We can separate the EasyMock implementation into FOUR steps


1. Creating a Mock Object using "EasyMock.createMock".
Create Mock : Using this static method we are creating a mock object. And this is the first step which we need to do in mock testing.
When we create these mock objects then we can follow three levels.
Regular: If we are expecting some methods to be executed then it was not executed then the test will fail. And if any unexpected tests executed then also test will fail. Here the order of the method execution is not important.
Ex: EmpDAO empDAO = EasyMock.createMock(EmpDAO.class);

Nice: If we are expecting some methods to be executed then it was not executed then the test will fail. And if any unexpected tests executed then it will return a default value. Here also order is not important.
Ex: EmpDAO empDAO = EasyMock.createNiceMock(EmpDAO.class);

Strict: Same as regular but here the Order of the expected methods also important.
Ex: EmpDAO empDAO = EasyMock.createStrictMock(EmpDAO.class);

2. Expecting mock object method calls using "EasyMock.expect".
This is used to expect some method calls from our mock object. Lets go through one example.
Let's assume we have the following methods which gets employee information from DB.
List<Employee> employee = empDao.getEmpDetails();
List<Employee> employee = empDao.getEmpDetailsByName("bond");

In the unit test we need to follow as follows...
EmpDao mockDao = EaskMock.createMock(EmpDao.class);

Employee mockEmp = new Employee();

mockEmp.setEmpName("bond");
mockEmp.setEmpCode("007"); 

List<Employee> empList=new ArrayList< Employee >(1);
empList.add(mockEmp);

expect(mockDao.getEmpDetails()).andReturn(empList);
expect(mockDao.getEmpDetailsByName("bond")).andReturn(empList);
replay(mockDao);


3. Registering/replaying expected methods using "EasyMock.replay".
Once the behavior of the mock objects has been recorded with expectations, the mock objects must be prepared to replay those expectations. We are using replay() method for this purpose. EaskMock will stop the expecting behavior once this method calls.
EasyMock.replay(mockDao);

4. Verifying the expected methods using "EasyMock.verify".
Veirfying the mock expectations is the final step which we need to follow. This includes validating that all methods that were expected to be called were called and that any calls that were not expected are also noted.
EasyMock.verify(mock);


Easymock is providing more functionalities like "Matchers" etc for more unit testing flexibility. EasyMock has been the first dynamic Mock Object generator, relieving users of hand-writing Mock Objects, or generating code for them. It helps us to increase our testing coverage a lot.