I have this block:
try {
withTimeout(200) {
val response = datafetcher.fetchData(request)
}
} catch (TimeoutCancellationException e) {
returns response with some error message added
}
I am trying to write a unit test by simulating some time on the fetchData part instead of throwing a TimeoutException to cover the exception code.
I have tried doing this:
runTest {
whenever(mockdatafetcher.fetchData(any())).thenAnswer(
runBlocking {
delay(7000)
defaultResponse
}
)
}
But, in debug mode, I can see it reaches the fetchData part and waits 7 seconds but the TimeoutException is not triggered. It gets the defaultResponse and move on like no TimeoutException happened.
also tried adding a never ending while loop to somehow trigger the TimeoutCancellationException (guess only cancellable functions can be used)
Can anyone help me with what I am doing wrong here? Or how to correctly test this. Using mockito runTest for unit testing
runBlockinginrunTestdoes not affectwithTimeoutstackoverflow.com/a/79622919/7613649