Hello :wave: I have a question. How can I assert t...
# coroutines
k
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
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
test3()
doesn't fail, so you cannot assert that. It launches a coroutine that fails, but it doesn't fail itself
k
@Joffrey Can you elaborate on that why the
applicationScope
will not propagate the exception ?
j
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
the test might even end before the exception is thrown depending on the dispatcher used to create the scope.
k
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.