Skip to content
Advertisement

Mockito thenReturn returns null when coming from generic function

I want to mock the ResultSet in a class, whose parameters come from a generic return function (getSQLValue):

JavaScript

The table class with the signature method:

JavaScript

And this is the unit test class:

JavaScript

The problem is that the ResultSet is always null, I can confirm when debugging.

Advertisement

Answer

Turns out that Mockito, after “accepting” the first argument as Integer, fails to aceppt the second argument as String. The solution (after much debugging) is to cast the any() matcher in the test function to the corresponding class type:

JavaScript

Now Mockito will match the parameters and the ResultSet will no longer return null.

Advertisement