You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For Unit tests, there is the library org.jetbrains.kotlinx:kotlinx-coroutines-test that provides the function runTest(). It provides a couroutines dispatcher that makes delay() return immediately, so unit tests testing e.g. timeout are still fast.
When expectThrows() is used in such test, it does not 'pass' the coroutine scope. That means that delay() delay for "real" time.
Example (this test fails because the junit timeout fails the test after 100ms, while the delay is only returning after 1sec):
@Test
@Timeout(value =100, unit =TimeUnit.MILLISECONDS)
fun`delays should return immediately`() = runTest {
expectThrows<IllegalArgumentException> {
delay(1000)
throwIllegalArgumentException()
}
}
a more 'classical' approach works:
@Test
@Timeout(value =100, unit =TimeUnit.MILLISECONDS)
fun`delays should return immediately`() = runTest {
try {
delay(1000)
throwIllegalArgumentException()
fail("this should not be reached, we expect the previous line to throw an exception")
} catch(e:IllegalArgumentException) {
// ok
}
}
Suggestion to fix that issue: All functions like expectThrows() that call code passed as a suspend function should do this using the coroutineScope() function. This would pass the coroutine scheduler.
The text was updated successfully, but these errors were encountered:
We can also utilize the fact that inline functions are achromatic in the sense that they don't care whether they are invoked from suspending or non-suspending context. This will work as well:
For Unit tests, there is the library
org.jetbrains.kotlinx:kotlinx-coroutines-test
that provides the functionrunTest()
. It provides a couroutines dispatcher that makesdelay()
return immediately, so unit tests testing e.g. timeout are still fast.When
expectThrows()
is used in such test, it does not 'pass' the coroutine scope. That means thatdelay()
delay for "real" time.Example (this test fails because the junit timeout fails the test after 100ms, while the delay is only returning after 1sec):
a more 'classical' approach works:
Suggestion to fix that issue: All functions like
expectThrows()
that call code passed as asuspend
function should do this using thecoroutineScope()
function. This would pass the coroutine scheduler.The text was updated successfully, but these errors were encountered: