1

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

Zoe
  • 27,060
  • 21
  • 118
  • 148
mangkool
  • 316
  • 2
  • 18

3 Answers3

2

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

Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8
2

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.

mol
  • 2,607
  • 4
  • 21
  • 40
1

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

Janusz Hain
  • 597
  • 5
  • 18
  • getFootballMatch return flowable objects, I tried to use "when" "thenReturn" but it still gave me the same error. do you have any suggestion to mock the method? – mangkool Sep 05 '18 at 10:33