-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support assertThrows() with suspending functions in Kotlin #1851
Comments
Thanks for creating the issue! While I'm not familiar with Kotlin's coroutines and suspending functions, but I can see how that would make sense. @DerkSchooltink Would you be interested in submitting a PR? |
I have created a PR with my suggested fix, but I'm having some issues with Azure pipeline not compiling coroutines library from Kotlin too well. I'm seeing actual bytecode in the stacktrace. Might be an issue on my end, will investigate later. |
Probably related to https://youtrack.jetbrains.com/issue/KT-22228 |
Reopening due to #2042 (comment) |
Resolves in |
Sorry to comment on a long-closed issue. I tried the "simplest thing". If the unit test function is not itself suspending, how does it test a suspending function? internal class ScratchTest {
@Test
fun `should test suspending functions`() {
assertThrows<IllegalStateException> { failing() }
}
}
private suspend fun failing() {
delay(1L) // pretend
error("Fail on purpose")
} The compiler rightfully complains that "failing()" cannot be called from "assertThrows" as there is no suspension context. What is the scope and context for test functions? (https://kotlinlang.org/docs/reference/coroutines/basics.html) The merged commit did not include tests I could examine as examples. |
Hi @binkley, the design of @Test
fun `should test suspending functions`() = runBlockingTest {
assertThrows<IllegalStateException> { failing() }
} |
Hey! This does not seem to work with the |
@williamboman-pp, this issue was closed over a year ago. Please open a new issue to address Thanks |
@ashdavies Doesn't work with Kotlin 1.8.0, JUnit 4.13.2 and kotlinx-coroutines-test 1.5.2 (see screenshot). Maybe there should be assertFailsWith function. Furthermore, For now, I use approach with
Another solution is |
Often when I write a unit test that has to assert that a certain exception is thrown I use the
assertThrows()
method. But I find myself regularly having to test suspending functions, so I have to wrap my function with arunBlocking()
. This is a lot of repeating code if your entire codebase is heavily based on coroutines.Proposed fix
I couldn't possibly have made this issue without also thinking about a solution.
Effectively, this is what I came up with:
This way you can pass suspending functions as parameter
executable
, which will be invoked in arunBlocking()
to make sure the suspending function joins before continuing.tl;dr
I think it would be a good addition to JUnit Jupiter to have functions that allow easy testing of suspending functions.
The text was updated successfully, but these errors were encountered: