Skip to content
Advertisement

Null pointer exception when using Mockito to mock interface

I’m using Mockito to test the following method:

JavaScript

Here is my test:

JavaScript

But I keep getting NullPointerExeption for this line:

JavaScript

ValueProducerFactory is an Interface

and the createValueProducer method signature is as follows:

JavaScript

I have a class named CachingValueProducerFactory that implements the interface

JavaScript

It seems like using Collections.emptyList() in the test is the problem, but I don’t see any other solution for this.

Advertisement

Answer

You are trying to mock two methods at once. In this line:

JavaScript

you are calling get() on the result of the createValueProducer() call, which is null because you haven’t mocked it yet.

There are two solutions:

Use deep stubbing:

JavaScript

Mock the Supplier first (pay attention to the removed get()):

JavaScript
User contributions licensed under: CC BY-SA
11 People found this is helpful
Advertisement