testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
package com.canbot.u05.utils
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Test
class CoroutineTest01 {
private suspend fun request(): String {
delay(10000);
return "response"
}
@Test
fun test1() = runTest {
val a = request();
Assert.assertEquals("response", a)
}
}
超时测试:
@Test
fun testFooWithTimeout() = runTest {
withTimeout(1_000) {
delay(80)
delay(2)
println("this won't be reached")
}
}