Search code examples
androidrx-javaandroid-espresso

Android Espresso not synchronized with Rx Completable


I have an issue with Espresso not waiting for Completable to finish, so my UI test is Failing

apiDataSource.getData()
             .flatMap { data ->
                    cacheDataSource.saveData(data)
                    .andThen(Observable.just(cacheDataSource.getData()))

is there a way to hold the Espresso thread until having the cacheDataSource.saveData(data) complete? Thank you in advance


Solution

  • The reason is that you call cacheDataSource.getData() right when you assemble the save sequence inside flatMap. From the documentation of just:

    Note that the item is taken and re-emitted as is and not computed by any means by just. Use fromCallable(Callable) to generate a single item on demand (when Observers subscribe to it).

    apiDataSource.getData()
             .flatMap { data ->
                    cacheDataSource.saveData(data)
                    .andThen(Observable.fromCallable { cacheDataSource.getData() })
             }