Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I handle NullPointerExceptions when using argThat with Mockito


How do I handle NullPointerExceptions when using argThat with Mockito


Handling `NullPointerExceptions` when using `argThat` with Mockito involves understanding the nature of the `argThat` method and how it interacts with Java's type system. Here are some key points and strategies to manage these exceptions:

Understanding `argThat`

- Return Value: The `argThat` method always returns `null`. This is documented in Mockito's API and is intended for use in verification rather than stubbing[4][8].
- Auto-Unboxing Caveat: When using `argThat` with primitive types (like `int`, `float`, etc.), it can lead to `NullPointerExceptions` due to auto-unboxing. For primitives, use specific matchers like `intThat`, `floatThat`, etc.[7][8].

Causes of `NullPointerException`

1. Primitive Types: Using `argThat` with primitive types can cause `NullPointerExceptions` because the method returns `null`, which cannot be auto-unboxed into a primitive type[7][8].

2. Null Arguments: If the argument passed to the method being stubbed is `null`, and you're using a custom matcher that doesn't handle `null`, it will throw a `NullPointerException` when trying to access properties of the argument[3][9].

Strategies to Handle `NullPointerExceptions`

1. Use Specific Primitive Matchers: For primitive types, use `intThat`, `floatThat`, etc., instead of `argThat`. These methods return a default value (e.g., `0` for `intThat`) that can be safely auto-unboxed[7][8].

2. Handle Null Arguments: Ensure your custom matchers handle `null` arguments gracefully. You can add a null check at the beginning of your `matches` method:

java
   public boolean matches(Object argument) {
       if (argument == null) {
           return false; // or throw an exception, depending on your needs
       }
       // Rest of your logic here
   }
   

3. Workaround for Non-Primitive Types: If you're using `argThat` with non-primitive types and encountering issues due to `null` returns, consider using Hamcrest matchers or ensuring that your stubbing logic doesn't rely on the return value of `argThat`[2][5].

4. Mocking with Defaults: In some cases, especially when dealing with methods that check for `null`, you might need to provide a default value. This can be achieved by wrapping the argument in an `Optional` or using a supplier to provide a default value if `argThat` returns `null`[4].

Example of Handling Null Arguments

Here's how you might modify a custom matcher to handle `null`:

java
class MyObjectMatcher extends ArgumentMatcher {
    public boolean matches(Object argument) {
        if (argument == null) {
            return false;
        }
        return ((MyObject) argument).getValue().equals("123");
    }
}

Example of Using Specific Primitive Matchers

For primitive types, use specific matchers like `intThat`:

```java
when(mockList.get(intThat(n -> n