mangkool
mangkool

Reputation: 308

Testing presenter who contains CompositeDisposable

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())

Upvotes: 1

Views: 2298

Answers (3)

mol
mol

Reputation: 2667

You have to mock matchRepositoryImpl.getFootballMatch("4328") method like this:

Mockito.`when`(matchRepositoryImpl.getFootballMatch("4328"))
        .thenReturn(Observable.just(OBJECT_YOU_WANT_TO_BE_RETURNED))

You can put this mock in @Before block, or in particular test.

Upvotes: 2

Juanjo Berenguer
Juanjo Berenguer

Reputation: 789

I don't know if this can help you.

But i let you some of my code when i have a Presenter and PresenterTest.

Presenter:

...
private void loadRepos() {
        disposableManager.add(repoRepository.getTrendingRepos()
                .doOnSubscribe(__ -> viewModel.loadingUpdated().accept(true))
                .doOnEvent((d, t) -> viewModel.loadingUpdated().accept(false))
                .doOnSuccess(__ -> viewModel.reposUpdated().run())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(dataSource::setData, viewModel.onError()));
        }
...

I observe the changes in the mainThread but in my test i change the scheduler to run it.

And in the PresenterTest i add this to run the tests.

PresenterTest:

public class TrendingReposPresenterTest {

    static {
        RxAndroidPlugins.setInitMainThreadSchedulerHandler(schedulerCallable -> Schedulers.trampoline());
    }

...

  private void initializePresenter() {
            presenter = new TrendingReposPresenter(viewModel, repoRepository,
                    screenNavigator,Mockito.mock(DisposableManager.class),dataSource);
        }

Here is my disposable manager class to handle the observables.

DisposableManager

public class DisposableManager {

    private final CompositeDisposable compositeDisposable = new CompositeDisposable();

    public void add(Disposable... disposables) {
        compositeDisposable.addAll(disposables);
    }

    public void dispose() {
        compositeDisposable.clear();
    }
}

I hope this helps you. But i am not sure if this is your problem

Upvotes: 2

Janusz Hain
Janusz Hain

Reputation: 617

matchRepositoryImpl.getFootballMatch("4328") returns null probably. You mocked matchRepositoryImpl, but didn't mock getFootballMatch method for it. It is quick guess tho.

BTW I suggest you to try MockK if you test using Kotlin

Upvotes: 1

Related Questions