Never check equality for two Floating-Point Numbers!

Would this test pass or fail?: @Test fun adding one tenth ten times equals one() { var result: Double = 0.0 repeat(10) { result += 0.1 } assert(result == 1.0) } It fails! But why? Non-decimal base Floating-point numbers like Float and Double are internally represented like this: mantissa * base ^ exponent For humans the base of 10 is very common. We use the decimal system. In the decimal system 0.
Read more →

runBlockingTest throws IllegalStateException: This job has not completed yet

runBlockingTest has quite a few pitfalls. One of them is this obscure exception: java.lang.IllegalStateException: This job has not completed yet at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1189) at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53) at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45) tl;dr: It is thrown when the test body in runBlockingTest suspends and the dispatcher doesn’t know when it will resume. That can happen if suspend funs are called in the test that have to wait for something from outside the test body. Try to avoid such suspend funs within runBlockingTest.
Read more →