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.

Prerequisites

The following configurations are required in order to run the application

Eclipse
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.

maven-arctype-quickstart

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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.roytuts</groupId>
    <artifactId>junit</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>junit</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.7</jdk.version>
        <junit.version>4.11</junit.version>
        <mockito.version>1.9.5</mockito.version>
        <powermock.version>1.5</powermock.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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) {
    this.id = id;
    this.name = name;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

Step 5. Create service interface

package com.roytuts.service;

import com.roytuts.model.ActivityModel;

public interface ActivityService {

    public void createActivity(final ActivityModel activity);

}

Step 6. Create dao interface

package com.roytuts.dao;

import com.roytuts.model.ActivityModel;

public interface ActivityDao {

    public void createActivity(final ActivityModel activity);

}

Step 7. Create service implementation class

package com.roytuts.service.impl;

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();
    }

    @Override
    public void createActivity(final ActivityModel activity) {
        activityDao.createActivity(activity);
    }

}

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 {

    @Override
    public void createActivity(final ActivityModel activity) {
        // TODO : create new Activity
    }

}

Step 9. Create Junit class

package com.roytuts.service.impl.test;

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;

@RunWith(PowerMockRunner.class)
public class ActivityServiceImplTest {

    private ActivityService service;
    private ActivityDao dao;
    private ActivityModel activity;

    @Before
    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);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testGetActivity() {
        // return mock activity model object instead of real object from dao
        Mockito.doNothing().when(dao).createActivity(activity);
    }

    private ActivityModel getActivityModel() {
        return new ActivityModel("1000", "Activity_TODO");
    }

}

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

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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