Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Best practices for unit testing RxJava

Best practices for unit testing RxJava

You've undoubtedly heard of RxJava. You probably started using it and instantly fell in love. You decided you're not going back, ever. Your code is now beautifully Rx-ified. Now, how can you test that it does what it's supposed to? In this talk, we'll look at the different options you have when writing unit tests for your Rx-powered code.

Simon Perčič

November 08, 2016
Tweet

More Decks by Simon Perčič

Other Decks in Technology

Transcript

  1. Best practices for unit testing RxJava Simon Perčič, senior Android

    developer Tuesday, November 8th, 2016 @ 18:00
  2. What to unit test? • Observables - composition of operators

    • Code that uses Rx (e.g. presenters)
  3. Wrong test #1 - on another thread Fails. → getPlanets()

    switches thread, Assert happens “too soon”
  4. Wrong test #2 - blocking Works. → it waits for

    the getPlanets() single to complete.
  5. • Get values ◦ Values ▪ List<T> getOnNextEvents() ◦ Errors

    ▪ List<Throwable> getOnErrorEvents() TestSubscriber methods
  6. • Time “manipulation” ◦ Advance time ▪ advanceTimeBy(long, TimeUnit) ▪

    advanceTimeTo (long, TimeUnit) ◦ Force trigger ▪ triggerActions() TestScheduler methods
  7. Tip #3 [3/3] - TestSubject • Enables us to queue

    events with a delay ◦ onNext(T, delayMs) ◦ onCompleted(delayMs) ◦ onError(Throwable, delayMs)
  8. • Use RxJava • Make your code unit-testable • Be

    aware of threading • Use built-in testing utils Recap