iex
06/16/2020, 5:47 PMwaitForExpectations(timeout: 5, handler: *nil*)
at the end, and in the callback expectation.fullfill()
to have the test wait for the callbackCasey Brooks
06/16/2020, 6:05 PMrunBlocking
, which allows you to wrap the async call with suspendCancelableCoroutine
and await for the result. Something like:
@Test
fun test1() = runBlocking {
val underTest = ServiceUnderTest()
val result = suspendCancellableCoroutine<String> { continuation ->
underTest.makeCall { continuation.resume(it) }
}
assertEquals("expectedValue", result)
}
elizarov
06/16/2020, 6:21 PMval event = Job()
event.complete() // to fire it
event.await() // to await for it
iex
06/16/2020, 8:12 PMiex
06/16/2020, 8:13 PM@Test
fun testCallCallback() = runBlocking {
val n = NativeApi()
val result = suspendCancellableCoroutine<String> { continuation ->
n.callCallback(object: Callback() {
override fun log(foo: String) {
println("!!! logged: $foo")
continuation.resume(foo, onCancellation = {})
}
})
}
println("!!! doesn't execute this")
assertEquals(result, "hi!")
}
iex
06/16/2020, 8:14 PMiex
06/16/2020, 8:15 PMiex
06/16/2020, 8:20 PMiex
06/16/2020, 8:31 PMrunBlockingTest
and adding a advanceTimeBy(120000)
at the endiex
06/16/2020, 8:33 PMCoroutineTestRule
)gildor
06/17/2020, 6:00 AMshould have mentioned that I’m using Android / JVM 1.6, sorryBut Job works fine on Android
iex
06/17/2020, 12:10 PMawait
appeared not to exist? Then I tried wait()
and it showed an error with JVM 1.6. What am I missing?Animesh Sahu
06/17/2020, 12:30 PMgildor
06/17/2020, 2:15 PM