Using Mockito's verify() method to assert method calls and argument matching

Introduction

Mockito is a popular Java testing framework that allows developers to create mock objects for unit testing. One of the most useful features provided by Mockito is the verify() method, which allows developers to assert method calls and perform argument matching. This article will explore the usage of Mockito�s verify() method and explain how it can be used effectively in unit testing scenarios.

The verify() method

The verify() method is a powerful tool provided by Mockito to verify that certain method calls have been made on a mock object. It allows developers to assert if specific methods were called, how many times, and with specific arguments.

The basic syntax of the verify() method is as follows: java verify(mockObject, verificationMode).methodName(expectedArguments); Here, mockObject is the mock object that we want to verify method calls on, verificationMode specifies how many times the method should have been called (e.g., times(2)), methodName is the name of the method to verify, and expectedArguments are the arguments that the method should have been called with.

Verifying method calls

Let�s say we have a simple UserService class that has a method saveUser(User user) which saves a user object to a database. To test this method, we can create a mock object of this class using Mockito and then use the verify() method to assert if the saveUser() method is called.

// Creating a mock object
UserService userService = Mockito.mock(UserService.class);

// Calling the method to be tested
userService.saveUser(new User("John Doe"));

// Verifying the method call
verify(userService).saveUser(any(User.class));

In the above example, the verify() method is used to ensure that the saveUser() method is called on the userService mock object. The any() method is used as an argument matcher to match any User object passed to the method.

Verifying method calls with specific arguments

Sometimes, we may need to verify if a method is called with specific arguments. Mockito allows us to do this by passing the expected arguments to the verify() method.

// Creating a mock object
UserService userService = Mockito.mock(UserService.class);

// Calling the method to be tested
userService.saveUser(new User("John Doe"));

// Verifying the method call with specific argument
verify(userService).saveUser(argThat(user -> user.getName().equals("John Doe")));

In the above example, the argThat() method is used as an argument matcher to match the expected argument. Here, we verify that the saveUser() method is called with a User object that has the name "John Doe".

Verifying method calls with specific argument matching

Mockito also provides several argument matchers that allow for more complex argument matching.

// Creating a mock object
UserService userService = Mockito.mock(UserService.class);

// Calling the method to be tested
userService.updateUser(new User("John Doe"), 25);

// Verifying the method call with specific argument matching
verify(userService).updateUser(
    argThat(user -> user.getName().equals("John Doe")),
    argThat(age -> age > 20 && age < 30)
);

In the above example, we use the argThat() method as argument matchers for both the User object and the age argument. Here, we verify that the updateUser() method is called with a User object having the name "John Doe" and an age between 20 and 30.

Verifying method calls with specified number of invocations

Mockito also allows us to verify that a method is called a certain number of times. We can use the times() method as a verification mode in the verify() call.

// Creating a mock object
UserService userService = Mockito.mock(UserService.class);

// Calling the method to be tested multiple times
userService.saveUser(new User("John Doe"));
userService.saveUser(new User("Jane Smith"));

// Verifying the method call with specified number of invocations
verify(userService, times(2)).saveUser(any(User.class));

In the above example, the times(2) verification mode is used to ensure that the saveUser() method is called exactly two times on the userService mock object.

Conclusion

The verify() method provided by Mockito is a powerful tool that allows developers to assert method calls and perform argument matching in unit tests. By using this method effectively, developers can ensure that their code is behaving as expected and that certain method invocations are being made. Understanding and utilizing the verify() method can greatly improve the accuracy and reliability of your unit tests when using Mockito.


noob to master © copyleft