Unit tests for methods using coroutine launch(UI) fails


I’m using kotlin coroutines and kotlin retrofit coroutines to do network requests in the project I’m currently working on. But my unittests are causing problems. The problem is in the getWorklist-method in below class.

I’m using launch(UI)-coroutine to run a network request, but for some reason the the getWorklist-method never enters into the coroutine.
If I change launch(UI) { ... } to runBlocking { ... } in WorklistInterctor#getWorklist() the tests all pass.

Here’s my code:

class WorklistInteractor @Inject
    constructor(private val worklistRepository: WorklistRepository,
        private val preferenceManager: PreferenceManager)
: NetworkInteractor, WorklistDialogContract.Interactor {

    private var job = Job()

    override fun getWorklist(listener: OnWorklistResultListener) {
        job = launch(UI) {
            val result = async {
                    ip = preferenceManager.worklistIp,
                    port = preferenceManager.worklistPort).awaitResult()

            when (result) {
            //Successful HTTP result
                is Result.Ok -> listener.onWorklistResult(result.value)
            // Any HTTP error
                is Result.Error -> {
                    Timber.e(result.exception, "HTTP error with code %s}", result.exception.code())
                    when(result.exception.code()) {
                        401 -> listener.onInvalidCredentialsFailure()
                        500 -> listener.internalServerError()
                        503 -> listener.noServerResponseFailure()
                        else -> listener.onError(result.exception.cause.toString())
            // Exception while request invocation
                is Result.Exception -> {
                    Timber.e(result.exception.cause, "Exception with cause %s", result.exception.cause.toString())
                    when(result.exception) {
                        is ConnectException -> listener.connectionRefused()
                        is SocketTimeoutException -> listener.failedToConnectToHost()
                        else -> listener.onError(result.exception.cause.toString())

    override fun cancel() {

Here’s one of my unittest:

fun `when worklistquery returns result, pass result back through listeners OnWorklistResult()`()
        = runBlocking {

    whenever(mWorklistRepositoryMock.getWorklist(anyString(), anyInt(), anyString()))



I keep getting the following message when run:

Wanted but not invoked:

-> at com.example.dialogs.worklistdialog.WorklistInteractorTest$when worklistquery returns result, pass result back through listeners OnWorklistResult()$1.doResume(WorklistInteractorTest.kt:58)

Actually, there were zero interactions with this mock.

Link to StackOverflow question