SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
Writing testable
Android apps
Tomáš Kypta
@TomasKypta
What does it mean
testable?
What does it mean testable?
• Test it quickly during development
• Test units in isolation
• There’s a way to provide mocks
How Google imagined
unit testing?
Instrumentation tests
What “standard” Android
app code looks like?
“Standard” Android app
• check Android SDK samples & documentation
• or Google’s iosched app
• business logic in Activities, Fragments, Services, …
• view and presenter effectively mixed together
This is actually the
structure recommended
by Google!
How to make it
testable?
Abstract away from
Android APIs
Android APIs
• difficult to test and mock
• we don’t have creation of Android components
under control
Abstracting Android APIs
• we need some boilerplate to get away
• remove all business logic from Android component
classes
• isolate business logic into presenters, managers,
POJOs, …
• when possible use MVP pattern
Abstracting Android APIs
• further improvements
• avoid or wrap Android API
Clean code
• try to write clean code
• avoid methods having hundreds of lines
• avoid Activities/Fragments having thousands of
lines
And how to test it?
• our presenters, POJOs, managers, providers, etc.
are simple to test
• Android components are stripped off of business
logic
• nothing important to test remains there
Bonus Advantages
• better readability
• better maintainability
Android Unit Tests
Android Unit Tests
• Added rather recently
• You need some work to make them really useful.
• Dependency injection
• Mocking
Dependency Injection
Dependency Injection
• the main ingredient
• avoid “new” keyword
• Dagger 2 FTW
Dagger 2
• by Google
• evolution of Dagger 1 (by Square)
• no reflection
• generated code in compile time
• constructor and field injection
Dagger 2
• Constructor injection
private ProviderC mProviderC;

private ProviderD mProviderD;



@Inject

public ProviderA(ProviderC providerC, ProviderD
providerD) {

mProviderC = providerC;

mProviderD = providerD;

}
Dagger 2
• Field injection
public class MainActivity extends AppCompatActivity {

@Inject ProviderA mProviderA;

@Inject ProviderB mProviderB;
@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);


((MyApp) getApplication()).getAppComponent()
.injectMainActivity(this);
}
}
Dagger 2
• prefer constructor injection wherever possible
• you can test the unit in isolation
• providing mocks is a piece of cake
Dagger 2
• Some catches when using field injection
• Construct the whole component
• Implicit provisions doesn’t support overrides!!
public class ProviderA {
…
@Inject

public ProviderA(ProviderC providerC) {

mProviderC = providerC;

}
}
Dagger 2
• Some catches when using field injection
• Make explicit provisions
@Module

public class AppModule {



@Provides @Singleton

ProviderB provideProvider2(ProviderC providerC) {

return new ProviderB(providerC);

}



@Provides

ProviderD provideProvider4() {

return new ProviderD();

}

}
• Beware marking constructors with @Inject when providing explicitly
• may create unwanted double provision
Mocking
Mocking
• Android framework has some historical things
• package android.test.mock
• for instrumentation tests
Android APIs & Unit Test
• historically
• java.lang.RuntimeException: Stub!
• now
• mockable android.jar
• Method ... not mocked.
Mocking
• it’s important to “program to an interface”
• avoid final and static
Mocking
@RunWith(JUnit4.class)

public class ProviderATest {



@Mock ProviderC mProviderC;



@Mock ProviderD mProviderD;



ProviderA mProviderA;



@Before

public void setUp() throws Exception {

MockitoAnnotations.initMocks(this);

Mockito.when(mProviderC.getString()).thenReturn("hello");

Mockito.when(mProviderD.getString()).thenReturn("world");

mProviderA = new ProviderA(mProviderC, mProviderD);

}
…
}
Mocking static
methods
Static methods
• avoid it as much as possible
• things like TextUtils.isEmpty() can be painful
• alternatives, e.g. Apache commons
StringUtils.isEmpty()
• PowerMock to the rescue
• still better to avoid static
Negatives of testable
apps?
Negatives?
• increase the number of methods and classes
• duplication of Android interfaces/classes
• bloated constructors with lots of dependencies
Dev App Builds
Dev App Builds
• How to use dev overrides in dev buils?
• utilize Android’s Gradle build
• Product Flavors can be used for that
Recommended
Libraries
Recommended Libraries
• Dagger 2
• Retrofit
• RxJava / RxAndroid
• Mortar
Recommended Libraries
- for testing
Q&A
Thank You

Weitere ähnliche Inhalte

Was ist angesagt?

Getting started with dagger 2
Getting started with dagger 2Getting started with dagger 2
Getting started with dagger 2Rodrigo Henriques
 
