SlideShare a Scribd company logo
How to TDD your MVP
Testing Android (mobile) apps
Michał Bujalski
Android Developer at TSH
Plan
- Why to test your apps
- Model-View-Presenter
- Unit Testing Android apps
- Test-Driven-Development
- Demo
Tests
Tests == Unit Tests
Do you test your apps?
Do you test your app?
Why u not test your code?
- testing takes a lot of time
Why u not test your code?
- testing takes a lot of time
Why u not test your code?
- ASAP-Driven-Development
Why u not test your code?
- ASAP-Driven-Development
Why u not test your code?
- our code is not testable so we don’t write tests
- we don’t write tests because our code is not testable
Why you should test your apps?
- give you confidence
- tests save time
- make easier to add new features
- make easier refactor
- testable, clean code
Testing mobile apps - problems
- big blocks of code
- deeply coupled with OS components
- MVC - View layer blends into Controller
MVP vs MVC
- MVC not always suitable for systems heavily tied with UI
- View and Controller often blend with each other
Model-View-Presenter
Model-View-Presenter
Model-View-Presenter
View
- contains no logic
- contains all view references
- controls the state of platform components (TextView, ImageView)
Model-View-Presenter
Presenter
Model-View-Presenter
Presenter
- contains ALL the logic necessary to present model on the view
- contains NO Framework or SDK references (Context, ContentProviders)
Model-View-Presenter
Model
- contains logic necessary for obtaining data ( API fetch, cache handling, db
calls)
Model-View-Presenter
data class Contact(val name:String)
interface ContactsContract{
interface View{
fun setData(contacts:List<Contact>)
}
interface Presenter{
fun fetchContacts()
}
}
Model-View-Presenter
class ContractsActivity: AppCompatActivity(), ContactsContract.View{
lateinit var presenter: ContactsPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter = ContactsPresenter(this)
}
override fun setData(contacts: List<Contact>) {
//set data to adapter
}
Model-View-Presenter
class ContactsPresenter(
private val view: ContactsContract.View
private val repository: ContactsRepository
): ContactsContract.Presenter {
override fun fetchContacts() {
//called contacts fetched
view.setData(emptyList())
}
}
Model-View-Presenter
- UI logic is written purely in Java/Kotlin
- testable
- does a lot of heavy lifting for Activities/Fragments/ViewControllers
Unit Testing in Android
- Junit
- Mockito
- Robolectric
Unit Testing in Android - Mockito
whenever(repository.fetchContacts()).thenReturn(emptyList())
verify(view).setData(any())
verify(view, never()).setData(any())
verify(view, times(2)).setData(any())
...
val inOrder = inOrder(view,repository)
inOrder.verify(view).showProgress()
inOrder.verify(view).hideProgress()
Unit Testing in Android - Robolectric
Unit Testing in Android - Robolectric
You don’t need Robolectric
Unit Testing in Android - Robolectric
Robolectric
- adds more unnecessary dependencies (e. g. API version)
- encourages coupling your logic (and tests) with Android SDK
- hard to maintain
Unit Testing in Android
If you need SDK components in your unit tests
- you are doing something wrong
Andrzej Sz.
Unit Testing in Android - Robolectric
Test-Driven-Development
Write tests before you start implementing feature
Test-Driven-Development
1. Write your tests
2. Make your code compile
3. Run your tests
4. If tests fail, fix implementation and goto 2 else goto step 4.
5. Refactor
6. Stop
Test-Driven-Development
- tests are part of development process
- concentrate on what the code should look not on how should it be
implemented
- test are actually code documentation
- test coverage for free
Test-Driven-Development - writing test scenarios
Contacts list
Test-Driven-Development - writing test scenarios
Contacts list
- fetch contacts from API
- show result on view
- if any error is returned present error on view
Test-Driven-Development - writing test scenarios
Contacts list
Given
api returns valid data
When
List update is called
Then
View should receive update
Test-Driven-Development - writing test scenarios
@Mock lateinit var view:ContactsContract.View
@Mock lateinit var repository: ContactsRepository
lateinit var presenter:ContactsContract.Presenter
@Test
fun `fetch contacts, success`(){
//given
whenever(repository.fetchContacts()).thenReturn(emptyList())
//when
presenter.fetchContacts()
//then
verify(view).setData(any())
}
Test-Driven-Development - writing test scenarios
Contacts list
Given
api returns no connection error
When
List update is called
Then
Show network error
Test-Driven-Development - writing test scenarios
@Mock lateinit var view:ContactsContract.View
@Mock lateinit var repository: ContactsRepository
lateinit var presenter:ContactsContract.Presenter
@Test
fun `fetch contacts, network error`(){
//given
whenever(repository.fetchContacts()).thenThrow(NetworkError())
//when
presenter.fetchContacts()
//then
verify(view).showNetworkError()
}
Test-Driven-Development - writing test scenarios
- simple
- test one story at time
- test small part of functionality
Test-Driven-Development - Demo
- Kotlin
- RxJava
- Mockito
- Mockito-Kotlin
- RxKotlin
- RxAndroid
Test-Driven-Development - Demo
- https://github.com/michalbujalski/TDDExample
Summary
TDD your MVP
- unit testing is every developer’s responsibility
- tests SAVE time
- don’t mock what you don’t own, don’t test the framework
- make writing tests part of your development process NOT the result
TDD your MVP
Thank you

