title: RxJava how to test debounce?
body: <p>I am trying to test RxJava2's "debounce" operator in the Android.</p>
  
  <p>I used the "debounce" operator for the search feature.</p>
  
  <p>In the View(Activity), searching is started.</p>
  
  <pre><code>etSearch.addTextChangedListener(object : TextWatcher {
  
      override fun afterTextChanged(editable: Editable) {
          mPresenter.search(editable.toString())
      }
  
      ...
  })
  </code></pre>
  
  <p>And the Presenter is here:</p>
  
  <pre><code>class MyPresenter(
      private val view: MyContract.View,
      private val apiService: ApiServie = ApiServiceImpl(),
      private val searchSubject: PublishSubject&lt;String&gt; = PublishSubject.create(),
      var debounceScheduler: Scheduler = Schedulers.computation()
  ) : MyContract.Presenter {
  
      init {
          setupSearch()
      }
  
      override fun search(keyword: String) {
          if (searchDisposable.size() == 0) {
              setupSearch()
          }
  
          searchSubject.onNext(keyword)
      }
  
      private fun setupSearch() {
          searchSubject.debounce(1000, TimeUnit.MILLISECONDS, debounceScheduler)
                  .distinctUntilChanged()
                  .switchMap { keyword -&gt;
                      apiService.search(keyword)
                  }
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe { response -&gt;
                      response.data?.let { data -&gt;
                          view.searchResult(data)
                      }
                  }
      }
  }
  </code></pre>
  
  <p>Actually, it works fine when I do the integration test.
  But what I want to do is testing the "search" function of the MyPresenter class.</p>
  
  <p>To do this, I read an <a href="https://medium.com/@grzesiek.matyszczak/how-to-test-debounce-in-rxjava-2-d43f64b5d58a" rel="nofollow noreferrer">article</a></p>
  
  <p>But it doesn't work...</p>
  
  <p>My test code is here:</p>
  
  <pre><code>class MyTest {
  
      @Mock
      private lateinit var mockView: MyContract.View
  
      @Mock
      private lateinit var mockApiService: ApiService
  
      private lateinit var mMyPresenter: MyPresenter
  
      private lateinit var inOrder: InOrder
  
      private val mTestScheduler: TestScheduler = TestScheduler()
  
      private val ZERO = 0
  
      private fun setUpScheduler() {
          val immediate = object : Scheduler() {
              override fun createWorker() = ExecutorScheduler.ExecutorWorker(Runnable::run)
          }
  
          RxJavaPlugins.setInitIoSchedulerHandler { immediate }
          RxAndroidPlugins.setInitMainThreadSchedulerHandler { immediate }
      }
  
      @Before
      fun setup() {
          MockitoAnnotations.initMocks(this)
          setUpScheduler()
  
          mMyPresenter = MyPresenter(mockView, mockApiService)
  
          inOrder = inOrder(mockView)
      }
  
      @Test
      fun searchBillsTest() {
          `when`(mockApiService.search("America"))
                  .thenReturn(Observable.just(mockResult))
  
          mMyPresenter.debounceScheduler = mTestScheduler
  
          mMyPresenter.search("America")
          mTestScheduler.advanceTimeBy(1000, TimeUnit.MILLISECONDS)
  
          verify(mockView).searchResult(mockResult)
      }
  }
  </code></pre>
  
  <p>The last verify is not called...
  I don't know why...
  I added "doOnNext" to print log to find the cause in "searchSubject", but "doOnNext" is not called...
  "doOnSubscribe" is called...</p>
  
  <p>Please do you know why?</p>