How do you use Mockito to verify interactions in unit tests?
Table of Contents
Introduction
In unit testing, it's important to ensure that your code interacts correctly with dependencies. Mockito is a popular mocking framework used to create mock objects and verify interactions in tests. When testing a class, you can use Mockito to verify that certain methods were called on mock objects, how many times they were called, and with what arguments. This helps ensure that your code is behaving as expected when interacting with its dependencies.
This guide explains how to use Mockito to verify interactions, focusing on method calls, the number of calls, and arguments passed during those calls.
Verifying Interactions in Mockito
1. Basic Verification with **verify()**
Mockito provides the verify()
method to check that a particular method was called on a mock object. The most basic form of verification is checking if a method was called on a mock.
Example:
In this example:
- We mock the
UserRepository
and call thesaveUser
method onUserService
. - We then verify that the
save
method was called exactly once on the mockUserRepository
with theuser
object as an argument.
2. Verifying the Number of Method Calls
Sometimes, you need to verify that a method was called a specific number of times. Mockito allows you to specify the number of times a method should have been called using verify(mock, times(int))
.
Example:
In this case:
- We create a mock list and call the
add
method three times. - We then verify that
add()
was called exactly three times usingtimes(3)
.
3. Verifying Method Calls with Specific Arguments
You can verify not only the number of calls but also the specific arguments passed to the method during those calls. Mockito provides verify(mock).methodName(arg)
to verify the arguments used in the method call.
Example:
Here, we verify that the correct arguments, "apple"
and "banana"
, were passed to the add()
method.
4. Verifying the Order of Method Calls
In some cases, the order in which methods are called is important. You can use Mockito's **InOrder**
feature to verify the exact order of method calls.
Example:
In this case:
- We create an
InOrder
object to verify that the methods were called in the expected order. - We then verify that
"first"
was added before"second"
.
5. Verifying Method Calls After a Condition or Timeout
Mockito provides support for verifying interactions after a certain condition is met or after a delay using **verifyNoMoreInteractions()**
and **verify(mock, timeout(int))**
.
Example:
In this example:
- We verify that the
add()
method was called onmockList
with"apple"
as an argument, but the verification happens after a specified timeout (in this case, 200 milliseconds).
6. Verifying No Interactions After a Test
If you want to ensure that no other interactions happened with the mock object after certain operations, you can use verifyNoMoreInteractions()
.
Example:
Here, we verify that add("first")
was called and ensure that no additional interactions occurred with mockList
.
Conclusion
Mockito's verification features allow you to ensure that the expected interactions occur between your code and its mock dependencies. Whether you need to verify the number of method calls, the arguments passed, the order of calls, or the absence of further interactions, Mockito provides simple and powerful tools to make sure your code behaves as expected.
Using **verify()**
, **InOrder**
, and related features, you can write robust unit tests that validate the behavior of your code while interacting with external components, ensuring that your application behaves correctly in real-world scenarios.