SlideShare a Scribd company logo
Best practices for unit testing RxJava
Simon Perčič, senior Android developer
Tuesday, November 8th, 2016 @ 18:00
RxJava?
● Useful for combining, transforming, manipulating
streams of data
● How to unit test?
What to unit test?
● Observables - composition of operators
● Code that uses Rx (e.g. presenters)
Let’s set an example
What will we test?
What will we test?
DataManager methods:
Tip #1 (testability)
Tip #1
Separate data providers
Test skeleton 1/3
mocked
subject of testing
Test skeleton 2/3
Test skeleton 3/3
Test examples*
* not best practices
Wrong test #1
Works.
→ But only if getPlanets() is run on the immediate thread
Wrong test #1
Works.
→ But only if getPlanets() is run on the immediate thread
Not immediate thread
Runs on another thread!
Wrong test #1 - on another thread
Fails.
→ getPlanets() switches thread, Assert happens “too soon”
Wrong test #2 - blocking
Works.
→ it waits for the getPlanets() single to complete.
Wrong test #2 - countdown
Blocks here
Tip #2 (threading)
Tip #2
Single / Observable transformer
Network thread
Tip #2 - in manager
Compose with scheduler transformer
Tip #2 - in test
No op transformation.
Tip #3 (the big one)*
* best practices
Built-in classes
● TestSubscriber
● TestScheduler
● TestSubject
Tip #3 [1/3] - TestSubscriber
● Subscriber with additional test methods
● Part of RxJava
TestSubscriber example
● Blocking
○ awaitTerminalEvent()
TestSubscriber methods
Will block
● Assertions
○ Completion
■ assertCompleted() / assertNotCompleted()
■ assertTerminalEvent() / assertNoTerminalEvent()
TestSubscriber methods
● Assertions
○ Errors
■ assertNoErrors()
■ assertError(Class / Throwable)
TestSubscriber methods
● Assertions
○ Values
■ assertNoValues()
■ assertValueCount(int)
■ assertValue(T) / assertValues(T…)
TestSubscriber methods
● Get values
○ Values
■ List<T> getOnNextEvents()
○ Errors
■ List<Throwable> getOnErrorEvents()
TestSubscriber methods
Tip #3 [2/3] - TestScheduler
● Time “manipulation”
○ Advance time
■ advanceTimeBy(long, TimeUnit)
■ advanceTimeTo (long, TimeUnit)
○ Force trigger
■ triggerActions()
TestScheduler methods
Tip #3 [3/3] - TestSubject
● Enables us to queue events with a delay
○ onNext(T, delayMs)
○ onCompleted(delayMs)
○ onError(Throwable, delayMs)
TestSubject - setup
TestSubject - test
● Use RxJava
● Make your code unit-testable
● Be aware of threading
● Use built-in testing utils
Recap
Questions?
@simonpercic @simonpercicsimonpercic

More Related Content

