Android: Testing an ExecutorService on the main thread

25
January 11, 2019, at 7:00 PM

I'm having a ViewModel with a code that is running inside the execute block (inside a Runnable). It all works fine, except that I can't successfully make the code run in the main thread when unit testing it (even debugging it, I can see the code inside does not run.)

If I make the code run outside the execute block, the test is working fine. We have a couple of helper functions that made this work in Java, but apparently that is not working in Kotlin. The way we used to run the code inside the block was just calling the method that executes it, and then call one of the two methods below. The functions are:

open fun runExecutor()
    {
        val captor = argumentCaptor<Runnable>()
        verify(executor, atLeastOnce()).execute(captor.capture())
        for (runnable in captor.allValues)
        {
            runnable.run()
        }
    }
    open fun runLastExecutor()
    {
        val captor = argumentCaptor<Runnable>()
        verify(executor, atLeastOnce()).execute(captor.capture())
        val runnableList = captor.allValues
        if (!runnableList.isEmpty())
        {
            runnableList[runnableList.size - 1].run()
        }
    }

I'm using Dagger 2 and in my so called TestServiceModule we have private val executor: ExecutorService = mock(). Maybe it is useful to let you all know that we're using Mockito 2 and Mockito Kotlin in case either or them might be the one to blame.

Thanks a lot in advance for the help!

READ ALSO
assigning a unique id for button [on hold]

assigning a unique id for button [on hold]

suppose there exist a form and has two button ,i want one button to get disabled until all parameter is filled by user while other Enabled,for users to navigate to back panelIs there exist a function to assign unique id to buttons?

47
Representing an ArrayList in a TextView

Representing an ArrayList in a TextView

I have created an ArrayList in an Android

26
CMake Error:Error occurred while communicating with CMake server

CMake Error:Error occurred while communicating with CMake server

When I new a CMake project by the Android Studio,it occures a problem when I open it by the first time:

61