User:Ggpalank/sandbox

EasyMock
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 ) so that a dummy functionality can be added to a mock interface/object that can be used in unit testing.

Benefits of EasyMock

 * 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
A simple Java Employee class: A class to use Employee object: Testing the createNewEmp method using EasyMock:

Mocks and Reality
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.

Requirements

 * EasyMock requires Java 1.5.0 and above.
 * Objenesis(2.0+), a Java library to instantiate a new object of a particular class.
 * Maven