Close

Getting Started with Mockito

[Updated: Apr 17, 2020, Created: Apr 16, 2020]

Mockito is an open source testing framework which allows to create mock objects.

Mockito can be used to create mock for an interface or a class.

A mock can be thought as a dummy implementation of an interface or a class which is used to define/change its behavior.

Examples

pom.xml

<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-core</artifactId>
   <version>3.3.3</version>
</dependency>

An example interface

package com.logicbig.example;

public interface MyService {
  public int doSomething();
}

Mocking the interface in a test

package com.logicbig.example;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class MyServiceTest {

  @Test
  public void testDoSomething(){
      MyService mock = Mockito.mock(MyService.class);
      int i = mock.doSomething();
      Assert.assertEquals(i, 0);
  }
}
D:\mockito-getting-started-creating-mocks>mvn test -Dtest="MyServiceTest"
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:mockito-getting-started-creating-mocks >-----
[INFO] Building mockito-getting-started-creating-mocks 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-getting-started-creating-mocks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ mockito-getting-started-creating-mocks ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-getting-started-creating-mocks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ mockito-getting-started-creating-mocks ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-getting-started-creating-mocks ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyServiceTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.913 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.188 s
[INFO] Finished at: 2020-04-17T00:11:00-05:00
[INFO] ------------------------------------------------------------------------
By default a mock's method returns either null, a primitive/primitive wrapper value, or an empty collection.
For example 0 for an int/Integer and false for a boolean/Boolean.

Stubbing methods

Subbing is defining of what values to return when we invoke methods of a mock object.

Stubbing is achieved by Mockito's when-thenReturn methods.

Let's see how to do stubbing for our interface MyInterface:

package com.logicbig.example;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class MyServiceTest2 {

  @Test
  public void testDoSomething2(){
      MyService mock = Mockito.mock(MyService.class);
      Mockito.when(mock.doSomething()).thenReturn(10);//stubbing
      int i = mock.doSomething();
      Assert.assertEquals(i, 10);
  }
}
D:\mockito-getting-started-creating-mocks>mvn test -Dtest="MyServiceTest2"
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:mockito-getting-started-creating-mocks >-----
[INFO] Building mockito-getting-started-creating-mocks 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-getting-started-creating-mocks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ mockito-getting-started-creating-mocks ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-getting-started-creating-mocks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ mockito-getting-started-creating-mocks ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-getting-started-creating-mocks ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyServiceTest2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.916 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.132 s
[INFO] Finished at: 2020-04-17T00:10:42-05:00
[INFO] ------------------------------------------------------------------------

A more meaningful example

Following is another application class which uses MyService instance:

package com.logicbig.example;

public class MyProcessor {
  private MyService myService;

  public MyProcessor(MyService myService) {
      this.myService = myService;
  }

  public String process() {
      int returnInteger = myService.doSomething();
      return String.format("My Integer is: " + returnInteger);
  }
}

Let's mock MyService again to test MyProcessor#process() method:

package com.logicbig.example;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class ProcessorTest {

  @Test
  public void processTest() {
      MyService myService = Mockito.mock(MyService.class);
      Mockito.when(myService.doSomething()).thenReturn(10);
      MyProcessor myProcessor = new MyProcessor(myService);
      String returnedValue = myProcessor.process();
      Assert.assertEquals(returnedValue, "My Integer is: 10");
  }
}
D:\mockito-getting-started-creating-mocks>mvn test -Dtest="ProcessorTest"
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:mockito-getting-started-creating-mocks >-----
[INFO] Building mockito-getting-started-creating-mocks 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-getting-started-creating-mocks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ mockito-getting-started-creating-mocks ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-getting-started-creating-mocks ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ mockito-getting-started-creating-mocks ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-getting-started-creating-mocks ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.ProcessorTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.694 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.798 s
[INFO] Finished at: 2020-04-17T00:20:03-05:00
[INFO] ------------------------------------------------------------------------

Example Project

Dependencies and Technologies Used:

  • mockito-core 3.3.3: Mockito mock objects library core API and implementation.
  • junit 4.13: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 8
  • Maven 3.5.4

Mockito - Getting started with Mockito Select All Download
  • mockito-getting-started-creating-mocks
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • ProcessorTest.java

    See Also