mardi 21 avril 2015

NullPointerException in Mockito when mocking method with primitive argument

I've spent the last little while pulling out my hair trying to find the problem in my test, and eventually figured out it has to do with mocking a method that takes primitive arguments. Here's a sample test that demos the problem:

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.Test;

public class MockitoTest {
    public static interface Foo {
        public Object causeProblems(long arg);
    }

    @Test
    public void testFoo() {
        Foo foo = mock(Foo.class);
        foo.causeProblems(123);
        verify(foo, times(1)).causeProblems(any());
    }
}

When running this test (I'm using Mockito 1.10 and Java8), and for some reason my stack trace is showing an NPE on the verify line:

java.lang.NullPointerException
    at com.amazon.jetstream.executor.worker.invoke.MockitoTest.testFoo(MockitoTest.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
....

I think part of my stack trace is being suppressed (?) Digging into it a bit further, I can get slightly more info out of it if I run it in Eclipse and "inspect" the line, which tells me simply:

java.lang.NullPointerException at longValue()

Questions:

  1. Does anyone know how to workaround this bug?
  2. If you can reproduce this, can you get more info out of your stack trace?

Aucun commentaire:

Enregistrer un commentaire