Unit Tests Your
Views
Jorge D. Ortiz-Fuentes
@jdortiz
A Canonical
Examples
production
@jdortiz
@jdortiz
Agenda
★ Overview
★ Implementation details
★ I write tests
★ We write tests
Architecture
MVVM
MVVM
View
Model
ModelView
Events & Info Operations
Changes Changes
Architecture Components
View
Model
Room
Activity +
Fragments
Events & Info Operations
LiveData LiveData
Unit Testing
Testing Pyramid
Acceptance test
Integration tests
Unit tests
We are here
Unit Testing: Model
View
Model
Room
Activity +
Fragments
Events & Info Operations
LiveData LiveData
@jdortiz
Testing the Model
★ Use the defined operations -> Assertion on
results
★ Connect observe changes
★ Don’t test generated code!
Unit Testing: ViewModel
View
Model
Room
Activity +
Fragments
Events & Info Operations
LiveData LiveData
@jdortiz
Testing the ViewModel
★ Use mock as model
★ Input
• Invoke methods of the view model (used to
communicate events)
• Notify of changes in the model
★ Output: Check if
✦ The state of the view model is the expected one
✦ It invokes expected operations in the model
Unit Testing: View
View
Model
Room
Activity +
Fragments
Events & Info Operations
LiveData LiveData
@jdortiz
Testing the View
★ Inputs
• Changes in the properties of the view model
• User interacting with the views
★ Outputs
• The view changes as expected
• It tells the viewModel about the corresponding
events
Implementation
Details
@jdortiz
Dependency Injection
★ Koin
★ Very simple to use
★ For testing:
• Provide module with mocks
• override = true
• Use AutoCloseKoinTest
@jdortiz
Interacting with the
Views
★ Robolectric (4.0)
• Runner: (AndroidJUnit4:
androidx.test.ext:junit)
• Java 9 for testing Android Q
• Use Config sdk for Java 8
• Espresso actions and matchers
• Runs on JVM
Remember mockito-
kotlin configuration
Let’s write some
tests!
http://bit.ly/
droidconuk19
Thank
you!
@jdortiz
Enjoyed?
Please share it!
https://www.linkedin.com/in/jorgeortiz/
https://twitter.com/jdortiz
@jdortiz
#AdvArchMobile

Unit Test your Views