David Glasser
09/03/2019, 6:27 PM@Test fun foo = runBlocking {
. (We haven't tried using runBlockingTest
since we haven't needed the extra time-related power it brings — but maybe this is the answer to our issue?)
A problem we've been running into is that it's quite easy to have a test whose final line doesn't return Unit. Then the runBlocking doesn't return Unit and neither does the test function, and JUnit just skips it. There is an IntelliJ inspection for this at least but it seems like a bit of a pain. What do people do for this? @Test fun foo: Unit = runBlocking {
? @Test fun foo() { runBlocking {
? Something else?David Glasser
09/03/2019, 6:30 PM: Unit
answer but that doesn't actually compile. runBlocking<Unit>
works but isn't what IntelliJ says!simon.vergauwen
09/03/2019, 6:30 PMDavid Glasser
09/03/2019, 6:31 PMsimon.vergauwen
09/03/2019, 6:31 PMrunBlocking<Unit>
is my way to go if I run into that scenario.simon.vergauwen
09/03/2019, 6:31 PMDavid Glasser
09/03/2019, 6:32 PMrunBlockingTest
in my JUnit tests if I'm not using any of its fancy features?"simon.vergauwen
09/03/2019, 6:35 PMrunBlocking
according to the docs https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/run-blocking-test.html.simon.vergauwen
09/03/2019, 6:35 PMThis is similar to runBlocking but it will immediately progress past delays and into launch and async blocks.
David Glasser
09/03/2019, 9:00 PMtseisel
09/04/2019, 8:20 AMrunBlockingTest
provides much more than just fast-forwarding delays and Unit
return type:
- it fails the test if a coroutine is still active for some time after the test block ends,
- it reports uncaught exceptions as test failures.David Glasser
09/04/2019, 9:02 AMjava.lang.IllegalStateException: This job has not completed yet
at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1128)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
David Glasser
09/04/2019, 9:03 AMtseisel
09/04/2019, 10:23 AMfun main() = runBlockingTest {
println("Start")
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {}
println("End")
}
The funny part is that I get the following :
Start
Exception in thread "main" java.lang.IllegalStateException: This job has not completed yet
at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1128)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45)
at fr.nihilus.music.media.CoroutinesPlaygroundKt.main(CoroutinesPlayground.kt:23)
at fr.nihilus.music.media.CoroutinesPlaygroundKt.main(CoroutinesPlayground.kt)
End
I don't think that's the intented behavior ...tseisel
09/04/2019, 1:48 PMsam
09/07/2019, 3:45 PMDavid Glasser
09/07/2019, 3:46 PMsam
09/07/2019, 3:46 PMsam
09/07/2019, 3:47 PM