Click here to Skip to main content
Click here to Skip to main content

Tagged as

Mockito- a great mock framework for Java development.

, 27 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with clean & simple API.


This artical will show some basic concepts of mocking framework, why we should use it and walkthrough a simple that applied Mockito in Java.

The concept of mocking  

Outside of the world of software development, the term “mock” means to imitate or to mimic. A “mock” can therefore be thought of as a stand-in, an imposter or as most commonly referred to as it pertains to softwaredevelopment, a fake.

Fakes are often used as stand-ins for dependencies of the class under test. 

Terms & Definitions  
Dependency – A dependency is when one class in an application depends upon another in order to perform its intended function. Dependencies are often stored in instance variables inside of the dependent class. 
Class Under Test – When writing unit tests, the term “unit” generally refers to a single class and specifically the class against which the tests are written. The class under test is therefore the application class that is being tested. 

Why Mock?  

When we learn to program, our objects are usually self contained. Any hello world has no dependencies on outside classes (System.out aside) and neither do many of the other classes we write in the process of learning a language. However, in the real world, software has dependencies. We have action classes that depend on services and services that depend on data access objects (DAOs) and the list goes on.


The idea of unit testing is that we want to test our code without testing the dependencies. This test allows you to verify that the code being tested works, regardless of it's dependencies. The theory is that if the code I write works as designed and my dependencies work as designed, then they should work together as designed. The code below would be an example of this:  

import java.util.ArrayList;
public class Counter {
     public Counter() {
     public int count(ArrayList items) {
          int results = 0;
          for(Object curItem : items) {
               results ++;
          return results;

I know the above example is about as simple as you get, but it illustrates the point. If you wanted to test the method count, you would write at test that addressed how the count method works. You aren't trying to test that ArrayList works because you assume that it has been tested and works as designed. Your only goal is to test your use of ArrayList.  

The concept behind mock objects is that we want to create an object that will take the place of the real object. This mock object will expect a certain method to be called with certain parameters and when that happens, it will return an expected result. 

What are the key mocking concepts?

When it comes to mocking, there are only 3 things you really need to worry about; stubbing, setting expectations and verifying. Some unit test scenarios don’t involve any of these, others involve only stubbing and others involve setting expectations and verifying.


Stubbing is the process of telling your fake how to behave when it is interacted with. You can generally stub public properties (those with getters and/or setters) and public functions.

When it comes to stubbing functions, you have a lot of choices typically. You may wish to return a specific value, throw an error or dispatch an event. Further, you may wish to indicate that the function behave differently depending upon how it is invoked (i.e. by matching the types or values of the parameters passed to the function).

If this sounds like a lot of work, it can be, but it generally isn’t. One great feature of many of the mocking frameworks is that you need not stub void functions. Nor do you have to stub any functions that are not invoked or properties that are not consulted during the execution of your tests.

Setting expectations 

One of the key features of a fake is the ability to tell the fake what you expect when your test runs. For example, you may expect that a specific function be invoked exactly 3 times. You may expect that it never be invoked. You may expect that it be invoked at least twice, but not more than 5 times. You may expect that it be invoked with specific types of arguments or specific values or any combination of the above. The possibilities are endless. 

Setting expectations is the process of telling your fake what you expect to happen to it. Remember that since it’s a fake, nothing actually happens. But, your class under test is none the wiser. From its perspective, it invoked the function and expects that it did whatever it was supposed to do. 

For what it’s worth, most mocking frameworks let you create mocks of interfaces or public classes. You are not limited to having to mock only interfaces. 


Setting expectations and verification go hand in hand. Setting expectations is done prior to invoking the function(s) on the class under test. Verification is done after. So, first you set expectations, then you verify that your expectations were met.

From a unit testing perspective, if your expectations were not met, the unit test fails. For example, if you set the expectation that the ILoginService.login function should be invoked exactly once with a specific username and password, but it was never invoked during the execution of your test, then the fake would not verify and the test should fail. 

What are the benefits of mocking? 

   You can create tests in advance; TDD  

        This one is one of the stronger benefits. If you create a Mock you can write Service Tests before the service is created, giving you the ability to add the tests to your automation environment in the development process. In other words, Service Mocking gives you the ability to use Test Driven Development. 

   Teams can work in parallel 

        This is similar to the above; create tests for code that does not exist. But the previous point was for the developer who writes tests, this one is for the Testing Team. How does the team start creating tests when you don’t have anything to test? Mock it and write tests against the Mock! This means it’s actually possible for the QA Team to have a full suite of tests ready when a service is ready to be tested; we don’t have downtimes when one team waits for the other to finish. This makes the financial argument for mocking particularly strong. 

   You can create proof of concepts or demos. 

        Since Mocks can (done levelheadedly) be very cost efficient to make, Mocks can be used to create a proof of concept, as a wireframe, or as a demo for something you’re considering building. This is very powerful and gives a good ground for making decisions about both whether to go ahead with a development project or not, but most importantly for actual design decisions. 

  You can write test for resource not accessible 

      This is one of those benefits that doesn’t fall into the actual benefits category, but rather works as a life saver. Ever wanted to test or use a service, only to get told that the service is behind a firewall and that that firewall can’t be opened for you or that you’re an authorized to use it? When you do, a MockService placed in an accessible place, including on your local computer, is a life saver. 

  Mock can be delivered to the customer 

      There are cases where there are reasons why you can’t allow access to your test systems for external sources, like partners or customers. These reasons can be access security, sensitivity of information, or just the fact that the test environment might not be accessible 24/7. In these cases; how do you provide a test system for your partner or customer to start development or test against? An easy solution is to provide a mock, either from your network or from your customers own network. A soapUI mock is extremely easy to deploy, it can either run in soapUI or exported as a .WAR file and placed in your java server of choice. 

  You can isolate systems 

      Sometimes you would like to test a part of your system without having other system parts affect it. This because the other systems will add noise to the test data and make it harder to make good conclusions from the data collected. Using mocks you can remove all dependencies mocking all systems except the one system you need to pinpoint in your tests. When doing mocks for isolation these mocks can be made extremely simple but reliable, fast, and predictable. This gives you a test environment where you have removed all stochastic behavior, have repeatable patterns and can monitor specific systems well. 

Mockito framework

Mockito is an open source testing framework for Java released under the MIT License. 
Mockito distinguishes itself from other mocking frameworks by allowing developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand.[4] One of the criticisms of mock objects is that there is a tighter coupling of the test code to the system under test.[5] Since Mockito attempts to eliminate the expect-run-verify pattern[6] by removing the specification of expectations, the coupling is reduced or minimized. The result of this distinguishing feature is simpler test code that should be easier to read and modify. 

You can verify interactions:  

// mock creation
List mockedList = mock(List.class);
</span>// using mock object
// selective and explicit vertification

Or stub method calls

// you can mock concrete class, not only interfaces
LinkedList mockedList = mock(LinkedList.class);
</span>// stubbing - before execution
</span>// following prints "first"
</span>// following prints "null" because get(999) was not stubbed.

A simple example Java code used Mockito 

                                                            Without Mock framework


                                                           With Mockito framework 

Step 1:  Create a Maven project in Eclipse 

 Define the pom.xml as below : 

<pre><project xmlns="" 

Step 2:  Add java source code  


 * @author Phat (Phillip) H. VU <>
public class Person
    private final Integer personID;
    private final String personName;
    public Person( Integer personID, String personName )
        this.personID = personID;
        this.personName = personName;
    public Integer getPersonID()
        return personID;
    public String getPersonName()
        return personName;


 * @author Phat (Phillip) H. VU <>
public interface PersonDao
    public Person fetchPerson( Integer personID );
    public void update( Person person );


 * @author Phat (Phillip) H. VU <>
public class PersonService
    private final PersonDao personDao;
    public PersonService( PersonDao personDao )
        this.personDao = personDao;
    public boolean update( Integer personId, String name )
        Person person = personDao.fetchPerson( personId );
        if( person != null )
            Person updatedPerson = new Person( person.getPersonID(), name );
            personDao.update( updatedPerson );
            return true;
            return false;


Step 3:  Added unit-test class.  

Then, jump to write unit test cases for class 

In assumption, class is as below:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
 * @author Phat (Phillip) H. VU <>
public class PersonServiceTest
    private PersonDao personDAO;
    private PersonService personService;
    public void setUp()
        throws Exception
        MockitoAnnotations.initMocks( this );
        personService = new PersonService( personDAO );
    public void shouldUpdatePersonName()
        Person person = new Person( 1, "Phillip" );
        when( personDAO.fetchPerson( 1 ) ).thenReturn( person );
        boolean updated = personService.update( 1, "David" );
        assertTrue( updated );
        verify( personDAO ).fetchPerson( 1 );
        ArgumentCaptor<Person> personCaptor = ArgumentCaptor.forClass( Person.class );
        verify( personDAO ).update( personCaptor.capture() );
        Person updatedPerson = personCaptor.getValue();
        assertEquals( "David", updatedPerson.getPersonName() );
        // asserts that during the test, there are no other calls to the mock object.
        verifyNoMoreInteractions( personDAO );
    public void shouldNotUpdateIfPersonNotFound()
        when( personDAO.fetchPerson( 1 ) ).thenReturn( null );
        boolean updated = personService.update( 1, "David" );
        assertFalse( updated );
        verify( personDAO ).fetchPerson( 1 );
        verifyZeroInteractions( personDAO );
        verifyNoMoreInteractions( personDAO );

Points of Interest 

 + What's Mock framework in overview.

 + Why we use Mockito in testing in Java development. 



First release on Dec 26, 2012




About the Author

Comments and Discussions

GeneralExcellent explanation Pinmembertruongnguyen161024-Nov-13 5:50 
GeneralMy vote of 5 Pinmemberhungvo353@yahoo.com11-Jan-13 3:41 
GeneralRe: My vote of 5 PinmemberPhat (Phillip) H. VU11-Jan-13 3:55 
GeneralMy vote of 5 Pinmemberphuchoang808@yahoo.com10-Jan-13 22:21 
GeneralRe: My vote of 5 PinmemberPhat (Phillip) H. VU10-Jan-13 22:33 
GeneralMy vote of 5 PinmemberDan Phillip3-Jan-13 16:13 
GeneralMy vote of 5 PinmemberThong Nguyen883-Jan-13 15:14 
GeneralMy vote of 5 PinmemberMember 97262843-Jan-13 4:53 
GeneralMy vote of 5 PinmemberPhat (Phillip) H. VU29-Dec-12 22:23 



| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150128.1 | Last Updated 27 Dec 2012
Article Copyright 2012 by Phat (Phillip) H. VU
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid