I am trying to test a function where it uses `List...
# announcements
p
I am trying to test a function where it uses
List.take(n)
and I setup the
iterator
of the mocked list and populated it. I thought it could be enough, but I am getting NPE.
Copy code
`when`(mockedList.iterator()).thenReturn(list.iterator())
I noticed that
take
creates a new arraylist by doing
val list = ArrayList<T>(n)
. could be the problem here?
n
Why not just create a list containing the values your test needs?
Then you don’t need to fiddle around with Mockito at all
p
I can’t, there is no init for this list
of course the code above is just an example
n
What do you mean, “there is no init”.
How does the code under test get passed the mocked list?
p
“There is no easily mockable initializer”
n
Instead of passing it a mocked list, pass it a List created by the listOf function
p
I prefer to ditch
take
t
So first to the NPE Problem: I'm not exactly sure, but I guess the problem is that
.take(n)
calls the
size
property of your mock, which might return null. But what exactly keeps you from just creating an instance of RealmList with one of the constructors that are documented on the page you posted?
on a completely different note: when using mockito with kotlin, I suggest you use the mockito-kotlin lib. That helps you avoiding the backticks each time you mock a function and it also avoids some errors with non-null parameters ans mockito matchers. Or just fully replace mockito with mockk
👍 1