PPTX
Unit testing
PDF
Unit testing
PDF
Coderetreat @Sibiu 2012 08 18
PDF
Why on Earth would I test if I have to just "Let it crash"?
PDF
Manage Selenium Grid with Terraform by Omkar Khatavkar (https://www.linkedin....
PDF
Unit testing Ch. 13 of Programming Ruby
PPTX
Auto integration testing
PPTX
Unit-Testing Your Legacy JavaScript
Unit testing
Unit testing
Coderetreat @Sibiu 2012 08 18
Why on Earth would I test if I have to just "Let it crash"?
Manage Selenium Grid with Terraform by Omkar Khatavkar (https://www.linkedin....
Unit testing Ch. 13 of Programming Ruby
Auto integration testing
Unit-Testing Your Legacy JavaScript

What's hot (20)

PPTX
Workshop: Unit Testing in Python
PPTX
TDD a piccoli passi
PDF
Coderetreat @Sofia 2012 10 27
PPTX
Selenium TestNG
PDF
Test Driven Development in Python
PPTX
Automation testing
PPTX
TDD in Go with Ginkgo and Gomega
PPTX
Mocking in python
PPT
Defensive programming
PDF
Software Testing Project: Testing csmap program
ODP
Automated testing in Python and beyond
 
PDF
TMPA-2017: Regression Testing with Semiautomatic Test Selection for Auditing ...
PDF
Rx java workshop
PDF
Programming katas for Software Testers - CounterStrings
PPT
Session13 J2ME Timer
PDF
Unit testing legacy code
PDF
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and Assertions
PDF
TMPA-2015: The Application of Parameterized Hierarchy Templates for Automated...
PDF
Defencive programming
PDF
Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Workshop: Unit Testing in Python
TDD a piccoli passi
Coderetreat @Sofia 2012 10 27
Selenium TestNG
Test Driven Development in Python
Automation testing
TDD in Go with Ginkgo and Gomega
Mocking in python
Defensive programming
Software Testing Project: Testing csmap program
Automated testing in Python and beyond
 
TMPA-2017: Regression Testing with Semiautomatic Test Selection for Auditing ...
Rx java workshop
Programming katas for Software Testers - CounterStrings
Session13 J2ME Timer
Unit testing legacy code
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and Assertions
TMPA-2015: The Application of Parameterized Hierarchy Templates for Automated...
Defencive programming
Vaidas Pilkauskas and Tadas Ščerbinskas - Can you trust your tests?
Ad

Viewers also liked (20)

PPTX
Grails 1.1 Testing - Unit, Integration & Functional
PPTX
Model-View-ViewModel and RxJava
PDF
RxJava 2.0 介紹
PDF
Practical RxJava for Android
PDF
Saving lives with rx java
ODP
Crear Unha Conta Gmail
PDF
Comentriobblicomoody 130113083659-phpapp01
PDF
Case Study Analysis Lucent Technologies
DOCX
Data proyek pt cabaro tahun 2013 2014
DOC
19 phuong phap_chung_minh_bdt
PDF
Kite introduction
PPTX
Corporate Image Consulting
PPTX
Helping each other grow
PDF
Khoa van-tay-kaba e10-fingerprint doorlock
PDF
Noor ka khilona urdu (نور کا کھلونا)
PDF
Quantum Entanglement - Cryptography and Communication
PDF
Gnbkk by mz
PPT
The Fear of Running out of Money
PDF
International CES 2012 番外編
PDF
Published Sox9 Paper!
Grails 1.1 Testing - Unit, Integration & Functional
Model-View-ViewModel and RxJava
RxJava 2.0 介紹
Practical RxJava for Android
Saving lives with rx java
Crear Unha Conta Gmail
Comentriobblicomoody 130113083659-phpapp01
Case Study Analysis Lucent Technologies
Data proyek pt cabaro tahun 2013 2014
19 phuong phap_chung_minh_bdt
Kite introduction
Corporate Image Consulting
Helping each other grow
Khoa van-tay-kaba e10-fingerprint doorlock
Noor ka khilona urdu (نور کا کھلونا)
Quantum Entanglement - Cryptography and Communication
Gnbkk by mz
The Fear of Running out of Money
International CES 2012 番外編
Published Sox9 Paper!
Ad

Similar to Best practices for unit testing RxJava (20)

PPTX
Tdd guide
PDF
Shift-Left Testing: QA in a DevOps World by David Laulusa
PDF
Testing Spark and Scala
PPTX
Unit testing
PDF
Developer Tests - Things to Know (Vilnius JUG)
PPTX
2016 10-04: tdd++: tdd made easier
PPTX
Test suite minimization
PDF
Unit testing in xcode 8 with swift
KEY
An introduction to mutation testing
ODP
PPTX
Using Rhino Mocks for Effective Unit Testing
PDF
Automating Speed: A Proven Approach to Preventing Performance Regressions in ...
PPTX
Random testing
PPT
White boxvsblackbox
PPTX
Java2 days 2013-j-treg
PPT
Test Driven
PDF
Tdd in swift
PDF
Test-Driven Development (TDD) in Swift
PDF
Scaling Security Threat Detection with Apache Spark and Databricks
Tdd guide
Shift-Left Testing: QA in a DevOps World by David Laulusa
Testing Spark and Scala
Unit testing
Developer Tests - Things to Know (Vilnius JUG)
2016 10-04: tdd++: tdd made easier
Test suite minimization
Unit testing in xcode 8 with swift
An introduction to mutation testing
Using Rhino Mocks for Effective Unit Testing
Automating Speed: A Proven Approach to Preventing Performance Regressions in ...
Random testing
White boxvsblackbox
Java2 days 2013-j-treg
Test Driven
Tdd in swift
Test-Driven Development (TDD) in Swift
Scaling Security Threat Detection with Apache Spark and Databricks

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced IT Governance
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
cuic standard and advanced reporting.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
Advanced IT Governance
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Advanced Soft Computing BINUS July 2025.pdf
Machine learning based COVID-19 study performance prediction
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Sensors and Actuators in IoT Systems using pdf
NewMind AI Weekly Chronicles - August'25 Week I
Mobile App Security Testing_ A Comprehensive Guide.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
20250228 LYD VKU AI Blended-Learning.pptx
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

Best practices for unit testing RxJava