How do you verify interactions with mocked objects in Mockito?

Table of Contents

Introduction

When writing unit tests using Mockito, it's important to not only define the behavior of mock objects but also verify that the interactions with these mock objects are as expected. Verification ensures that the correct methods are called with the correct parameters, and that the interactions follow the expected flow.

Mockito provides a simple yet powerful mechanism to verify interactions with mocked objects using the verify() method. In this guide, we will walk through how to verify interactions with mocked objects in Mockito, ensuring that your tests are robust and accurate.

Verifying Interactions with Mocks in Mockito

1. The verify() Method

Mockito’s verify() method is used to check if a particular method was called on a mock object during the execution of the test. The verify() method takes the mock object and a verification mode as arguments. This helps you confirm that the correct methods were called and how many times they were invoked.

Syntax:

  • mockObject: The mock object on which you want to verify the interaction.
  • verificationMode: Defines how many times the method should be called (e.g., times(1), never(), etc.).
  • methodCall(): The actual method you want to verify.

2. Verifying Method Calls

Example: Verifying a Single Method Call

Let’s consider a simple example where we want to verify that a method is called exactly once on a mocked object.

In this example:

  • We mock the UserRepository and create a UserService instance using the mock.
  • The createUser() method on the UserService class calls the save() method on the mock repository.
  • The verify(mockRepo, times(1)).save(any(User.class)) statement ensures that the save() method was called exactly once on the mock with any User object as a parameter.

Example: Verifying Method Call with Specific Arguments

You can also verify that a method was called with specific arguments.

In this example:

  • The verify(mockRepo).findById(1) ensures that findById(1) was called on the mock repository.

3. Verifying the Number of Method Calls

Mockito allows you to verify that a method is called a specific number of times. This is done by using different verification modes like times(), never(), atLeast(), or atMost().

Example: Verifying Method Call Count

In this example:

  • verify(mockRepo, times(2)).save(any(User.class)) ensures that the save() method was called exactly twice.

4. Verifying No Interaction with Mocked Objects

You can verify that a mock object was never interacted with during the test using the never() verification mode.

Example: Verifying No Interaction

In this example:

  • verify(mockRepo, never()).save(any(User.class)) ensures that the save() method was never called on the mock.

5. Verifying Method Calls with Arguments Matchers

Mockito provides argument matchers, which allow you to verify method calls with flexible arguments. Common argument matchers include any(), eq(), and capture().

Example: Verifying Method Call with Argument Matcher

In this example:

  • argThat(user -> "John Doe".equals(user.getName())) is used to verify that the save() method was called with a User object whose name is "John Doe".

6. Verifying Void Methods

Mockito also allows verification for void methods, which do not return a value. You can use verify() to ensure a void method was called.

Example: Verifying Void Method Call

In this example:

  • verify(mockRepo, times(1)).deleteById(1) ensures that the deleteById(1) method was called exactly once.

Conclusion

Verifying interactions with mock objects in Mockito is essential for ensuring that the correct methods are called during the execution of your unit tests. The verify() method provides an easy way to check if methods were called with the correct arguments, how many times they were called, and whether there were any unwanted interactions with the mocks.

By using verification effectively, you can ensure that your code behaves as expected and adheres to the correct interaction patterns. Whether you're verifying the number of method calls, specific arguments, or even void method invocations, Mockito's verification tools provide the necessary functionality to make your unit tests comprehensive and reliable.

Similar Questions