Mockito matchers provide several ways to handle null values, which are crucial when writing unit tests that involve methods with nullable parameters or return types. Here's a detailed overview of how Mockito handles null values using its matchers:
Using `isNull()` Matcher
The `isNull()` matcher is used to verify that an argument passed to a method is null. This is particularly useful when you expect a method to be called with a null parameter. For example, if you have a method `findItems(String query, Map qualifiers)`, and you expect the `qualifiers` map to be null, you can use `isNull()` like this:
java
verify(itemService).findItems(anyString(), isNull(Map.class));
This ensures that the `findItems` method was called with a null `qualifiers` map[5][9].
Using `notNull()` or `isNotNull()` Matcher
The `notNull()` and `isNotNull()` matchers are used to verify that an argument passed to a method is not null. These matchers are essentially the opposite of `isNull()`. They ensure that the argument is not null, which can be important in scenarios where null values are not expected or would cause issues.
java
verify(itemService).findItems(anyString(), notNull(Map.class));
This verifies that the `findItems` method was called with a non-null `qualifiers` map[1][11].
Handling Null Values with `any()` Matcher
The `any()` matcher does not perform any checks on the argument type or value, including null. It matches any input, regardless of whether it is null or not. However, when using `any()` with other matchers, you must use matchers for all arguments to avoid the `InvalidUseOfMatchersException`[1][9].
Using `RETURNS_SMART_NULLS` Strategy
Mockito also provides a strategy called `RETURNS_SMART_NULLS`, which can be used when creating mocks. This strategy returns "smart nulls" instead of regular nulls when an unstubbed method is called. Smart nulls provide better error messages when a `NullPointerException` occurs, helping you identify where the issue originated[2][3].
Best Practices
When handling null values with Mockito, it's essential to use the appropriate matcher based on your test scenario. If you expect a null value, use `isNull()`. If you expect a non-null value, use `notNull()` or `isNotNull()`. Always ensure that you use matchers consistently across all arguments in a method call to avoid errors. Additionally, consider using `RETURNS_SMART_NULLS` for better error handling in your tests.
Citations:[1] https://krossovochkin.com/posts/2021_09_11_mocking_matchers_api/
[2] https://www.toptal.com/java/a-guide-to-everyday-mockito
[3] https://contributors.scala-lang.org/t/mockito-testing-nulls-and-exceptions/3915
[4] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.isNull
[5] https://stevewall123.wordpress.com/2015/01/08/using-matches-for-null-parameter-with-mockito/
[6] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.nullable
[7] https://groups.google.com/g/mockito/c/efQb_um8WeY
[8] https://mockk.io
[9] https://stackoverflow.com/questions/32841543/matchers-any-for-null-value-in-mockito
[10] https://www.youtube.com/watch?v=6-gj2W2ycMI
[11] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.notNull