Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
uburoiubu opened this issue Dec 20, 2018 · 4 comments
Open

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

uburoiubu opened this issue Dec 20, 2018 · 4 comments

Comments

@uburoiubu
Copy link

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?

@SidyakinAV
Copy link

SidyakinAV commented Jan 18, 2019

It is probably because dispose() is not open, so Mockito can not stub final method

@aliillyas
Copy link

aliillyas commented Feb 3, 2019

I have the same issue. Host is an enum and initializeHost is a method within that enum.

    Host(int resource) {
        initializeHost(resource);
    }

    @SuppressWarnings("ResourceType")
    public void initializeHost(int resource) {
        ....
    }

I would like doNothing() on initializeHost. But every time I run the test initializeHost runs

private val host = Host.testServer
    
@Test
fun test1() {
    // GIVEN
    testSubject = RequestCookieInterceptor(cookiesManager, host)

    doNothing().whenever(cmxHost.initializeHost(any()))

    // WHEN
    val response = testSubject.intercept(interceptorChain)

    // THEN
    assert(response.request().header(HttpHeaders.COOKIE) == COOKIE)
}

@bohsen
Copy link

bohsen commented Feb 4, 2019

@aliillyas

Host is an enum and initializeHost is a method within that enum.

As far as I know enums can't be mocked. Can I ask why it's an enum? This would be very strange design to me.

@aliillyas
Copy link

aliillyas commented Feb 4, 2019

This is an older enum implementation, I think its time to convert it to a class. Thank you for the answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants