In this post, I will be showing one of the most widely used and popular JUnit Testing Mocking framework – Mockito. Mockito is one of the widely used testing API for Java.

The following configurations are required in order to run the application
JDK 1.7
Have maven installed and configured
Junit, Mockito, PowerMockito dependency in pom.xml
Now we will see the below steps how to create a maven based Java project in Eclipse
Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : junit
Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.

Step 4. Create a Model class

package com.roytuts.model;
public class ActivityModel {
  private String id;
  private String name;
  public ActivityModel() {}
  public ActivityModel(String id, String name) { = id; = name;
  public String getId() {
    return id;
  public void setId(String id) { = id;
  public String getName() {
    return name;
  public void setName(String name) { = name;

Step 5. Create service interface

package com.roytuts.service;
import com.roytuts.model.ActivityModel;
public interface ActivityService {
  public ActivityModel getActivity(final String id);

Step 6. Create dao interface

package com.roytuts.dao;
import com.roytuts.model.ActivityModel;
public interface ActivityDao {
  public ActivityModel getActivity(final String id);

Step 7. Create service implementation class

package com.roytuts.service.impl;
import java.util.UUID;
import com.roytuts.dao.ActivityDao;
import com.roytuts.dao.impl.ActivityDaoImpl;
import com.roytuts.model.ActivityModel;
import com.roytuts.service.ActivityService;
public class ActivityServiceImpl implements ActivityService {
  private ActivityDao activityDao;
  public ActivityServiceImpl() {
    activityDao = new ActivityDaoImpl();
  public ActivityModel getActivity(String id) {
    ActivityModel activity = activityDao.getActivity(id);
    final String newName = getActivityGeneratedName();
    activity = updateActivityName(activity, newName);
    return activity;
  private String getActivityGeneratedName() {
    String newName = "Activity_" + UUID.randomUUID().toString();
    return newName;
  private ActivityModel updateActivityName(ActivityModel activity, String name) {
    ActivityModel activityModel = new ActivityModel();
    return activityModel;

Step 8. Create dao implementation class

package com.roytuts.dao.impl;
import com.roytuts.dao.ActivityDao;
import com.roytuts.model.ActivityModel;
public class ActivityDaoImpl implements ActivityDao {
  public ActivityModel getActivity(String id) {
    // TODO implement logic
    System.out.println("New Activity fetched successfully");
    return null;

Step 9. Create Junit class

package com.roytuts.service.impl.test;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import com.roytuts.dao.ActivityDao;
import com.roytuts.dao.impl.ActivityDaoImpl;
import com.roytuts.model.ActivityModel;
import com.roytuts.service.ActivityService;
import com.roytuts.service.impl.ActivityServiceImpl;
public class ActivityServiceImplTest {
  private ActivityService service;
  private ActivityDao dao;
  private ActivityModel activity;
  public void setUp() throws Exception {
    //below object should be real in order to carry out testing on it
    service = new ActivityServiceImpl();
    //create mock object of DAO instead of using the real object
    dao = Mockito.mock(ActivityDaoImpl.class);
    //create new ActivityModel
    activity = getActivityModel();
    //set mock dao object to activityDao in service implementation class
    Whitebox.setInternalState(service, "activityDao", dao);
  public void tearDown() throws Exception {}
  public void testGetActivity() {
    //return mock activity model object instead of real object from dao
    //call the real service class method
    ActivityModel activityModel = service.getActivity("0001");
    //verify with assert
    assertEquals(activityModel.getId(), activity.getId());
  private ActivityModel getActivityModel() {
    return new ActivityModel("1000", "Activity_TODO");

Step 10. Now run the Junit class. You will see that there is no error.


Leave a Reply

Your email address will not be published. Required fields are marked *