Close

Mockito - BDD Style Stubbing with given() - willReturn()

[Last Updated: Aug 28, 2020]

BDDMockito class provides Behavior Driven Development style (BDD) methods to support given- when - then syntax.

Let's rewrite our previous stubbing example by using the methods BDDMockito.given() and BDDMockito.willReturn() methods.

Example

Example application

package com.logicbig.example;

public interface MyService {
   int doSomething();
}
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);
  }
}

Write tests using BDDMockito given() and willReturn() methods

package com.logicbig.example;

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

public class ProcessorTest {

  @Test
  public void processTest() {
      MyService myService = Mockito.mock(MyService.class);
      BDDMockito.given(myService.doSomething()).willReturn(10);
      MyProcessor myProcessor = new MyProcessor(myService);
      String returnedValue = myProcessor.process();
      Assert.assertEquals(returnedValue, "My Integer is: 10");
  }
}
D:\mockito-stubbing-by-using-bdd-style-given>mvn test -Dtest="ProcessorTest"
[INFO] Scanning for projects...
[INFO]
[INFO] ---< com.logicbig.example:mockito-stubbing-by-using-bdd-style-given >---
[INFO] Building mockito-stubbing-by-using-bdd-style-given 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-stubbing-by-using-bdd-style-given ---
[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-stubbing-by-using-bdd-style-given ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-stubbing-by-using-bdd-style-given ---
[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-stubbing-by-using-bdd-style-given ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-stubbing-by-using-bdd-style-given ---

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

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.624 s
[INFO] Finished at: 2020-08-28T00:14:08-05:00
[INFO] ------------------------------------------------------------------------

Alternatively we can also write above test as:

package com.logicbig.example;

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

public class ProcessorTest2 {

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

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

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.745 s
[INFO] Finished at: 2020-08-28T00:15:43-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.
  • BDD style stubbing Example
  • JDK 8
  • Maven 3.5.4

BDD Style Stubbing with given() - willReturn() Select All Download
  • mockito-stubbing-by-using-bdd-style-given
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • ProcessorTest.java

    See Also