Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can verifyZeroInteractions be used with all types of mocked objects


Can verifyZeroInteractions be used with all types of mocked objects


`verifyZeroInteractions` is a powerful tool in the Mockito framework used to assert that no method calls have been made on a mocked object. It is designed to ensure that certain interactions do not occur during a test, which is crucial for maintaining the purity and reliability of unit tests. However, its applicability depends on the type of mocked object and the context of the test.

Applicability to Mocked Objects

`verifyZeroInteractions` can be used with any type of mocked object that is created using Mockito's mocking capabilities. This includes mocks of interfaces, classes, and even abstract classes. The key requirement is that the object must be a Mockito mock, meaning it has been created using `mock()`, `@Mock`, or another Mockito mechanism.

For example, if you have a class `Api` that you want to mock, you can create a mock of it and use `verifyZeroInteractions` to ensure no methods are called on it during a test:

dart
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

class MockApi extends Mock implements Api {}

void main() {
  group('verifyZeroInteractions example', () {
    test('should verify no interactions with the API', () {
      // Arrange
      final api = MockApi();

      // Act
      // ... (code that should not interact with the API)

      // Assert
      verifyZeroInteractions(api);
    });
  });
}

Limitations and Considerations

While `verifyZeroInteractions` is versatile, it should not be used as a blanket assertion in every test. Overusing this method can lead to brittle tests that fail unnecessarily when minor, harmless changes are made to the system. It is most effective when you need to ensure that a component does not trigger any side effects or communicate with other parts of the system during certain operations.

Moreover, `verifyZeroInteractions` is similar to `verifyNoMoreInteractions` in that both ensure no additional interactions occur beyond what has been explicitly verified. However, `verifyNoMoreInteractions` is typically used after specific method calls have been verified, whereas `verifyZeroInteractions` asserts that no interactions have occurred at all.

Best Practices

To effectively use `verifyZeroInteractions`, follow these guidelines:

- Use it judiciously: Only apply `verifyZeroInteractions` when it is crucial to ensure no interactions occur. This helps maintain test reliability and avoids unnecessary failures.
- Understand the test context: Ensure that the test setup and the code under test align with the expectation that no interactions should occur.
- Combine with other verification methods: Use `verifyZeroInteractions` alongside other Mockito verification methods to create comprehensive tests that cover various scenarios.

In summary, `verifyZeroInteractions` is a valuable tool for ensuring that mocked objects do not receive any method calls during a test. It can be used with any type of Mockito mock but should be applied thoughtfully to maintain robust and meaningful tests.

Citations:
[1] https://www.dhiwise.com/post/how-to-use-verifyzerointeractions-for-better-flutter-tests
[2] https://javadoc.io/doc/org.mockito/mockito-core/1.10.16/org/mockito/Mockito.html
[3] https://stackoverflow.com/questions/55825251/is-there-an-equivalent-of-verifyzerointeractions-for-verifying-an-exact-number
[4] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.Mockito.verifyZeroInteractions
[5] https://www.baeldung.com/mockito-verify
[6] https://groups.google.com/g/mockito/c/_4j0yF4As1A
[7] https://mincong.io/2019/09/22/mockito-verify/
[8] https://www.javacodegeeks.com/2015/11/mockito-verification.html
[9] https://www.digitalocean.com/community/tutorials/mockito-verify