简体   繁体   中英

Mockito: verify on a Mocked method not working

I'm facing a strange behaviour with Mockito.

Test initialization:

private val dao = Mockito.mock(AdDetailsDao::class.java)
    private val offeredResponse = Mockito.mock(AdDetailsRestResponse::class.java) as AdDetailsRestResponse<AdOffered>

This test is completed successfully:

@Test
    fun shouldCallWantedAdDetailsAndNotOfferedAdDetails_whenIsNotOfferedAndNotFallback(){
        val handler = AdDetailsHandler(dao, false, false, CompositeDisposable())

        handler.loadAd("some_id")

        verify(dao).getWantedAdDetails(anyString())
        verify(dao, times(0)).getOfferedAdDetails(anyString())
    }

this one is not:

@Test
    fun shouldCallOfferedAdDetailsAndWantedAdDetails_whenIsOfferedAndFallbackAndOfferedAdNotExist(){
        val handler = AdDetailsHandler(dao, true, true, CompositeDisposable())

        doReturn(false).`when`(offeredResponse).success
        doReturn(offeredResponse).`when`(dao).getOfferedAdDetails(anyString())

        handler.loadAd("some_id")

        verify(dao).getWantedAdDetails(anyString())
    }

Methods being tested (note this method are executed in the background by a scheduler):

private fun loadWantedTask(id: String): AdDetailsResponse {
        return trackTaskDuration(AnalyticsTagsFabric.Event.AD_DETAILS_LOADED) {
            val response = adDetailsDao.getWantedAdDetails(id)
            AdDetailsWantedResponse(response.advert)
        }
    }

    private fun loadOfferedWithFallbackTask(id: String): AdDetailsResponse {
        return trackTaskDuration(AnalyticsTagsFabric.Event.AD_DETAILS_LOADED) {
            System.out.println("task")
            val response = adDetailsDao.getOfferedAdDetails(id)
            System.out.println("success:" + response.success+ " obj id:"+response)
            if (response.success) {
                System.out.println("in offered")
                AdDetailsOfferedResponse(response.advert)
            }
            else {
                System.out.println("offered else")
                val response = adDetailsDao.getWantedAdDetails(id)
                System.out.println("success:" + response.success+ " obj id:"+response)
                if (response.success) {
                    System.out.println("in wanted")
                    AdDetailsWantedResponse(response.advert)
                } else {
                    System.out.println("in error")
                    AdDetailsErrorResponse(Exception())
                }
            }
        }

The output when testing the second method is:

task
success:false obj id:Mock for AdDetailsRestResponse, hashCode: 991806841
offered else

you can clearly see it comes to the point where getWantedAdDetails() is clearly called, but mockito says there no interaction with the mock.

What am I doing wrong? I'm thinking that I can't mock the response of mock's method and verify a method call on that same mock but I can't figure out if that's true and/or find a solution.

So it looked like the test was finishing before the background execution therefore not capturing the interactions. forcing those methods to run on the main thread just during the tests solved the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM