Slide 1

Slide 1 text

Unit Testing Rx API’s (Understanding Rx API’s) Trevor John Draft trevorrjohn @trevorrjohn

Slide 2

Slide 2 text

What We Will Cover Learn how RxJava APIs work (should work) How to use what we learned to write simple tests

Slide 3

Slide 3 text

Quick Overview Rx programming definition: An API for asynchronous programming with observable streams (http: //reactivex.io/) While the API is simple it is very large Most common use case is fetching data

Slide 4

Slide 4 text

RxJava Usage Allows you to simply handle responses while ignoring threading It allows for easier error handling Minimizes race conditions

Slide 5

Slide 5 text

Understanding How Rx Works Observable

Slide 6

Slide 6 text

Understanding How Rx Works Observable

Slide 7

Slide 7 text

Understanding How Rx Works Observable

Slide 8

Slide 8 text

Understanding How Rx Works Subscription

Slide 9

Slide 9 text

Understanding How Rx Works With Rx if nobody is listening it won’t do anything! No subscription, no anything (There are exceptions)

Slide 10

Slide 10 text

Unit Test It Unit test → Mock it!

Slide 11

Slide 11 text

WHOA!? Crazy right?! Not flexible Not practical What can we do? What if we want to use map?

Slide 12

Slide 12 text

WHOA!? The test doesn’t even work… :(

Slide 13

Slide 13 text

Observable Creation OnSubscribe interface? Subscriber?

Slide 14

Slide 14 text

Subscriber! I know those methods! Observable#subscribe() How data is passed back How does this help us test?

Slide 15

Slide 15 text

So what now?

Slide 16

Slide 16 text

Fake OnSubscribe Captures the subscriber Creates interface for notifying the subscriber

Slide 17

Slide 17 text

How Do We Use It? That looks good! Not tied to the implementation!

Slide 18

Slide 18 text

What is Better? 5* statements Reusable Magic? 11 statements Ridged Very straightforward

Slide 19

Slide 19 text

Another Option? Subjects! - They are super powerful and could be a better/simpler solution for testing. Less configurable, but it works and no “fake” classes

Slide 20

Slide 20 text

Common Smells Name your tests! @Test public void methodName_context_assertion() Keep tests short! Limit assertions per test

Slide 21

Slide 21 text

Questions?