Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I verify that a method was called with certain parameters using Mockito


How do I verify that a method was called with certain parameters using Mockito


Verifying that a method was called with certain parameters using Mockito is a crucial aspect of unit testing. Mockito provides the `verify()` method to achieve this. Here's a detailed guide on how to use it:

Basic Syntax of Mockito Verify

The basic syntax for verifying a method call with specific parameters is as follows:

java
verify(mockObject).methodName(argument1, argument2, ...);

This syntax ensures that the `methodName` was called on the `mockObject` with the specified arguments.

Verifying Specific Parameters

To verify that a method was called with specific parameters, you can directly pass those parameters to the `verify()` method. For example:

java
// Create a mock object
Map mockMap = mock(Map.class);

// Use the mock object
mockMap.put("key", "value");

// Verify the behavior
verify(mockMap).put("key", "value");

In this example, `verify(mockMap).put("key", "value")` confirms that the `put()` method was called on `mockMap` with the arguments `"key"` and `"value"`.

Using ArgumentMatchers

If you don't care about the exact value of a parameter or want to match it loosely, you can use Mockito's `ArgumentMatchers`. For instance, if you want to verify that a method was called with any string argument, you can use `anyString()`:

java
verify(mockMap).put(anyString(), anyString());

This will pass if `put()` was called with any string arguments.

Verifying Method Calls with Complex Objects

When verifying method calls with complex objects, ensure that the objects have a properly implemented `equals()` method. This is crucial because Mockito uses the `equals()` method to compare objects. If the `equals()` method is not correctly overridden, Mockito might not correctly identify the objects as equal, leading to test failures.

For example, if you have a method that takes a custom object as a parameter:

java
public void process(CustomObject objectParam) {
    // Method implementation
}

You can verify that this method was called with a specific `CustomObject` like this:

java
CustomObject objectParam = new CustomObject(1, "Customer name");
verify(testObject).process(eq(objectParam));

However, if `CustomObject` does not have a correctly overridden `equals()` method, this verification might fail even if the objects seem identical.

Verifying the Number of Method Calls

Mockito also allows you to verify how many times a method was called. You can use `times()`, `atLeast()`, `atLeastOnce()`, and `atMost()` to specify the number of calls:

java
// Verify that the method was called exactly once
verify(mockObject, times(1)).methodName();

// Verify that the method was called at least once
verify(mockObject, atLeastOnce()).methodName();

// Verify that the method was called at least a certain number of times
verify(mockObject, atLeast(2)).methodName();

Verifying No More Interactions

After verifying all expected method calls, you can use `verifyNoMoreInteractions()` to ensure that no other methods were called on the mock object:

java
verifyNoMoreInteractions(mockObject);

This helps prevent unexpected interactions with the mock object.

Conclusion

Mockito's `verify()` method is a powerful tool for ensuring that methods are called with the correct parameters and the expected number of times. By combining it with `ArgumentMatchers` and understanding how to handle complex objects, you can write robust unit tests that validate the behavior of your code effectively.

Citations:
[1] https://ioflood.com/blog/mockito-verify/
[2] https://www.vogella.com/tutorials/Mockito/article.html
[3] https://www.dhiwise.com/post/mastering-unit-testing-a-complete-guide-to-mockito-verify
[4] https://stackoverflow.com/questions/11802801/using-mockito-how-do-i-verify-a-method-was-a-called-with-a-certain-argument
[5] https://www.digitalocean.com/community/tutorials/mockito-verify
[6] https://reflectoring.io/clean-unit-tests-with-mockito/
[7] https://stackoverflow.com/questions/52007080/mockito-verify-the-arguments-of-a-method-invocation/52007081
[8] https://www.javacodegeeks.com/2015/11/mockito-verification.html