Java is not a language with training wheels. It is a language for professional programmers.

— Hebert Sheildt

JUnit Using EasyMock

Junit is one of the most popular frameworks for Java platform for performing UT. Mocking is also an aspect which goes hand in hand with JUnit. Unit Testing is done by the developer itself for the code he’s written.

Let’s consider,
Developer 1: Developed Business Logic Code.
Developer 2: Developed Persistence Logic Code. (Code which interacts with the database)


In case, developer 1 has to use the persistence logic code (method call) written by developer 2, he might mock that method call. Doing so will let developer focus only on his code and test it. It’s more of like staying in an environment and just testing the code in that environment itself.

While unit testing, developer just test the code in the same environment and mocks the interaction with the any other environment.

A more real time example would be where the code has to interact with the payment gateway, for Unit Testing a developer might not want to send requests to the payment gateway.

Actual interactions with the payment gateway might be done during the SIT (System Integrated Testing ) phase.

There are many frameworks for mocking; Mockito and EasyMock are one of the most popular frameworks. I will be using EasyMock in the below example. Mocking is using a fake object during the UT phase. And these frameworks do this by generating proxy objects (java.lang.reflect.Proxy).However, not required to know the details about proxy objects.

I will just be using a simple editor for the example as it will allow better understanding of the implementations which an IDE might be not be able to show.

We will need following jars:

- mysql-connector-java-5.1.6-bin.jar   <Your JDBC driver, incase you don't mock>
- junit-4.11-beta-1.jar
- hamcrest-core-1.3.RC2.jar
- easymock-3.2.jar
- cglib-nodep-2.2.2.jar
- objenesis-1.2.jar

There are 4 java files for this example.

- (POJO Class)
-  <JUnit Test Class>

Here I am using data type as String for rollNo (ideally should be of int type), using it just for the sake of this example.

This is the DAO class which actually hits the database.

Please note there is a SOP on line 6, which prints on the console if the code hits the database. is the class which call the getStudentDetails() method of the class.

Now, is the Test class where the actual action happens. This class has JUnit and EasyMock API.

EasyMock framework creates a proxy object / fake object of the class which we want to mock, and this proxy object can be used to return whatever you want as an return value of any of the methods of this class.

We can compare EasyMock object to a tape.

createMock()  ----     Blank Tape.
expect()            ----     Recording your favourite song.
replay()             ----     Playing your favourite song.
reset()               ----     Erasing the tape,making it blank to record a new song.

In this we are using EasyMock.isA(), but if we expect that a specific input is to be received by the method that mock, then we can set such expectations

This ensures that getStudentDetails() is expected ONLY when the input to the method is "111", for anything else we get an exception.

Finally we run our test class, with the following command.

Please note "Actual DB call will occur......" is not printed on the console, this shows the DB was not hit. There are a lot of methods which serve different purposes, after this example you might want to visit

© Copyright © 2013 All Rights Reserved.

Free Web Hosting