Close

Mockito - verifyNoMoreInteractions() and verifyNoInteractions()

[Last Updated: Aug 11, 2020]

Mockito.verifyNoMoreInteractions()

public static void verifyNoMoreInteractions(Object... mocks)

Checks if any of given mocks has any unverified interaction.

We can use this method after calling verify() methods. It is to make sure that no interaction is left for verification.

Mockito.verifyNoInteractions()

public static void verifyNoInteractions(Object... mocks)

This method verifies that no interactions happened on given mocks.

Examples

Example Application

package com.logicbig.example;

public interface MyService {
  void doSomething();

  void doSomething2();
}
package com.logicbig.example;

public class MyProcessor {
  private MyService myService;

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

  public void process() {
      myService.doSomething();
  }

  public void process2() {
      myService.doSomething2();
  }
}

Using Mockito.verifyNoMoreInteractions()

package com.logicbig.example;

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

public class ProcessorTest {

  @Test
  public void processTest() {
      MyService myMockService = Mockito.mock(MyService.class);
      MyProcessor myProcessor = new MyProcessor(myMockService);
      myProcessor.process();
      myProcessor.process2();

      Mockito.verify(myMockService).doSomething();
      Mockito.verify(myMockService).doSomething2();

      Mockito.verifyNoMoreInteractions(myMockService);
  }
}
D:\mockito-verify-no-more-interactions>mvn test -Dtest=ProcessorTest
[INFO] Scanning for projects...
[INFO]
[INFO] ------< com.logicbig.example:mockito-verify-no-more-interactions >------
[INFO] Building mockito-verify-no-more-interactions 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-verify-no-more-interactions ---

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

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.828 s
[INFO] Finished at: 2020-07-30T00:53:17-05:00
[INFO] ------------------------------------------------------------------------

In case if we don't verify all interactions:

package com.logicbig.example;

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

public class ProcessorTest2 {

  @Test
  public void processTest() {
      MyService myMockService = Mockito.mock(MyService.class);
      MyProcessor myProcessor = new MyProcessor(myMockService);
      myProcessor.process();
      myProcessor.process2();

      Mockito.verify(myMockService).doSomething();

      Mockito.verifyNoMoreInteractions(myMockService);
  }
}
D:\mockito-verify-no-more-interactions>mvn test -Dtest=ProcessorTest2
[INFO] Scanning for projects...
[INFO]
[INFO] ------< com.logicbig.example:mockito-verify-no-more-interactions >------
[INFO] Building mockito-verify-no-more-interactions 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-verify-no-more-interactions ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.ProcessorTest2
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.755 sec <<< FAILURE!
processTest(com.logicbig.example.ProcessorTest2) Time elapsed: 0.663 sec <<< FAILURE!
org.mockito.exceptions.verification.NoInteractionsWanted:
No interactions wanted here:
-> at com.logicbig.example.ProcessorTest2.processTest(ProcessorTest2.java:17)
But found this interaction on mock 'myService':
-> at com.logicbig.example.MyProcessor.process2(MyProcessor.java:15)
***
For your reference, here is the list of all invocations ([?] - means unverified).
1. -> at com.logicbig.example.MyProcessor.process(MyProcessor.java:11)
2. [?]-> at com.logicbig.example.MyProcessor.process2(MyProcessor.java:15)

at com.logicbig.example.ProcessorTest2.processTest(ProcessorTest2.java:17)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Failed tests: processTest(com.logicbig.example.ProcessorTest2): (..)

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.720 s
[INFO] Finished at: 2020-07-30T00:58:10-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project mockito-verify-no-more-interactions: There are test failures.
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Using Mockito.verifyNoInteractions()

package com.logicbig.example;

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

public class ProcessorTest3 {

  @Test
  public void processTest() {
      MyService myMockService = Mockito.mock(MyService.class);
      MyProcessor myProcessor = new MyProcessor(myMockService);

      Mockito.verifyNoInteractions(myMockService);
  }
}
D:\mockito-verify-no-more-interactions>mvn test -Dtest=ProcessorTest3
[INFO] Scanning for projects...
[INFO]
[INFO] ------< com.logicbig.example:mockito-verify-no-more-interactions >------
[INFO] Building mockito-verify-no-more-interactions 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-verify-no-more-interactions ---

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

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.775 s
[INFO] Finished at: 2020-07-30T01:04:12-05:00
[INFO] ------------------------------------------------------------------------

In case if some interaction has taken placed before calling verifyNoInteractions()

package com.logicbig.example;

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

public class ProcessorTest4 {

  @Test
  public void processTest() {
      MyService myMockService = Mockito.mock(MyService.class);
      MyProcessor myProcessor = new MyProcessor(myMockService);
      myProcessor.process();
      Mockito.verifyNoInteractions(myMockService);
  }
}
D:\mockito-verify-no-more-interactions>mvn test -Dtest=ProcessorTest4
[INFO] Scanning for projects...
[INFO]
[INFO] ------< com.logicbig.example:mockito-verify-no-more-interactions >------
[INFO] Building mockito-verify-no-more-interactions 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mockito-verify-no-more-interactions ---
[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-no-more-interactions ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mockito-verify-no-more-interactions ---

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.ProcessorTest4
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.749 sec <<< FAILURE!
processTest(com.logicbig.example.ProcessorTest4) Time elapsed: 0.663 sec <<< FAILURE!
org.mockito.exceptions.verification.NoInteractionsWanted:
No interactions wanted here:
-> at com.logicbig.example.ProcessorTest4.processTest(ProcessorTest4.java:13)
But found these interactions on mock 'myService':
-> at com.logicbig.example.MyProcessor.process(MyProcessor.java:11)
Actually, above is the only interaction with this mock.
at com.logicbig.example.ProcessorTest4.processTest(ProcessorTest4.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Failed tests: processTest(com.logicbig.example.ProcessorTest4): (..)

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.705 s
[INFO] Finished at: 2020-07-30T01:06:23-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project mockito-verify-no-more-interactions: There are test failures.
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

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 - verifyNoMoreInteractions() and verifyNoInteractions() Examples Select All Download
  • mockito-verify-no-more-interactions
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • ProcessorTest.java

    See Also