Hello 👋 I have a question. How can I assert that the following function fails:
Copy code
fun test3() {
applicationScope.launch {
throw IllegalStateException("test")
}
}
Using
assertFails
does not work as it fails with the message that the the function
test3
did not fail
m
mkrussel
04/05/2023, 8:36 PM
I would think inject a coroutine context that can be used to create the scope.
Then in the test, add a exception handler to that context and test if the handler got an exception.
But ideally, you should be trying to observe some effect that happens due to the exception and test that.
j
Joffrey
04/05/2023, 9:05 PM
test3()
doesn't fail, so you cannot assert that. It launches a coroutine that fails, but it doesn't fail itself
k
koufa
04/05/2023, 9:24 PM
@Joffrey Can you elaborate on that why the
applicationScope
will not propagate the exception ?
j
Joffrey
04/05/2023, 9:31 PM
The scope is independent from your function call, so it's incorrect to say that the function
test3()
fails, that's all I meant. The exception will not bubble up the call stack of
test3()
, it will bubble up the call stack of the dispatcher of the
applicationScope
, which is independent from the stack of
test3
.
m
mkrussel
04/05/2023, 9:32 PM
the test might even end before the exception is thrown depending on the dispatcher used to create the scope.
k
koufa
04/05/2023, 9:39 PM
Thanks guys. You are right I was confused due to the use of test dispatchers where launch executes eagerly but of course it does not alter the way exceptions are propagated. So the method succeeds but the thread running launch will fail.