Close

Mockito - BDD Style Verification using then() - should()

[Last Updated: Aug 28, 2020]

BDDMockito provides then() and should() methods to verify behavior.

Let's rewrite our old verifying behavior example.

Example

Example application

package com.logicbig.example;

public interface MyService {
  public int doSomething(String processName);
}
package com.logicbig.example;

public class MyProcessor {
  private String processName;
  private MyService myService;

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

  public void process() {
      int returnInteger = processName != null ? myService.doSomething(processName) : -1;
      System.out.println("My Integer is: " + returnInteger);
  }
}

Using BDD style verification using then() and should()

package com.logicbig.example;

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);
      String processName = "dummy-process-name";
      BDDMockito.given(myService.doSomething(processName)).willReturn(10);
      MyProcessor myProcessor = new MyProcessor(processName, myService);
      myProcessor.process();
      BDDMockito.then(myService).should().doSomething(processName);
  }
}
D:\mockito-bdd-style-behavior-verification>mvn test -Dtest="ProcessorTest"
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:mockito-bdd-style-behavior-verification >----
[INFO] Building mockito-bdd-style-behavior-verification 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-bdd-style-behavior-verification ---
[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-bdd-style-behavior-verification ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-bdd-style-behavior-verification ---
[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-bdd-style-behavior-verification ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-bdd-style-behavior-verification ---

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

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.356 s
[INFO] Finished at: 2020-08-28T00:24:09-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

BDD Style Behavior Verification Example Select All Download
  • mockito-bdd-style-behavior-verification
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • ProcessorTest.java

    See Also