Unit tests for methods using coroutine launch(UI) fails


#1

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 {
                worklistRepository.getWorklist(
                    ip = preferenceManager.worklistIp,
                    port = preferenceManager.worklistPort).awaitResult()
            }.await()

            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() {
        job.cancel()
    }
}

Here’s one of my unittest:

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

    whenever(mWorklistRepositoryMock.getWorklist(anyString(), anyInt(), anyString()))
            .thenReturn(Calls.response(expectedWorklistResult))

    mInteractor.getWorklist(mOnWorklistResultListenerMock)

    verify(mOnWorklistResultListenerMock).onWorklistResult(anyList<WorklistItem>())
}

I keep getting the following message when run:

Wanted but not invoked:
onWorklistResultListener.onWorklistResult(

);
-> 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