Angular2 workshop
Angular2 workshopAngular2 workshop
Angular2 workshopNir Kaufman
 
Android talks #08 dagger2
Android talks #08   dagger2Android talks #08   dagger2
Android talks #08 dagger2Infinum
 
基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構玄武 Wu
 
Angular 2 Essential Training
Angular 2 Essential Training Angular 2 Essential Training
Angular 2 Essential Training Patrick Schroeder
 
Commit University - Exploring Angular 2
Commit University - Exploring Angular 2Commit University - Exploring Angular 2
Commit University - Exploring Angular 2Commit University
 
Angular2 with TypeScript
Angular2 with TypeScript Angular2 with TypeScript
Angular2 with TypeScript Rohit Bishnoi
 
Automated Testing in Angular Slides
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular SlidesJim Lynch
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2Knoldus Inc.
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsYakov Fain
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsYakov Fain
 
Tech Webinar: Angular 2, Introduction to a new framework
Tech Webinar: Angular 2, Introduction to a new frameworkTech Webinar: Angular 2, Introduction to a new framework
Tech Webinar: Angular 2, Introduction to a new frameworkCodemotion
 
React Nativeの光と闇
React Nativeの光と闇React Nativeの光と闇
React Nativeの光と闇Yukiya Nakagawa
 
AngularJS Unit Test
AngularJS Unit TestAngularJS Unit Test
AngularJS Unit TestChiew Carol
 

Was ist angesagt? (20)

Getting started with dagger 2
Getting started with dagger 2Getting started with dagger 2
Getting started with dagger 2
 
Angular2 workshop
Angular2 workshopAngular2 workshop
Angular2 workshop
 
Epoxy 介紹
Epoxy 介紹Epoxy 介紹
Epoxy 介紹
 
Dagger2 Intro
Dagger2 IntroDagger2 Intro
Dagger2 Intro
 
Android talks #08 dagger2
Android talks #08   dagger2Android talks #08   dagger2
Android talks #08 dagger2
 
Dagger 2 ppt
Dagger 2 pptDagger 2 ppt
Dagger 2 ppt
 
基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構
 
Angular 2 Essential Training
Angular 2 Essential Training Angular 2 Essential Training
Angular 2 Essential Training
 
Commit University - Exploring Angular 2
Commit University - Exploring Angular 2Commit University - Exploring Angular 2
Commit University - Exploring Angular 2
 
Angular2 with TypeScript
Angular2 with TypeScript Angular2 with TypeScript
Angular2 with TypeScript
 
Automated Testing in Angular Slides
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular Slides
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
 
Tech Webinar: Angular 2, Introduction to a new framework
Tech Webinar: Angular 2, Introduction to a new frameworkTech Webinar: Angular 2, Introduction to a new framework
Tech Webinar: Angular 2, Introduction to a new framework
 
Angular
AngularAngular
Angular
 
React Nativeの光と闇
React Nativeの光と闇React Nativeの光と闇
React Nativeの光と闇
 
AngularJS Unit Test
AngularJS Unit TestAngularJS Unit Test
AngularJS Unit Test
 
Ngrx meta reducers
Ngrx meta reducersNgrx meta reducers
Ngrx meta reducers
 
Angular2 - In Action
Angular2  - In ActionAngular2  - In Action
Angular2 - In Action
 

Andere mochten auch

Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworksTomáš Kypta
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworksTomáš Kypta
 
Android Develpment vol. 3, MFF UK, 2015
Android Develpment vol. 3, MFF UK, 2015Android Develpment vol. 3, MFF UK, 2015
Android Develpment vol. 3, MFF UK, 2015Tomáš Kypta
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for AndroidTomáš Kypta
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for AndroidTomáš Kypta
 

Andere mochten auch (6)

Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
 
Guide to the jungle of testing frameworks
Guide to the jungle of testing frameworksGuide to the jungle of testing frameworks
Guide to the jungle of testing frameworks
 
Android Develpment vol. 3, MFF UK, 2015
Android Develpment vol. 3, MFF UK, 2015Android Develpment vol. 3, MFF UK, 2015
Android Develpment vol. 3, MFF UK, 2015
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 

Ähnlich wie Test Android Apps with Dagger 2 Dependency Injection

Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to realityDaniel Gallego Vico
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application TestingTroy Miles
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuPhat VU
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2Sanket Shah
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...DevDay.org
 
Using and contributing to the next Guice
Using and contributing to the next GuiceUsing and contributing to the next Guice
Using and contributing to the next GuiceAdrian Cole
 
Angular2 with type script
Angular2 with type scriptAngular2 with type script
Angular2 with type scriptRavi Mone
 
Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationThibault Even
 
