1

I've seen RxImmediateSchedulerRule usage to override schedulers for testing.But in my case,it doesn't work as I'm using another kind of schedulers -ThreadExecutor and PostExecutionThread from android10 cleanarchitecture github repo.

class UseCase(val repo: Repo, val threadExecutor: ThreadExecutor, val postExecutionThread: PostExecutionThread){
    fun execute() = repo.getData()
    .subscribeOn(Schedulers.from(threadExecutor))
    .observeOn(postExecutionThread.scheduler)
}

I'm able to override scheduler in observeOn method with below code.

whenever(postExecutionThread.scheduler).thenReturn(Schedulers.trampoline())

But I didn't find way to override scheduler in subscribeOn method.How can I do that?

Chan Myae Aung
  • 547
  • 3
  • 15

1 Answers1

0

You can pass the below test implementation of ThreadExecutor to the constructor of your UseCase in the test code. This is actually a factory function, which I prefer over derived classes in such cases (but you could use a derived class as well).

fun immediateExecutor() : ThreadExecutor {
    return object : ThreadExecutor {
        override fun run(command: Runnable) {
            command.run()
        }
    }
}

It just runs commands immediately, which should be sufficient for unit testing your use cases.

Janos Breuer
  • 480
  • 2
  • 6
  • 1
    Thanks @Janos Breuer! Your solution works perfectly. And I've just found another solution which might need to change constructor declaration (https://stackoverflow.com/a/43320828/6270674) – Chan Myae Aung Jan 10 '19 at 11:47