My view model holds a very simple recyclerview adapter
When I try to send it messages (which in turn calls notifyDatasetChanged) it throws an exception like so
java.lang.NullPointerException
at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11996)
at
the problem is that the mObservers variable from AdapterDataObservable is null
the thing is that this extends Observable<AdapterDataObserver> which in turn defines mObservers as
protected final ArrayList<T> mObservers = new ArrayList<T>();
so basically the moment my adapter is instantiated , it will call
private final AdapterDataObservable mObservable = new AdapterDataObservable();
(which is called by the way, mObservable is not null)
which in turn should call mObservers = new ArrayList<T>();
can someone explain why this is never called? or if there is a way to get past this problem?
as a side note the adapter is not mocked it is a solid object.
Edit:
Here is the code of the tests I'm using:
class LoginViewModelTest {
private lateinit var vm: LoginViewModel
@get:Rule
val rule = InstantTaskExecutorRule()
@Before
fun setUp() {
whenever(settings.hasShownWelcome).thenReturn(false)
whenever(settings.serverIp).thenReturn("http://127.0.0.1")
//this is where the crash happens
vm = LoginViewModel(settings, service, app, TestLog, TestDispatchers) { p -> permissionGranted }
}
And below is the code that is tested:
class LoginViewModel(private val settings: ISettings, private val service: AppService, application: Application, l: ILog, dispatchers: IDispatchers, val permissionChecker: (String) -> Boolean) : BaseViewModel(application, l, dispatchers)
val stepAdapter :StepAdapter
init {
val maxSteps = calculateSteps()
//after this assignment, during the normal run, the stepAdapter.mObservable.mObservers is an empty array
//during unit tests, after this assignment it is null
stepAdapter = StepAdapter(maxSteps)
}