More Related Content

PPT
Getting Started with Zend Framework
PPTX
Rits Brown Bag - TypeScript
PPTX
Prince sttalkv5
PPTX
Api testing
PDF
Why vREST?
PPTX
Test api
PDF
Api presentation
PPTX
Getting Started with Zend Framework
Rits Brown Bag - TypeScript
Prince sttalkv5
Api testing
Why vREST?
Test api
Api presentation

What's hot (19)

PPTX
Mule Integration with Atlassian JIRA
PPTX
Api testing
PPTX
Api Testing
PPTX
Salesforce Coding techniques that keep your admins happy (DF13)
PPTX
Api Testing
PDF
Reasons To Automate API Testing Process
PPTX
Siebel Open UI Debugging (Siebel Open UI Training, Part 7)
PPTX
Customizing the Presentation Model and Physical Renderer in Siebel Open UI
PPTX
B4USolution_API-Testing
PPTX
Web API testing : A quick glance
PDF
Spring MVC
PPT
Overview of CSharp MVC3 and EF4
PPTX
Flash Testing with Selenium RC
PDF
Webservices in SalesForce (part 1)
PPTX
Overview of MVC Framework - by software outsourcing company india
PPTX
Request Validation In Spring Rest-Part2
PDF
Spring MVC 3.0 Framework (sesson_2)
PPT
Presentation for soap ui
Mule Integration with Atlassian JIRA
Api testing
Api Testing
Salesforce Coding techniques that keep your admins happy (DF13)
Api Testing
Reasons To Automate API Testing Process
Siebel Open UI Debugging (Siebel Open UI Training, Part 7)
Customizing the Presentation Model and Physical Renderer in Siebel Open UI
B4USolution_API-Testing
Web API testing : A quick glance
Spring MVC
Overview of CSharp MVC3 and EF4
Flash Testing with Selenium RC
Webservices in SalesForce (part 1)
Overview of MVC Framework - by software outsourcing company india
Request Validation In Spring Rest-Part2
Spring MVC 3.0 Framework (sesson_2)
Presentation for soap ui
Ad

Similar to How to tdd your mvp (20)

PDF
Android-centric-architecture vs. unit testable apps
PDF
How to build rock solid apps & keep 100m+ users happy
PDF
Model View Presenter
PDF
Android testing
PPTX
MVVM presentation
PDF
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
PDF
Building Maintainable Android Apps (DroidCon NYC 2014)
PDF
How to build rock solid apps & keep 100m+ users happy
PDF
Unit Testing & TDD Training for Mobile Apps
PDF
MvvmCross Seminar
PDF
MvvmCross Introduction
PDF
Refactoring Wunderlist. UA Mobile 2016.
PPTX
The real beginner's guide to android testing
PDF
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
PDF
Good bye Massive View Controller!
PDF
Android meetup
PPTX
MVVM ( Model View ViewModel )
PDF
PukaPuka Presentation
PDF
Five android architecture
PPTX
MV(X) architecture patterns
Android-centric-architecture vs. unit testable apps
How to build rock solid apps & keep 100m+ users happy
Model View Presenter
Android testing
MVVM presentation
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Building Maintainable Android Apps (DroidCon NYC 2014)
How to build rock solid apps & keep 100m+ users happy
Unit Testing & TDD Training for Mobile Apps
MvvmCross Seminar
MvvmCross Introduction
Refactoring Wunderlist. UA Mobile 2016.
The real beginner's guide to android testing
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Good bye Massive View Controller!
Android meetup
MVVM ( Model View ViewModel )
PukaPuka Presentation
Five android architecture
MV(X) architecture patterns
Ad

