Close

Mockito - Stubbing a method's return Value

[Updated: Apr 22, 2020, Created: Apr 21, 2020]

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

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

Example

Example Application

mockito-defining-return-values-by-method-stubbing/src/main/java/com/logicbig/example/MyService.java

package com.logicbig.example;

public interface MyService {
  public int doSomething();
}

mockito-defining-return-values-by-method-stubbing/src/main/java/com/logicbig/example/MyProcessor.java

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

Stubbing methods

Using when-theReturn

mockito-defining-return-values-by-method-stubbing/src/test/java/com/logicbig/example/ProcessorTest.java

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-consecutive-method-calls-stubbing>mvn test -Dtest="ProcessorTest"
[INFO] Scanning for projects...
[INFO]
[INFO] ---< com.logicbig.example:mockito-consecutive-method-calls-stubbing >---
[INFO] Building mockito-consecutive-method-calls-stubbing 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-consecutive-method-calls-stubbing ---
[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-consecutive-method-calls-stubbing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-consecutive-method-calls-stubbing ---
[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-consecutive-method-calls-stubbing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-consecutive-method-calls-stubbing ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.ProcessorTest
My Integer is: 10
My Integer is: 20
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.678 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.728 s
[INFO] Finished at: 2020-04-21T23:53:38-05:00
[INFO] ------------------------------------------------------------------------

Using doReturn-when

mockito-defining-return-values-by-method-stubbing/src/test/java/com/logicbig/example/ProcessorTest2.java

package com.logicbig.example;

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

public class ProcessorTest2 {

  @Test
  public void processTest() {
      MyService myService = Mockito.mock(MyService.class);
      Mockito.doReturn(10).when(myService.doSomething());
      MyProcessor myProcessor = new MyProcessor(myService);
      String returnedValue = myProcessor.process();
      Assert.assertEquals(returnedValue, "My Integer is: 10");
  }
}
D:\mockito-consecutive-method-calls-stubbing>mvn test -Dtest="ProcessorTest2"
[INFO] Scanning for projects...
[INFO]
[INFO] ---< com.logicbig.example:mockito-consecutive-method-calls-stubbing >---
[INFO] Building mockito-consecutive-method-calls-stubbing 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-consecutive-method-calls-stubbing ---
[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-consecutive-method-calls-stubbing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-consecutive-method-calls-stubbing ---
[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-consecutive-method-calls-stubbing ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-consecutive-method-calls-stubbing ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.ProcessorTest2
My Integer is: 10
My Integer is: 20
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.854 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.589 s
[INFO] Finished at: 2020-04-21T23:54:29-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 - Method Stubbing Example Select All Download
  • mockito-defining-return-values-by-method-stubbing
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • ProcessorTest.java

    See Also