I tried to create unit test for my presenter class which using RxJava CompositeDisposable but it always gave a null pointer exception.
This is my presenter class:
class LastMatchPresenter(val mView : MatchContract.View,
val matchRepositoryImpl: MatchRepositoryImpl,
val scheduler: SchedulerProvider) : MatchContract.Presenter{
val compositeDisposable = CompositeDisposable()
override fun getFootballMatchData() {
mView.showLoading()
compositeDisposable.add(matchRepositoryImpl.getFootballMatch("4328")
.observeOn(scheduler.ui())
.subscribeOn(scheduler.io())
.subscribe{
mView.displayFootballMatch(it.events)
mView.hideLoading()
})
}
}
This the test class:
class LastMatchPresenterTest {
@Mock
lateinit var mView: MatchContract.View
@Mock
lateinit var matchRepositoryImpl: MatchRepositoryImpl
lateinit var scheduler: SchedulerProvider
lateinit var mPresenter: LastMatchPresenter
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
scheduler = TestSchedulerProvider()
mPresenter = LastMatchPresenter(mView, matchRepositoryImpl, scheduler)
}
@Test
fun getFootballMatchData() {
mPresenter.getFootballMatchData()
mView.showLoading()
}
}
when I ran the test it gave me the following error:
java.lang.NullPointerException
at com.rahmat.app.footballclub.feature.lastmatch.LastMatchPresenter.getFootballMatchData(LastMatchPresenter.kt:20)
at com.rahmat.app.footballclub.feature.lastmatch.LastMatchPresenterTest.getFootballMatchData(LastMatchPresenterTest.kt:43)
Where it point to:
This line .observeOn(scheduler.ui())