More from The Software House (20)

PDF
Jak kraść miliony, czyli o błędach bezpieczeństwa, które mogą spotkać również...
PDF
Uszanowanko Podsumowanko
PDF
Jak efektywnie podejść do certyfikacji w AWS?
PDF
O co chodzi z tą dostępnością cyfrową?
PDF
Chat tekstowy z użyciem Amazon Chime
PDF
Migracje danych serverless
PDF
Jak nie zwariować z architekturą Serverless?
PDF
Analiza semantyczna artykułów prasowych w 5 sprintów z użyciem AWS
PDF
Feature flags na ratunek projektu w JavaScript
PDF
Typowanie nominalne w TypeScript
PDF
Automatyzacja tworzenia frontendu z wykorzystaniem GraphQL
PDF
Serverless Compose vs hurtownia danych
PDF
Testy API: połączenie z bazą danych czy implementacja w pamięci
PDF
Jak skutecznie read model. Case study
PDF
Firestore czyli ognista baza od giganta z Doliny Krzemowej
PDF
Jak utrzymać stado Lambd w ryzach
PDF
Jak poskromić AWS?
PDF
O łączeniu Storyblok i Next.js
PDF
Amazon Step Functions. Sposób na implementację procesów w chmurze
PDF
Od Figmy do gotowej aplikacji bez linijki kodu
Jak kraść miliony, czyli o błędach bezpieczeństwa, które mogą spotkać również...
Uszanowanko Podsumowanko
Jak efektywnie podejść do certyfikacji w AWS?
O co chodzi z tą dostępnością cyfrową?
Chat tekstowy z użyciem Amazon Chime
Migracje danych serverless
Jak nie zwariować z architekturą Serverless?
Analiza semantyczna artykułów prasowych w 5 sprintów z użyciem AWS
Feature flags na ratunek projektu w JavaScript
Typowanie nominalne w TypeScript
Automatyzacja tworzenia frontendu z wykorzystaniem GraphQL
Serverless Compose vs hurtownia danych
Testy API: połączenie z bazą danych czy implementacja w pamięci
Jak skutecznie read model. Case study
Firestore czyli ognista baza od giganta z Doliny Krzemowej
Jak utrzymać stado Lambd w ryzach
Jak poskromić AWS?
O łączeniu Storyblok i Next.js
Amazon Step Functions. Sposób na implementację procesów w chmurze
Od Figmy do gotowej aplikacji bez linijki kodu

Recently uploaded (20)

PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPT
tcp ip networks nd ip layering assotred slides
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
Funds Management Learning Material for Beg
PDF
The Internet -By the Numbers, Sri Lanka Edition
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PDF
Decoding a Decade: 10 Years of Applied CTI Discipline
PPTX
Internet___Basics___Styled_ presentation
PPTX
Slides PPTX World Game (s) Eco Economic Epochs.pptx
PDF
Sims 4 Historia para lo sims 4 para jugar
PPTX
innovation process that make everything different.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PPTX
presentation_pfe-universite-molay-seltan.pptx
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
Job_Card_System_Styled_lorem_ipsum_.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
tcp ip networks nd ip layering assotred slides
SASE Traffic Flow - ZTNA Connector-1.pdf
Funds Management Learning Material for Beg
The Internet -By the Numbers, Sri Lanka Edition
Cloud-Scale Log Monitoring _ Datadog.pdf
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
An introduction to the IFRS (ISSB) Stndards.pdf
Triggering QUIC, presented by Geoff Huston at IETF 123
Decoding a Decade: 10 Years of Applied CTI Discipline
Internet___Basics___Styled_ presentation
Slides PPTX World Game (s) Eco Economic Epochs.pptx
Sims 4 Historia para lo sims 4 para jugar
innovation process that make everything different.pptx
Paper PDF World Game (s) Great Redesign.pdf
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
presentation_pfe-universite-molay-seltan.pptx
522797556-Unit-2-Temperature-measurement-1-1.pptx

How to tdd your mvp