Problem in creating unit test android with RxJava and Mockito

1

The test comes down to making the assertions that the methods are called when the execution is done successfully, but an error is occurring that I can not resolve.

My class:

class LoginActivityPresenter(private var authUseCase: AuthStructure, private var view: LoginActivityView, private var database: SupervisorDatabaseInterface) {

        fun tryLogin(identifier: String, password: String) {
            val disposable = database.getQrCode()
                    .doOnSubscribe { view.showProgressDialog() }
                    .doAfterTerminate { view.hideProgressDialog() }
                    .flatMap { it -> authUseCase.authenticate(identifier, password, it.qrcode) }
                    .subscribe(
                            {
                                database.insertUserToken(it)
                                view.loginSucess()
                            },
                            {
                                when (it) {
                                    is EmptyResultSetException -> {
                                        view.loginErrorNoQrCode()
                                    }
                                    else -> {
                                        view.loginError()
                                        it.printStackTrace()
                                    }
                                }
                            }
                    )
        }
    }

Test class:

class LoginActivityPresenterTest {
    private val immediateScheduler = object : Scheduler() {
        override fun createWorker(): Worker {
            return ExecutorScheduler.ExecutorWorker(Executor { it.run() })
        }
    }

    private fun <T> any(): T {
        Mockito.any<T>()
        return uninitialized()
    }

    private fun <T> uninitialized(): T = null as T

    @Mock
    private lateinit var mLoginActivityView: LoginActivityView

    @Spy
    private lateinit var mAuthStructure: AuthStructure

    @Mock
    private lateinit var mSupervisorDatabaseInterface: SupervisorDatabaseInterface

    @InjectMocks
    private lateinit var mLoginActivityPresenter: LoginActivityPresenter

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)

        RxJavaPlugins.setInitIoSchedulerHandler { immediateScheduler }
        RxAndroidPlugins.setInitMainThreadSchedulerHandler { immediateScheduler }
    }

    @After
    fun tearDown() {
        RxJavaPlugins.reset()
        RxAndroidPlugins.reset()
    }

    @Test
    fun 'should happen when you successfully log in'() {
        val userToken = UserToken()
        val qrCode = QRCode("qr")
        val identifier = "test"
        val password = "password"

'when'(mSupervisorDatabaseInterface.getQrCode()).thenReturn(Single.just(qrCode))

        mLoginActivityPresenter.tryLogin(identifier, password)

        verify(mLoginActivityView, times(1)).showProgressDialog()

        verify(mLoginActivityView, times(1)).hideProgressDialog()

        verify(mLoginActivityView, times(1)).loginSucess()

        verify(mLoginActivityView, never()).loginError()
    }
}

The result:

java.lang.NullPointerException: The single returned by the mapper is null
    at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
    at io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onSuccess(SingleFlatMap.java:76)
    at io.reactivex.internal.operators.single.SingleDoAfterTerminate$DoAfterTerminateObserver.onSuccess(SingleDoAfterTerminate.java:70)
    at io.reactivex.internal.operators.single.SingleDoOnSubscribe$DoOnSubscribeSingleObserver.onSuccess(SingleDoOnSubscribe.java:77)
    at io.reactivex.internal.operators.single.SingleJust.subscribeActual(SingleJust.java:30)
    at io.reactivex.Single.subscribe(Single.java:3096)
    at io.reactivex.internal.operators.single.SingleDoOnSubscribe.subscribeActual(SingleDoOnSubscribe.java:41)
    at io.reactivex.Single.subscribe(Single.java:3096)
    at io.reactivex.internal.operators.single.SingleDoAfterTerminate.subscribeActual(SingleDoAfterTerminate.java:43)
    at io.reactivex.Single.subscribe(Single.java:3096)
    at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
    at io.reactivex.Single.subscribe(Single.java:3096)
    at io.reactivex.Single.subscribe(Single.java:3082)
    at id.teste.supervisor.presenters.login.LoginActivityPresenter.tryLogin(LoginActivityPresenter.kt:14)
    at id.teste.supervisor.presenters.login.LoginActivityPresenterTest.should happen when you successfully log in(LoginActivityPresenterTest.kt:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)


Wanted but not invoked:
mLoginActivityView.loginSucess();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenterTest.should happen when you successfully log in(LoginActivityPresenterTest.kt:78)

However, there were exactly 3 interactions with this mock:
mLoginActivityView.showProgressDialog();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenter$tryLogin$disposable$1.accept(LoginActivityPresenter.kt:11)

mLoginActivityView.loginError();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenter$tryLogin$disposable$5.accept(LoginActivityPresenter.kt:25)

mLoginActivityView.hideProgressDialog();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenter$tryLogin$disposable$2.run(LoginActivityPresenter.kt:12)


Wanted but not invoked:
mLoginActivityView.loginSucess();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenterTest.should happen when you successfully log in(LoginActivityPresenterTest.kt:78)

However, there were exactly 3 interactions with this mock:
mLoginActivityView.showProgressDialog();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenter$tryLogin$disposable$1.accept(LoginActivityPresenter.kt:11)

mLoginActivityView.loginError();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenter$tryLogin$disposable$5.accept(LoginActivityPresenter.kt:25)

mLoginActivityView.hideProgressDialog();
-> at id.teste.supervisor.presenters.login.LoginActivityPresenter$tryLogin$disposable$2.run(LoginActivityPresenter.kt:12)


    at id.teste.supervisor.presenters.login.LoginActivityPresenterTest.should happen when you successfully log in(LoginActivityPresenterTest.kt:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)


Process finished with exit code 255
    
asked by anonymous 03.08.2018 / 13:52

0 answers