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)

PDF
rxJava 2 tips and tricks
PDF
How to Think in RxJava Before Reacting
PDF
Practical RxJava for Android
PDF
Effective Unit Testing
PDF
The Mayans Lost Guide to RxJava on Android
PPTX
An Introduction to RxJava
PDF
Intro to RxJava/RxAndroid - GDG Munich Android
PDF
Reactive programming on Android
PDF
RxJava on Android
PDF
Unit testing, principles
PDF
RxJava@DAUG
PPTX
Advance unittest
PPTX
Reactive Programming on Android - RxAndroid - RxJava
PPT
Testing Software Engineering systems end to end
PPTX
Reactive Java (33rd Degree)
PPTX
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
PDF
RxJava - introduction & design
PDF
Rx java testing patterns
PPT
Software Engineering XUnit Testing and Patterns
PPTX
Introduction to RxJava on Android
rxJava 2 tips and tricks
How to Think in RxJava Before Reacting
Practical RxJava for Android
Effective Unit Testing
The Mayans Lost Guide to RxJava on Android
An Introduction to RxJava
Intro to RxJava/RxAndroid - GDG Munich Android
Reactive programming on Android
RxJava on Android
Unit testing, principles
RxJava@DAUG
Advance unittest
Reactive Programming on Android - RxAndroid - RxJava
Testing Software Engineering systems end to end
Reactive Java (33rd Degree)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
RxJava - introduction & design
Rx java testing patterns
Software Engineering XUnit Testing and Patterns
Introduction to RxJava on Android

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
Teaching material agriculture food technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation_ Review paper, used for researhc scholars
Dropbox Q2 2025 Financial Results & Investor Presentation
Per capita expenditure prediction using model stacking based on satellite ima...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Network Security Unit 5.pdf for BCA BBA.
Empathic Computing: Creating Shared Understanding
Programs and apps: productivity, graphics, security and other tools
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Teaching material agriculture food technology
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Best practices for unit testing RxJava