Introduction

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

We use doNothing() method from Mockito framework when we write Junit test case on void method.

Prerequisites

The following configurations are required in order to run the application Eclipse, Java at least 1.7, Maven 3.3, Junit, Mockito, PowerMocito

Creating Project

Now we will see the below steps how to create a maven based Java project in Eclipse

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 archtype – maven-arctype-quickstart and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below:

Group Id : com.roytuts
Artifact Id : junit

Updating Build File

Modify the pom.xml file as shown below. We need to add required dependency for Junit, Mockito and PowerMockito framework.

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

Creating Model Class

Create a below model class that will be used finally in DAO layer to query database or persistence layer.

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;
  }
  
  //getters and setters
}

Creating Service and DAO Interface

Create service interface with below code.

package com.roytuts.service;

import com.roytuts.model.ActivityModel;

public interface ActivityService {
    public void createActivity(final ActivityModel activity);
}

Create dao interface with the following code.

package com.roytuts.dao;

import com.roytuts.model.ActivityModel;

public interface ActivityDao {
    public void createActivity(final ActivityModel activity);
}

Creating Service and DAO Implementation

Create service implementation class though this class does not do any real business activity.

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

Create dao implementation class with the below source code and this class also does not do any real activity.

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
    }
	
}

Creating Junit Class

Create Junit class to test our service class. Here we will see how to use doNothing() method from Mockito framework.

We are using doNothing() method on void method only because void method does not return anything.

If you want to verify whether your void method was tested or executed in test case then you can check tutorial on Mockito verify() example.

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

Testing Junit

Now run the above Junit class.

Tags:

Leave a Reply

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