mockito: NullPointerException when using argThat() for long parameter in verify

A minimal code example:

  @Test
  public void shouldNotBeBroken() throws Exception {
    TakesALong takesALong = mock(TakesALong.class);

    verify(takesALong).call(argThat(CoreMatchers.equalTo(11L)));
  }

  static interface TakesALong {
    void call(long l);
  }

This leads to an NPE logged as happening at the ‘verify’ call, which made me think that the the result of verify(takesALong) was null, but it seems like the NPE has a somewhat incomplete stack trace. Variations that don’t show this problem include:

  • changing the interface to take a Long instead of a long, or
  • using longThat(Matcher<Long>) instead of argThat()

If it is considered an error to use argThat where you’re passing in a long, then I think at least there should be a better error message.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 18 (17 by maintainers)

Commits related to this issue

Most upvoted comments

I agree that better error message would be great. Do you have suggestions how to implement it?

There’s a ‘longThat’ method that can be used in this context.

Hope that helps!