0

The problem: Mockito throws an NPE in the test code statement, specifically the verify call with an argument matcher, with no further indication of the cause than the test code line.

The actual Exception when executing the test code is not only lacking information, but is also misleading:

java.lang.NullPointerException at de.example.testing.TmpTest.testSomething(TmpTest.java:31)

Very shrunk down example of the code to be tested:

class Medium {
    void setLock(boolean locked) {
        // do stuff
    }

    // and other functionality ...
}

Testing code:

@Test
void testSomething() {
    // call testing code
    
    verify(_medium, never())
        .setLock(any()); // <-- NPE
}

In this example, _medium is a valid Mock and the execution of verify(_medium, never()) also returns a non-null MockitoMock instance.

Dennis Kühn
  • 121
  • 1
  • 7

1 Answers1

3

The solution was: You cannot use any() to match arguments of primitive types.

In this case, Mockito offers anyBoolean(), as for all other primitives. However, due to the NPE, this is not obvious and may result in an extensive search and headscratching.

Dennis Kühn
  • 121
  • 1
  • 7