So I have a simple test, where I try to test the error result, something like this:
@Test
fun `test`() {
every { Util.fetchLocation()} returns Single.error(Exception())
val resultErrors = testableClass.testableFun().test().errors()
Assert.assertTrue(resultErrors.size == 1)
Assert.assertTrue(resultErrors.firstOrNull() is Exception)
}
Inside the testableClass calls a method, which I mocked. The problem is, inside this class, I had to map RxJava Single into suspend fun, and after that the suspend fun to Single<> something like this:
private suspend fun getString(): String{
return "string"
}
private fun getIntAsSingle(): Single<Int> {
return Single.just(2)
}
private suspend fun mapSingleToSuspend(){
getIntAsSingle().await()
}
private suspend fun awesomeMethod(): String{
return getString() + mapSingleToSuspend().toString()
}
private fun mapSuspendToSingle(): Single<String>{
return rxSingle {
awesomeMethod()
}
}
The rxSingle method comes from the kotlinx.coroutines.rx2 library
There is no other way to do this, because I have to use the result of first suspend fun with the result of first Single, and after that the return value has to be Single<>
I think, the problem comes from the RxJava await method, because when I tried to debug the test, the test result triggered sooner than the function had been called what contains the await() method.
Inside the test class I used this in the setUp() method to RxJava, but it didn't help:
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
For testing I use JUnit4 with mockk library.