Skip to main content

Introducing EasyMock

This blog will share my understanding about EasyMock, before moving forward to EasyMock first want to tell you about what is Mock and what is Mock Object and there purpose.

One of the web definition of Mock is “An intimation of counterfeit”. If we apply definition of mock to mock objects, mock objects are just an intimation or counterfeit of objects. Now we have to look where these mock objects are applied in real world.

Consider a use case where we have a UserService that performs some business operation on a User entity and then persists user entity using UserDao(Persistence Service). When we write test case for UserService, unintentionally we will be testing UserDao also and we have to take care of successful execution of UserDao also. In such cases Mock Objects comes into picture, we create a mock object of UserDao and provide this mock object to UserService. UserDao mock object will intimate as UserDao and will behave as required.

In coming sections I'll be taking this use case as an example

EasyMock is about creating mock objects for interfaces. The mock objects are created using java proxy mechanism. Once mock objects are created you have to record the behavior of mock objects before using mock objects. Recording behavior of mock objects signifies registering method calls to mock object which can be invoked while testing.

Before using a mock object it needs to be instantiated. To instantiate a mock object first it needs to created using createMock(EasyMock.createMock()) . Once a mock object is created it comes into record state, in record state the expected behavior of mock object is recorded which includes specifying methods which can be invoked on mock objects. After recording behaviors to a mock object the state of mock object is changed to replay state by calling replay(EasyMock.replay()) api.

So if we have to create a mock object for UserDao using EasyMock, the API call will be like this
userDao = EasyMock.createMock(UserDao.class);

This section tells you about adding behavior for method call to mock objects. As told in previous section behaviors to a mock object are added in record sate. Adding behavior for method call is similar to actually calling the method, the only difference is that at the time of recording behavior gets registered to mock object. If we have to record the delete operation of user entity we have to write userDao.delete(user) in the record phase of userDao.

So if we have to record the persistence behaviour to mock “userDao” object the code code will be like this
// Setting up the expected value of the method call delete
userDao.delete(dummyUser);
// Setup is finished need to activate the mock userDao object
EasyMock.replay(userDao);

In next blog I'll discuss behavior of mock objects in replay phase and the advanced features of easy mock such as restricting no of times a method can be called, ensuring the sequence of method calls, verifying the return values etc.
So keep waiting :)

Comments

Popular posts from this blog

EC2 Ssh Connection Refused

When ssh: connect to host ip_address port 22 Connection refused



Unable to access server???
Exactly when you see the error - “ssh: connect to host ip_address port 22: Connection refused” while connecting your AWS EC2 Instance. In order to find solution of the problem, you will go to AWS forum and other channels where you need to answers several questions first. But it's very difficult to find the actual problem. In order to get clues what the problem is, we should provide as many details as possible about what we have tried and the results we are getting. Because there are hundreds of reason why a server or service might not be accessible, also connectivity is one of the toughest issue to diagnose, especially when you are hosting something critical on your box. I've seen several topics on this problem, but none offers a solution to it.  I was not aware for what should I look at first. So I walk through from the very basics and investigated the following thing Use of verbose while ss…

jgit-flow maven plugin to Release Java Application

Introduction As a DevOps I need a smooth way to release the java application, so I compared two maven plugin that are used to release the java application and in the end I found that Jgit-flow plugin is far better than maven-release plugin on the basis of following points: Maven-release plugin creates .backup and release.properties files to your working directory which can be committed mistakenly, when they should not be. jgit-flow maven plugin doesn't create these files or any other file in your working directory.Maven-release plugin create two tags.Maven-release plugin does a build in the prepare goal and a build in the perform goal causing tests to run 2 times but jgit-flow maven plugin builds project once so tests run only once.If something goes wrong during the maven plugin execution, It become very tough to roll it back, on the other hand jgit-flow maven plugin makes all changes into the branch and if you want to roll back just delete that branch.jgit-flow maven plugin doesn…

VPC per envrionvment versus Single VPC for all environments

This blog talks about the two possible ways of hosting your infrastructure in Cloud, though it will be more close to hosting on AWS as it is a real life example but this problem can be applied to any cloud infrastructure set-up. I'm just sharing my thoughts and pros & cons of both approaches but I would love to hear from the people reading this blog about their take as well what do they think.


Before jumping right away into the real talk I would like to give a bit of background on how I come up with this blog, I was working with a client in managing his cloud infrastructure where we had 4 environments dev, QA, Pre Production and Production and each environment had close to 20 instances, apart from applications instances there were some admin instances as well such as Icinga for monitoring, logstash for consolidating logs, Graphite Server to view the logs, VPN server to manage access of people.




At this point we got into a discussion that whether the current infrastructure set-u…