Unit test with Mockito

Have you ever tried to build  a Unit Test about servlet, web services or REST services? Whatever is your answer I would like to illustrate how build it with Mockito framework with, as usual, a very easy example.

First, the official referece is available at http://code.google.com/p/mockito/. Once having said that, let’s go to see how it works. Before go on, this is the maven dependecy to include.


Now the simple servlet code

package it.samplemockito;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 * Servlet implementation class OrderProcess
public class OrderProcess extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String loggedUserName = "";
    private boolean isManager = false;

     * @see HttpServlet#HttpServlet()
    public OrderProcess() {
        // TODO Auto-generated constructor stub

	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	java.security.Principal principal = request.getUserPrincipal();

        if( principal != null )


        if (isManager())
        	System.out.println("USER ALLOWED");
        	System.out.println("USER NOT ALLOWED");

	public String getLoggedUserName() {
		return loggedUserName;

	public void setLoggedUserName(String loggedUserName) {
		this.loggedUserName = loggedUserName;

	public boolean isManager() {
		return isManager;

	public void setManager(boolean isManager) {
		this.isManager = isManager;

The above code reads the user name and checks whether the user’s role is Manager.

The most interesting code is the test. Here the code about it.

package it.samplemockito.test;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import it.samplemockito.OrderProcess;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class OrderProcessTest {

 private final String USER = "User";
 private final String ROLE = "Manager";
 private HttpServletRequest request;
 private HttpServletResponse response;
 private Principal principal;
 public void processTest() throws ServletException, IOException
  OrderProcess process = new OrderProcess();
  process.doGet(request, response);
  assertEquals(process.getLoggedUserName(), USER);

The objects request, response and principal are built by Mockito. Their properties are set using the command when…theReturn().

So, I set principal name, and the user role as “Manager”.

That’s good enough for my unit test!


2 thoughts on “Unit test with Mockito

  1. Above code doesnt work . We need to add the below lines,else request ,response and principal will be null.

    public void initMocks() {

    1. I’m sorry to read about it but it’s working for me.
      I deployed the servlet on Tomcat 7 and run JUnit from Eclipse as JUnit test.
      Let me know if you’d like to receive the Eclipse project in zip format to check any different configuration.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.