Android architecture
Android architecture Android architecture
Android architecture Trong-An Bui
 
Angular js 2
Angular js 2Angular js 2
Angular js 2Ran Wahle
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0Michael Vorburger
 
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2Ivan Matiishyn
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...DicodingEvent
 
From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN StackTroy Miles
 
OpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheConOpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheConos890
 

Ähnlich wie Test Android Apps with Dagger 2 Dependency Injection (20)

Apache DeltaSpike: The CDI Toolbox
Apache DeltaSpike: The CDI ToolboxApache DeltaSpike: The CDI Toolbox
Apache DeltaSpike: The CDI Toolbox
 
Apache DeltaSpike the CDI toolbox
Apache DeltaSpike the CDI toolboxApache DeltaSpike the CDI toolbox
Apache DeltaSpike the CDI toolbox
 
Spring boot
Spring bootSpring boot
Spring boot
 
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
 
Devday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvuDevday2016 real unittestingwithmockframework-phatvu
Devday2016 real unittestingwithmockframework-phatvu
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Using and contributing to the next Guice
Using and contributing to the next GuiceUsing and contributing to the next Guice
Using and contributing to the next Guice
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
Angular2 with type script
Angular2 with type scriptAngular2 with type script
Angular2 with type script
 
Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son application
 
Android architecture
Android architecture Android architecture
Android architecture
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Introduction to Angular2
Introduction to Angular2Introduction to Angular2
Introduction to Angular2
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
 
Kku2011
Kku2011Kku2011
Kku2011
 
From MEAN to the MERN Stack
From MEAN to the MERN StackFrom MEAN to the MERN Stack
From MEAN to the MERN Stack
 
OpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheConOpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheCon
 

Mehr von Tomáš Kypta

Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stackTomáš Kypta
 
Unit testing and Android
Unit testing and AndroidUnit testing and Android
Unit testing and AndroidTomáš Kypta
 
Android Development for Phone and Tablet
Android Development for Phone and TabletAndroid Development for Phone and Tablet
Android Development for Phone and TabletTomáš Kypta
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
Android development - the basics, MFF UK, 2014
Android development - the basics, MFF UK, 2014Android development - the basics, MFF UK, 2014
Android development - the basics, MFF UK, 2014Tomáš Kypta
 
Android Development 201
Android Development 201Android Development 201
Android Development 201Tomáš Kypta
 
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013Tomáš Kypta
 
Užitečné Android knihovny pro vývoj a testování
Užitečné Android knihovny pro vývoj a testováníUžitečné Android knihovny pro vývoj a testování
Užitečné Android knihovny pro vývoj a testováníTomáš Kypta
 
Programování pro Android - úvod, FI MUNI, 2013
Programování pro Android - úvod, FI MUNI, 2013Programování pro Android - úvod, FI MUNI, 2013
Programování pro Android - úvod, FI MUNI, 2013Tomáš Kypta
 
Stylování ActionBaru
Stylování ActionBaruStylování ActionBaru
Stylování ActionBaruTomáš Kypta
 
Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Tomáš Kypta
 
Android development - the basics, FI MUNI, 2012
Android development - the basics, FI MUNI, 2012Android development - the basics, FI MUNI, 2012
Android development - the basics, FI MUNI, 2012Tomáš Kypta
 

Mehr von Tomáš Kypta (14)

Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
 
ProGuard
ProGuardProGuard
ProGuard
 
Unit testing and Android
Unit testing and AndroidUnit testing and Android
Unit testing and Android
 
Android Development for Phone and Tablet
Android Development for Phone and TabletAndroid Development for Phone and Tablet
Android Development for Phone and Tablet
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Android development - the basics, MFF UK, 2014
Android development - the basics, MFF UK, 2014Android development - the basics, MFF UK, 2014
Android development - the basics, MFF UK, 2014
 
Android Libraries
Android LibrariesAndroid Libraries
Android Libraries
 
Android Development 201
Android Development 201Android Development 201
Android Development 201
 
Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013Android development - the basics, MFF UK, 2013
Android development - the basics, MFF UK, 2013
 
Užitečné Android knihovny pro vývoj a testování
Užitečné Android knihovny pro vývoj a testováníUžitečné Android knihovny pro vývoj a testování
Užitečné Android knihovny pro vývoj a testování
 
Programování pro Android - úvod, FI MUNI, 2013
Programování pro Android - úvod, FI MUNI, 2013Programování pro Android - úvod, FI MUNI, 2013
Programování pro Android - úvod, FI MUNI, 2013
 
Stylování ActionBaru
Stylování ActionBaruStylování ActionBaru
Stylování ActionBaru
 
Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012
 
