RxJava is a valuable part of the Java developer toolset and the number one 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. See below for how you can challenge yourself with coding tasks and improve your practical RxJava skills.
Test Driven Learning
Test Driven Development (TDD) has become a significant part of development culture; everyone is aware of it, even if they’re not completely following it. I think adopting this idea to learn new frameworks and libraries makes absolute sense.
What if you have a set of cases with an acceptance criteria? With TDD, not only can you read another blog post or code snippet, but also quickly try out how you understand the problem. It basically means that you can code a solution with an immediate right/wrong answer.
This approach would require a minimum amount of configuration and dependencies. You just check out the code, import it into your IDE, and run unit tests. To be sure, these tests will fail from scratch. What you need now is 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 the classic blog series for RxJava beginners - Grokking RxJava by Dan Lew. Another pretty useful resource which has helped me was the Intro to RxJava guide at GitHub.
The context of the challenge is following. The 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 plain old Java.
Current code challenge implementation
- 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
- 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
- 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 us to make assertions about them
- All tests will fail 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 hearing your feedback and would be happy to add new test cases. Also, feel free to submit your own pull requests and add more challenges. You can find me on Twitter at @sergiizhuk if you have further questions.