Arsen
03/16/2025, 5:52 PMasync()
?/** org.mockito.kotlin.wheneverBlocking */
wheneverBlocking { worker.getValue() }.thenThrow(RuntimeException("Something went wrong!"))
/** io.mockk.coEvery */
coEvery { worker.getValue() } throws RuntimeException("Something went wrong!")
mockito
and mockk
throws from async
block instead of await
// function under test
// ...
val deferred = async {
worker.getValue() // Throws here
}
try {
val result = deferred.await()
} catch (e: Exception) {
println("Caught")
}
Update: I finally figured out real cause (explanation in reply-thread) 😕Arsen
03/17/2025, 9:39 AMawait()
and captured by catch block
as expected. The point of confusion is printed stack trace like it wasn't caught. (1st screenshot)
The reason for stack trace been printed is "structured concurrency" or the way Jobs
propagates their failures. While exception was caught it still do not saves Job
from failure, so Job propagate this failure up to the first parentJob
which is capable to handle it. If there are no such parentJobs
then "top most" one just throw the original cause
as is (2nd screenshot).Arsen
03/17/2025, 9:50 AMEntityUnderTest
in real scenario runs under Scope
with SupervisorJob
while Test environment id driven by runTest() {}
builder