Android development - the basics, FI MUNI, 2012
Android development - the basics, FI MUNI, 2012Android development - the basics, FI MUNI, 2012
Android development - the basics, FI MUNI, 2012
 

Kürzlich hochgeladen

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Kürzlich hochgeladen (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Test Android Apps with Dagger 2 Dependency Injection

  • 2. What does it mean testable?
  • 3. What does it mean testable? • Test it quickly during development • Test units in isolation • There’s a way to provide mocks
  • 7. “Standard” Android app • check Android SDK samples & documentation • or Google’s iosched app • business logic in Activities, Fragments, Services, … • view and presenter effectively mixed together
  • 8. This is actually the structure recommended by Google!
  • 9. How to make it testable?
  • 11. Android APIs • difficult to test and mock • we don’t have creation of Android components under control
  • 12. Abstracting Android APIs • we need some boilerplate to get away • remove all business logic from Android component classes • isolate business logic into presenters, managers, POJOs, … • when possible use MVP pattern
  • 13. Abstracting Android APIs • further improvements • avoid or wrap Android API
  • 14. Clean code • try to write clean code • avoid methods having hundreds of lines • avoid Activities/Fragments having thousands of lines
  • 15. And how to test it? • our presenters, POJOs, managers, providers, etc. are simple to test • Android components are stripped off of business logic • nothing important to test remains there
  • 16. Bonus Advantages • better readability • better maintainability
  • 18. Android Unit Tests • Added rather recently • You need some work to make them really useful. • Dependency injection • Mocking
  • 20. Dependency Injection • the main ingredient • avoid “new” keyword • Dagger 2 FTW
  • 21. Dagger 2 • by Google • evolution of Dagger 1 (by Square) • no reflection • generated code in compile time • constructor and field injection
  • 22. Dagger 2 • Constructor injection private ProviderC mProviderC;
 private ProviderD mProviderD;
 
 @Inject
 public ProviderA(ProviderC providerC, ProviderD providerD) {
 mProviderC = providerC;
 mProviderD = providerD;
 }
  • 23. Dagger 2 • Field injection public class MainActivity extends AppCompatActivity {
 @Inject ProviderA mProviderA;
 @Inject ProviderB mProviderB; @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState); 
 ((MyApp) getApplication()).getAppComponent() .injectMainActivity(this); } }
  • 24. Dagger 2 • prefer constructor injection wherever possible • you can test the unit in isolation • providing mocks is a piece of cake
  • 25. Dagger 2 • Some catches when using field injection • Construct the whole component • Implicit provisions doesn’t support overrides!! public class ProviderA { … @Inject
 public ProviderA(ProviderC providerC) {
 mProviderC = providerC;
 } }
  • 26. Dagger 2 • Some catches when using field injection • Make explicit provisions @Module
 public class AppModule {
 
 @Provides @Singleton
 ProviderB provideProvider2(ProviderC providerC) {
 return new ProviderB(providerC);
 }
 
 @Provides
 ProviderD provideProvider4() {
 return new ProviderD();
 }
 } • Beware marking constructors with @Inject when providing explicitly • may create unwanted double provision
  • 28. Mocking • Android framework has some historical things • package android.test.mock • for instrumentation tests
  • 29. Android APIs & Unit Test • historically • java.lang.RuntimeException: Stub! • now • mockable android.jar • Method ... not mocked.
  • 30. Mocking • it’s important to “program to an interface” • avoid final and static
  • 32. @RunWith(JUnit4.class)
 public class ProviderATest {
 
 @Mock ProviderC mProviderC;
 
 @Mock ProviderD mProviderD;
 
 ProviderA mProviderA;
 
 @Before
 public void setUp() throws Exception {
 MockitoAnnotations.initMocks(this);
 Mockito.when(mProviderC.getString()).thenReturn("hello");
 Mockito.when(mProviderD.getString()).thenReturn("world");
 mProviderA = new ProviderA(mProviderC, mProviderD);
 } … }
  • 34. Static methods • avoid it as much as possible • things like TextUtils.isEmpty() can be painful • alternatives, e.g. Apache commons StringUtils.isEmpty() • PowerMock to the rescue • still better to avoid static
  • 36. Negatives? • increase the number of methods and classes • duplication of Android interfaces/classes • bloated constructors with lots of dependencies
  • 38. Dev App Builds • How to use dev overrides in dev buils? • utilize Android’s Gradle build • Product Flavors can be used for that
  • 40. Recommended Libraries • Dagger 2 • Retrofit • RxJava / RxAndroid • Mortar
  • 42. Q&A