38
Best practices for unit testing RxJava Simon Perčič, senior Android developer Tuesday, November 8th, 2016 @ 18:00

Best practices for unit testing RxJava

Embed Size (px)

Citation preview

Page 1: Best practices for unit testing RxJava

Best practices for unit testing RxJava

Simon Perčič, senior Android developer

Tuesday, November 8th, 2016 @ 18:00

Page 2: Best practices for unit testing RxJava

RxJava?

● Useful for combining, transforming, manipulating streams of data

● How to unit test?

Page 3: Best practices for unit testing RxJava

What to unit test?

● Observables - composition of operators

● Code that uses Rx (e.g. presenters)

Page 4: Best practices for unit testing RxJava

Let’s set an example

Page 5: Best practices for unit testing RxJava

What will we test?

Page 6: Best practices for unit testing RxJava

What will we test?

DataManager methods:

Page 7: Best practices for unit testing RxJava

Tip #1 (testability)

Page 8: Best practices for unit testing RxJava

Tip #1Separate data providers

Page 9: Best practices for unit testing RxJava

Test skeleton 1/3

mocked

subject of testing

Page 10: Best practices for unit testing RxJava

Test skeleton 2/3

Page 11: Best practices for unit testing RxJava

Test skeleton 3/3

Page 12: Best practices for unit testing RxJava

Test examples*

* not best practices

Page 13: Best practices for unit testing RxJava

Wrong test #1

Works. → But only if getPlanets() is run on the immediate thread

Page 14: Best practices for unit testing RxJava

Wrong test #1

Works. → But only if getPlanets() is run on the immediate thread

Page 15: Best practices for unit testing RxJava

Not immediate thread

Runs on another thread!

Page 16: Best practices for unit testing RxJava

Wrong test #1 - on another thread

Fails. → getPlanets() switches thread, Assert happens “too soon”

Page 17: Best practices for unit testing RxJava

Wrong test #2 - blocking

Works. → it waits for the getPlanets() single to complete.

Page 18: Best practices for unit testing RxJava

Wrong test #2 - countdown

Blocks here

Page 19: Best practices for unit testing RxJava

Tip #2 (threading)

Page 20: Best practices for unit testing RxJava

Tip #2

Single / Observable transformer

Network thread

Page 21: Best practices for unit testing RxJava

Tip #2 - in manager

Compose with scheduler transformer

Page 22: Best practices for unit testing RxJava

Tip #2 - in test

No op transformation.

Page 23: Best practices for unit testing RxJava

Tip #3 (the big one)*

* best practices

Page 24: Best practices for unit testing RxJava

Built-in classes

● TestSubscriber

● TestScheduler

● TestSubject

Page 25: Best practices for unit testing RxJava

Tip #3 [1/3] - TestSubscriber

● Subscriber with additional test methods

● Part of RxJava

Page 26: Best practices for unit testing RxJava

TestSubscriber example

Page 27: Best practices for unit testing RxJava

● Blocking○ awaitTerminalEvent()

TestSubscriber methods

Will block

Page 28: Best practices for unit testing RxJava

● Assertions○ Completion

■ assertCompleted() / assertNotCompleted()■ assertTerminalEvent() / assertNoTerminalEvent()

TestSubscriber methods

Page 29: Best practices for unit testing RxJava

● Assertions○ Errors

■ assertNoErrors()■ assertError(Class / Throwable)

TestSubscriber methods

Page 30: Best practices for unit testing RxJava

● Assertions○ Values

■ assertNoValues()■ assertValueCount(int)■ assertValue(T) / assertValues(T…)

TestSubscriber methods

Page 31: Best practices for unit testing RxJava

● Get values○ Values

■ List<T> getOnNextEvents()○ Errors

■ List<Throwable> getOnErrorEvents()

TestSubscriber methods

Page 32: Best practices for unit testing RxJava

Tip #3 [2/3] - TestScheduler

Page 33: Best practices for unit testing RxJava

● Time “manipulation”○ Advance time

■ advanceTimeBy(long, TimeUnit)■ advanceTimeTo (long, TimeUnit)

○ Force trigger■ triggerActions()

TestScheduler methods

Page 34: Best practices for unit testing RxJava

Tip #3 [3/3] - TestSubject

● Enables us to queue events with a delay○ onNext(T, delayMs)○ onCompleted(delayMs)○ onError(Throwable, delayMs)

Page 35: Best practices for unit testing RxJava

TestSubject - setup

Page 36: Best practices for unit testing RxJava

TestSubject - test

Page 37: Best practices for unit testing RxJava

● Use RxJava

● Make your code unit-testable

● Be aware of threading

● Use built-in testing utils

Recap

Page 38: Best practices for unit testing RxJava

Questions?

@simonpercic @simonpercicsimonpercic