Skip to content

Mockito calls the real method, stubbing does not work #314

Open
@uburoiubu

Description

@uburoiubu

I have the following test:

private var interceptSelectedRoute = mock<InterceptSelectedRoute>()
@Captor
val interceptSelectedRouteCaptor = argumentCaptor<DisposableObserver<DetailedRoute>>()
@Test
    fun `when selected route is intercepted, we should start persisting it`() {

        doNothing().`when`(interceptSelectedRoute).dispose()

        interactor.start()

        val selectedRoute = NavigatorRouteFactory.makeDetailedRoute()

        verify(interceptSelectedRoute).execute(interceptSelectedRouteCaptor.capture(), eq(null))

        interceptSelectedRouteCaptor.firstValue.onNext(selectedRoute)

        assertEquals(
                expected = NavigatorStateMachine.State.Initialization.PersistingRoute(selectedRoute),
                actual = interactor.stateMachine.state
        )
    }

Inside interactor there is this method invocation:

private fun stopInterceptingSelectedRoute() {
        interceptSelectedRoute.dispose()
   }

interceptSelectedRoute is a child of the following class:

abstract class ObservableUseCase<T, in Params> constructor(
        private val postExecutionThread: PostExecutionThread
) {

    val subscriptions = CompositeDisposable()

    abstract fun buildUseCaseObservable(params: Params? = null): Observable<T>

    open fun execute(observer: DisposableObserver<T>, params: Params? = null) {
        val observable = this.buildUseCaseObservable(params)
                .subscribeOn(Schedulers.io())
                .observeOn(postExecutionThread.scheduler)

        observable.subscribeWith(observer).disposedBy(subscriptions)
    }

    fun dispose() {
        subscriptions.clear()
    }

}

When stopInterceptingSelectedRoute() is invoked, I get NullPointerException because subscriptions == null.

I don't understand why doNothing() does not work here as expected (meaning that the real method is not invoked). Could you help?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions