I am back with my testing question. I am testing t...
# coroutines
a
I am back with my testing question. I am testing this function
Copy code
fun execute(appConfig: AppConfig): Deferred<String> = async {
   registerDevice(appConfig.serviceId)    
}
Copy code
private suspend fun registerDevice(serviceId: String): String = suspendCoroutine {
   //some nasty 3rd party library delivering data in a callback
   lib.registerDeviceForService(object: Callback {
          override fun onSuccess(response) {
             it.resume(response.data)
          }
      }
   )
}
Here is the test
Copy code
@Test
    fun `ok result of registration results in success`() = blocking {
        val deferred = classToTest.execute(appConfig)
        mockRegisterResponseSuccess()
        val result = deferred.await()
        result.should.be.equal(trackingId)
    }
To ensure callback is executed, I have to use Mockito ArgumentCaptor:
Copy code
private fun mockRegisterResponseSuccess(result: ApiResponseStatus) {
        then(lib).should().registerDeviceForService(callbackCaptor.capture())
        callbackCaptor.firstValue.onSuccess(Response(data = "testdata"))
    }
Problem: test function is
async
and it’s code will be executed only after
await()
called that means that I can not capture the callback and mock it’s execution Any ideas?