https://kotlinlang.org logo
#android
Title
# android
a

alex.tavella

09/16/2019, 5:26 PM
Hello all, is there any way to test a
RecyclerView.Adapter
on a local VM? I have mocked some stuff but I'm getting a NPE when
notifyDataSetChanged
gets called
Copy code
java.lang.NullPointerException
	at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11996)
	at androidx.recyclerview.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:7070)
s

starke

09/16/2019, 8:27 PM
http://robolectric.org/ although it’s probably easier to extract the logic to a plain java class that doesn’t have any dependencies on android stuff
a

alex.tavella

09/16/2019, 9:12 PM
I want to test diffing and itemCount and stuff which are specific to the adapter
s

starke

09/16/2019, 9:13 PM
oo my itemCount method is usually as simple as
items.size()
although nowadays I tend to use groupie which handles that for you
and allows you to create pure java classes to hold the diffing logic
all to say - it is possible to extract that logic if you don’t want to deal with the headaches of testing w/ android classes
a

alex.tavella

09/16/2019, 9:20 PM
You don't use
ListAdapter
then?
Bc
DiffUtil
is heavily coupled with Android fwk
c

Chris Craik

09/21/2019, 2:16 AM
DiffUtil is not heavily coupled with android, there's actually only one method that takes an adapter that prevents it from being in an Android-free .jar library. I somewhat regret we didn't move DiffUtil to a separate package with the androidx refactor, to help us eventually move it to a .jar library on its own.
a

alex.tavella

12/17/2019, 12:50 AM
Yea, sorry about the "heavily". Theres just that one class which obliges me to use a non-mocked android.jar
9 Views