Practical Challenges For RxJava Learners

Sergii Zhuk
ProAndroidDev
Published in
3 min readFeb 4, 2017

--

RxJava is a valuable part of Java developer toolset and #1 language improvement framework for Android developers. Many of us want to learn it better, read some blogs and sources, but often miss practice to consolidate collected knowledge. Below I suggest a way to challenge yourself with coding tasks and improve practical RxJava skills.

Test Driven Learning

Test Driven Development has become a significant part of development culture, and everyone knows about it even if not completely following it. I think that adopting this idea to learn new frameworks and libraries also makes sense.

What if you have a set of cases with an acceptance criteria? So you can not only read another blog post or a code snippet, but quickly try out how you understand the problem. Basically it means that you can code a solution with an immediate right/wrong answer.

This approach would require minimum amount of configuration and dependencies. You just checkout the code, import it to your IDE and run unit tests. These tests will be failing from scratch. So now you need to add proper logic implementation to make them green and in this way pass the challenge. Pretty simple, isn’t it?

Learn RxJava by coding

I’ve built a set of simple code challenges to learn RxJava using JUnit tests as an acceptance criteria. For now they are focused on some basic concepts described in a classic blog series for RxJava beginners - Grokking RxJava by Dan Lew. Another pretty useful resource which helped me a lot was Intro-to-RxJava guide at GitHub.

The context of the challenge is following. Class Country with fields “currency”, “population” and “name” is provided. You have an interface CountriesService with a set of methods you should implement in the class CountriesServiceSolved to make unit tests pass. Obviously you should use RxJava and meet the interface contract.

Current cases don’t cover any Android topics so the project doesn’t contain an Android module, only the plain old Java.

Current code challenge implementation

Dependencies:

  • RxJava 2.0.5
  • JUnit 4.12

Reactive types covered:

  • Observable: the heart of Rx, a class that emits a stream of data or events
  • Single : a version of an Observable that emits a single item or fails
  • Maybe: lazy emission pattern, can emit 1 or 0 items or an error signal

Operators covered:

  • map: transforms the items by applying a function to each item
  • flatMap: takes the emissions of one Observable and returns merged emissions in another Observable to take its place
  • filter: emits only those items that pass a criteria (predicate test)
  • skip/take: suppress or takes the first n items
  • all: determines whether all items meet some criteria
  • reduce: applies a function to each item sequentially, and emits the final value. For example, it can be used to sum up all emitted items
  • toMap: converts an Observable into another object or data structure
  • test: returns TestObserver with current Observable subscribed
  • timeout: to handle timeouts, e.g. deliver some fallback data

Testing approach:

  • The set of test cases are defined in a separate java file
  • As a “receiver” of emitted test events we use TestObserver. It records events and allows to make assertions about them
  • All tests are failing when you take them from the master branch of a repository. This is expected behaviour. You should make tests pass by implementing the logic in CountriesServiceSolved class

Ready, Steady, Go!

Try out the code challenge in the repository here. I’m looking forward to hear your feedback and would be happy to add new test cases. Also feel free to submit your own pull requests and add more challenges.

Thanks for reading and please press a “heart” icon if you like this post.

--

--