Close

Mockito - Verifying Multiple Number of Method Invocations

[Last Updated: Jul 23, 2020]

By default, Mockito.varify() confirms that the target method was called only once. We can verify any number of invocations by using following methods of Mockito class:

 public static <T> T verify(T mock, VerificationMode mode) 
public static VerificationMode times(int wantedNumberOfInvocations) 
public static VerificationMode never()
public static VerificationMode atLeastOnce()
public static VerificationMode atLeast(int minNumberOfInvocations) 
public static VerificationMode atMostOnce()
public static VerificationMode atMost(int maxNumberOfInvocations)

Example

Example project

package com.logicbig.example;

public interface MyService {
  public void doSomething(int x);
}
package com.logicbig.example;

public class MyProcessor {
  private MyService myService;

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

  public void process(int n) {
      myService.doSomething(n);
      System.out.println("Processed: " + n);
  }
}

Test to verify various or no interaction

package com.logicbig.example;

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

public class ProcessorTest {

  @Test
  public void processTest() {
      MyService myService = Mockito.mock(MyService.class);
      MyProcessor myProcessor = new MyProcessor(myService);
      myProcessor.process(0);
      myProcessor.process(1);
      myProcessor.process(2);
      myProcessor.process(2);
      myProcessor.process(2);

      Mockito.verify(myService, Mockito.atMostOnce()).doSomething(0);
      Mockito.verify(myService, Mockito.atMostOnce()).doSomething(1);
      Mockito.verify(myService, Mockito.atMost(3)).doSomething(2);
      Mockito.verify(myService, Mockito.atLeastOnce()).doSomething(1);
      Mockito.verify(myService, Mockito.atLeast(2)).doSomething(2);
      Mockito.verify(myService, Mockito.times(3)).doSomething(2);
      Mockito.verify(myService, Mockito.never()).doSomething(3);

  }
}
D:\mockito-verify-method-calls-for-varing-invocations>mvn test -Dtest=ProcessorTest
[INFO] Scanning for projects...
[INFO]
[INFO] --< com.logicbig.example:mockito-verify-method-calls-for-varing-invocations >--
[INFO] Building mockito-verify-method-calls-for-varing-invocations 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-verify-method-calls-for-varing-invocations ---
[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-verify-method-calls-for-varing-invocations ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-verify-method-calls-for-varing-invocations ---
[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-verify-method-calls-for-varing-invocations ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-verify-method-calls-for-varing-invocations ---

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

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.205 s
[INFO] Finished at: 2020-07-23T01:43:02-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

Verifying various number of invocations Select All Download
  • mockito-verify-method-calls-for-varing-invocations
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • ProcessorTest.java

    See Also