TESTING ANDROID APPS
Android-centric architecture vs. unit-testable android apps
PAUL WEIN
Consultant
Mobile Architect
150 8 +850
Offices EmployeesActive Clients
What Where
Netlight operates throughout Europe with
offices in Stockholm, Oslo, Munich,
Helsinki, Hamburg, Berlin, Paris and
London.
How
Founded in Stockholm, Sweden, since
1999, Netlight has helped leaders in the
digital industry to realise business critical
projects in the forefront of technology.
Netlight is an international consultancy firm
that offers a full range of services with a
focus on Technology and IT-Management.
Continuous	integration
Unit	tests
Instrumentation	tests
Code	review
Code	coverage
Test	driven	development
Behavior	driven	development
Static	code	analysis
Usability	tests
Test	documentation
Test	reports
Crashtracking
System	tests
Back	box	tests
White	box	tests
Security	tests
Load	tests
QA SHOULD BE PART
OF THE MINDSET OF A
DEVELOPER, EVEN
BEFORE HE STARTS
WRITING CODE.
If you don't test your software your users will!
Agenda
‣ Why has testing been so hard?
‣ Android-centric architecture
‣ Model-View-Presenter (MVP)
‣ Model-View-ViewModel (MVVM)
‣ Model-View-Intent (MVI)
‣ Tools
Why has testing Android Apps been so hard?
Testable code requires layers
of abstraction, which have a
performance cost
Performance Concerns
Misunderstanding
Android Components
Don’t architect your app
around system components
(Activities, Services,
Broadcast Receivers,
Content Providers)
The rise of TDD
Test-driven development
was on the rise at the same
time Android was
App building blocks
Intent
ServiceContent	Provider
Broadcast	
Receiver
Activity Fragment
Android-centric architecture
Activity
Lifecycle	Events
Business	Rules
Access	Hardware
Access	Sensors
Framework	APIs
….
• Mix presentation logic
with business rules
• Code is difficult to
understand
• To rigid to handle
varying use-cases
• Tight coupling & low
cohesion
Android-centric architecture
Activity
Lifecycle	Events
Business	Rules
Access	Hardware
Access	Sensors
Framework	APIs
….
• Mix presentation logic
with business rules
• Code is difficult to
understand
• To rigid to handle
varying use-cases
• Tight coupling & low
cohesion
Activity == View & Controller?
Address book example
Model View Presenter (MVP)
View Presenter Model
The presenter
‣ Make the presenter Framework
independent
‣ The presenter does not depend on
any android specific classes
‣ Abstract the presenter from
implementation details
‣ Abstraction makes it possible to write
non-instrumented tests
Model View Presenter (MVP)
View
<<Activity	/	
Fragment>>
Presenter
<<POJO>>
Model
<<POJO>>
IPresenter
<<Interface>>
IView
<<Interface>>
ViewPresenterContract
<<Interface>>
Model View Presenter (MVP)
View
<<Activity	/	
Fragment>>
Presenter
<<POJO>>
Model
<<POJO>>
IPresenter
<<Interface>>
IView
<<Interface>>
ViewPresenterContract
<<Interface>>
Unit	Tests Unit	TestsInstrumented	Tests
The ViewPresenterContract
The presenter
The view
Unit test
Model View Presenter
Clear separation of UI and
business logic
Separation Modularity
Reuse of presenters and
views
Testing
Testing in isolation without
external dependencies
Model View ViewModel (MVVM)
View ViewModel Model
Observable
/ Databinding
Reference
Model View Intent (MVI)
View Intent Model
State
Actions
Model View Intent (MVI)
Facebook Flux
Action Dispatcher Store
Action
View
MVI Principles
‣ Model that represents the state of the
app
‣ Pure functions
‣ Unidirectional data flow
‣ Immutable data
‣ State reducer takes current state as
input and produces the next state
MVI - Advantages
‣ Separates mutation and
asynchronicity
‣ Makes testing much easier
assertEquals(expectedModel, model)
‣ Readable code
‣ Predictable code
MVI ViewPresenterContract
MVI Presenter
MVI View
MVI Unit test
MVI Instrumentation test
MVI Logging & Timetravel
• { "loading": true, "error": null, "contacts": null }
• { "loading": false, "error": null, "contacts": […] }
• { "loading": true, "error": null, "contacts": null }
• { "loading": false, "error": {NetworkError}, "contacts": null }
Tools
• JUnit
• JaCoCo
• Mockito
• Espresso
• Crashlytics
• Infer
• LeakCanary
KENT BECK
PAUL WEIN
paul.wein@netlight.com
netlight.com

Android-centric-architecture vs. unit testable apps