SlideShare a Scribd company logo
1 of 17
Unit Testing Android without Going
Bald!
CodeMash 2015
David Carver
Gplus: +David Carver
Twitter: @kingargyle
https://github.com/NineWorlds/serenity-android
What is Unit Testing??
● Issolated tests to a particular class.
● Does not test the functionality of other classes.
● Tests only the public methods.
– May test the protected methods
● Tests the expected output of a method.
Android Testing
● Difficult to Unit Test
● Must run on device or emulator
– Emulator slow to start up and execute
– Devices must be connected to CI machines
● Slow startup and execution times.
● Developers do not run the tests as often or do
not even write tests.
● Leading to untested code, and more manual
testing or only Integration tests.
Why not just use AndroidTestCase
● Slow and requires an Emulator or Device to
run.
● Can use Android Mocks for Activities and
Services but still requires a device or emulator.
● Requires a separate project for tests.
● Tests and code are not kept together.
● Uses Junit and not Junit 4.
What other Options?
● Robotium – Integration / Functional testing
framework that tests from the UI. Not
designed for Unit Testing.
● Calabash – Ruby/Cucumber type framework.
More suited for Integration / Behavior tests.
● UI Automator – Extremely slow. Integration
testing
● MonekyRunner – Integration Testing.
● All of these require a device or emulator to
run.
Unit Testing Goals
● Must be fast otherwise developers won't run
them and won't write them.
● Should be written in the same language. Less
learning curve.
● Should integrate with existing tools and IDEs.
● Must provide a fast feedback on the
development cycle.
● Ideally integrate with code coverage tools and
easy integration as part of the build.
Rogaine for Android Unit Testing
● Junit 4 – Provides a more modern way to test.
● Mockito
– Mocking Framework
– Can mock concrete classes
– Fluent style API for easy readability and setup
– Can spy on existing methods and classes
● Robolectric – Provides a Mocking Framework
and TestRunner for Android
● Dagger – Dependency Injection Framework
Robolectric
● Mocking framework for the Android Platform.
– Provides mock implementations for various
Platform versions.
– Allows Tests to run on the Java VM instead of a
device or emulator.
– Tests run locally
– Feedback cycle is seconds instead of minutes.
– Open Source project with large community
– https://github.com/robolectric/robolectric
Mockito
● Mocking Framework that does the heavy lifting
for you.
● doReturn(true).when(mockView).isVisible()
– When the method isVisible is called return true as
the result.
– Mock code never executes real implementations.
● Allows for issolation of the Class/Method
under tests from it's dependencies.
● Helps focus testing to the result of the method
and not testing of other frameworks.
Mockito
● View spyView = spy(realView)
● doReturn(true).when(spyView).isVisible()
– When the method isVisible is called return true as
the result.
– Mock code never executes real implementations.
● verify(spyView).isVisible()
– Allows verification that a method is called without
actually calling the underlying code.
– Good for mocking out protected methods that
otherwise aren't accessible.
Real World Results
● Selenium Android Driver – extremely slow
● UI Automator – 20 tests – 8 minutes to
execute.
● Robolectric, Mockito, plus RoboGuice –
better, 600 tests, about 8 minutes to execute.
● Robolectric, Mockito, plus Dagger – 1700
tests, 3 minutes to execute.
– Code Coverage using Jacoco – 91%
● Robolectric allows for integration with existing
Technical Debt tools like Sonar.
Test Examples
● Simple Junit 4 test
● Robolectric Activity Test
● Robolectric plus Mockito Test – (Views)
● https://github.com/NineWorlds/serenity-
android/tree/master/serenity-app/src/test/java
Making your Code Testable
● Avoid Anonymous Inner Classes
– Hard to write unit tests for.
– End up writing Integration Tests
● Try to write classes and methods that follow
the Single Responsibility Principal.
● Listeners should be implemented in their own
Classes, not in Activities!
● Use Dependency Injection
– Allows for easy injection of Mocks for tests
● Activities should just be lifecycle managers
Conclusion
● Robolectric does not replace Integration or
Functional Tests.
● Robotium and UI Automator still have their
places.
● DO NOT Use Robolectric as an Integration
Test platform it is a Mocking Framework.
● Use Robolectric.buildActivity() sparingly. Has
high startup overhead.
● Mockito can mock anything in the Android
Framework, but may have large number of
mocks.
Conclusion
● Running tests locally is 10 times faster and
productive than running tests on a device.
● The more tests the less likely you are to have
critical bugs slip through.
● If you get a bug...WRITE A TEST FOR IT.
● Provides a regression test suite when multiple
developers are working on the code.
● Lets you keep what little hair you may have
left.

More Related Content

What's hot

不只自動化而且更敏捷的Android開發工具 gradle
不只自動化而且更敏捷的Android開發工具 gradle不只自動化而且更敏捷的Android開發工具 gradle
不只自動化而且更敏捷的Android開發工具 gradlesam chiu
 
Automate your build on Android with Jenkins
Automate your build on Android with JenkinsAutomate your build on Android with Jenkins
Automate your build on Android with JenkinsBeMyApp
 
GitHub Actions for 5 minutes
GitHub Actions for 5 minutesGitHub Actions for 5 minutes
GitHub Actions for 5 minutesSvetlin Nakov
 
2013 10-28 php ug presentation - ci using phing and hudson
2013 10-28 php ug presentation - ci using phing and hudson2013 10-28 php ug presentation - ci using phing and hudson
2013 10-28 php ug presentation - ci using phing and hudsonShreeniwas Iyer
 
Introducing Playwright's New Test Runner
Introducing Playwright's New Test RunnerIntroducing Playwright's New Test Runner
Introducing Playwright's New Test RunnerApplitools
 
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.UA Mobile
 
From devOps to front end Ops, test first
From devOps to front end Ops, test firstFrom devOps to front end Ops, test first
From devOps to front end Ops, test firstCaesar Chi
 
Setup Build & Deploy with Jenkins CI
Setup Build & Deploy with Jenkins CISetup Build & Deploy with Jenkins CI
Setup Build & Deploy with Jenkins CIwalming
 
基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構玄武 Wu
 
Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)Adam Štipák
 
Apache Maven - eXo VN office presentation
Apache Maven - eXo VN office presentationApache Maven - eXo VN office presentation
Apache Maven - eXo VN office presentationArnaud Héritier
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osArnav Gupta
 
TDD on OSGi, in practice.
TDD on OSGi, in practice.TDD on OSGi, in practice.
TDD on OSGi, in practice.Elian, I.
 
Trying to Sell PVS-Studio to Google, or New Bugs in Chromium
Trying to Sell PVS-Studio to Google, or New Bugs in ChromiumTrying to Sell PVS-Studio to Google, or New Bugs in Chromium
Trying to Sell PVS-Studio to Google, or New Bugs in ChromiumAndrey Karpov
 
DEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android ApplicationsDEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android ApplicationsKyungmin Lee
 
Test Driven Development with OSGi - Balázs Zsoldos
Test Driven Development with OSGi - Balázs ZsoldosTest Driven Development with OSGi - Balázs Zsoldos
Test Driven Development with OSGi - Balázs Zsoldosmfrancis
 
Discovery the p2 API (updated to Indigo)
Discovery the p2 API (updated to Indigo)Discovery the p2 API (updated to Indigo)
Discovery the p2 API (updated to Indigo)Pascal Rapicault
 

What's hot (20)

Maven basics
Maven basicsMaven basics
Maven basics
 
不只自動化而且更敏捷的Android開發工具 gradle
不只自動化而且更敏捷的Android開發工具 gradle不只自動化而且更敏捷的Android開發工具 gradle
不只自動化而且更敏捷的Android開發工具 gradle
 
Automate your build on Android with Jenkins
Automate your build on Android with JenkinsAutomate your build on Android with Jenkins
Automate your build on Android with Jenkins
 
GitHub Actions for 5 minutes
GitHub Actions for 5 minutesGitHub Actions for 5 minutes
GitHub Actions for 5 minutes
 
2013 10-28 php ug presentation - ci using phing and hudson
2013 10-28 php ug presentation - ci using phing and hudson2013 10-28 php ug presentation - ci using phing and hudson
2013 10-28 php ug presentation - ci using phing and hudson
 
Introducing Playwright's New Test Runner
Introducing Playwright's New Test RunnerIntroducing Playwright's New Test Runner
Introducing Playwright's New Test Runner
 
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.
Критика "библиотечного" подхода в разработке под Android. UA Mobile 2016.
 
From devOps to front end Ops, test first
From devOps to front end Ops, test firstFrom devOps to front end Ops, test first
From devOps to front end Ops, test first
 
Setup Build & Deploy with Jenkins CI
Setup Build & Deploy with Jenkins CISetup Build & Deploy with Jenkins CI
Setup Build & Deploy with Jenkins CI
 
基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構
 
Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)Testing with Codeception (Webelement #30)
Testing with Codeception (Webelement #30)
 
Apache Maven - eXo VN office presentation
Apache Maven - eXo VN office presentationApache Maven - eXo VN office presentation
Apache Maven - eXo VN office presentation
 
Enhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_osEnhancing and modifying_the_core_android_os
Enhancing and modifying_the_core_android_os
 
TDD on OSGi, in practice.
TDD on OSGi, in practice.TDD on OSGi, in practice.
TDD on OSGi, in practice.
 
Trying to Sell PVS-Studio to Google, or New Bugs in Chromium
Trying to Sell PVS-Studio to Google, or New Bugs in ChromiumTrying to Sell PVS-Studio to Google, or New Bugs in Chromium
Trying to Sell PVS-Studio to Google, or New Bugs in Chromium
 
DEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android ApplicationsDEVIEW2013: Automating Performance Tests for Android Applications
DEVIEW2013: Automating Performance Tests for Android Applications
 
Gradle
GradleGradle
Gradle
 
Test Driven Development with OSGi - Balázs Zsoldos
Test Driven Development with OSGi - Balázs ZsoldosTest Driven Development with OSGi - Balázs Zsoldos
Test Driven Development with OSGi - Balázs Zsoldos
 
Apache Maven
Apache MavenApache Maven
Apache Maven
 
Discovery the p2 API (updated to Indigo)
Discovery the p2 API (updated to Indigo)Discovery the p2 API (updated to Indigo)
Discovery the p2 API (updated to Indigo)
 

Similar to Unit Test Android Without Going Bald

Testing on Android
Testing on AndroidTesting on Android
Testing on AndroidAri Lacenski
 
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
 
Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Grzegorz Miejski
 
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2
 
3x3: Speeding Up Mobile Releases
3x3: Speeding Up Mobile Releases3x3: Speeding Up Mobile Releases
3x3: Speeding Up Mobile ReleasesDrew Hannay
 
Katalon Studio - A Codeless Automation Tool.pdf
Katalon Studio - A Codeless Automation Tool.pdfKatalon Studio - A Codeless Automation Tool.pdf
Katalon Studio - A Codeless Automation Tool.pdfKnoldus Inc.
 
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...Bruno Tanoue
 
DevOps presentation
DevOps presentationDevOps presentation
DevOps presentationAxsh Co. LTD
 
Test Driven Development using QUnit
Test Driven Development using QUnitTest Driven Development using QUnit
Test Driven Development using QUnitsatejsahu
 
Mobile App Quality Roadmap for DevTest Teams
Mobile App Quality Roadmap for DevTest TeamsMobile App Quality Roadmap for DevTest Teams
Mobile App Quality Roadmap for DevTest TeamsPerfecto by Perforce
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Inside Android Testing
Inside Android TestingInside Android Testing
Inside Android TestingFernando Cejas
 
Reliable mobile test automation
Reliable mobile test automationReliable mobile test automation
Reliable mobile test automationVishal Banthia
 
Expedia 3x3 presentation
Expedia 3x3 presentationExpedia 3x3 presentation
Expedia 3x3 presentationDrew Hannay
 
Introduction to Test Automation
Introduction to Test AutomationIntroduction to Test Automation
Introduction to Test AutomationPekka Klärck
 

Similar to Unit Test Android Without Going Bald (20)

Groovy android
Groovy androidGroovy android
Groovy android
 
Testing on Android
Testing on AndroidTesting on Android
Testing on Android
 
Android testing
Android testingAndroid testing
Android testing
 
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...
 
Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019Tests immutable when refactoring - SegFault Unconference Cracow 2019
Tests immutable when refactoring - SegFault Unconference Cracow 2019
 
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile EnvironmentWSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
WSO2Con Asia 2014 - Effective Test Automation in an Agile Environment
 
Wso2con test-automation
Wso2con test-automationWso2con test-automation
Wso2con test-automation
 
3x3 Speeding Up Mobile Releases
3x3 Speeding Up Mobile Releases3x3 Speeding Up Mobile Releases
3x3 Speeding Up Mobile Releases
 
3x3: Speeding Up Mobile Releases
3x3: Speeding Up Mobile Releases3x3: Speeding Up Mobile Releases
3x3: Speeding Up Mobile Releases
 
Katalon Studio - A Codeless Automation Tool.pdf
Katalon Studio - A Codeless Automation Tool.pdfKatalon Studio - A Codeless Automation Tool.pdf
Katalon Studio - A Codeless Automation Tool.pdf
 
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...[ENGLISH] TDC 2015 - PHP  Trail - Tests and PHP Continuous Integration Enviro...
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
 
Test automation proposal
Test automation proposalTest automation proposal
Test automation proposal
 
DevOps presentation
DevOps presentationDevOps presentation
DevOps presentation
 
Test Driven Development using QUnit
Test Driven Development using QUnitTest Driven Development using QUnit
Test Driven Development using QUnit
 
Mobile App Quality Roadmap for DevTest Teams
Mobile App Quality Roadmap for DevTest TeamsMobile App Quality Roadmap for DevTest Teams
Mobile App Quality Roadmap for DevTest Teams
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Inside Android Testing
Inside Android TestingInside Android Testing
Inside Android Testing
 
Reliable mobile test automation
Reliable mobile test automationReliable mobile test automation
Reliable mobile test automation
 
Expedia 3x3 presentation
Expedia 3x3 presentationExpedia 3x3 presentation
Expedia 3x3 presentation
 
Introduction to Test Automation
Introduction to Test AutomationIntroduction to Test Automation
Introduction to Test Automation
 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 

Unit Test Android Without Going Bald

  • 1. Unit Testing Android without Going Bald! CodeMash 2015 David Carver Gplus: +David Carver Twitter: @kingargyle https://github.com/NineWorlds/serenity-android
  • 2. What is Unit Testing?? ● Issolated tests to a particular class. ● Does not test the functionality of other classes. ● Tests only the public methods. – May test the protected methods ● Tests the expected output of a method.
  • 3. Android Testing ● Difficult to Unit Test ● Must run on device or emulator – Emulator slow to start up and execute – Devices must be connected to CI machines ● Slow startup and execution times. ● Developers do not run the tests as often or do not even write tests. ● Leading to untested code, and more manual testing or only Integration tests.
  • 4.
  • 5. Why not just use AndroidTestCase ● Slow and requires an Emulator or Device to run. ● Can use Android Mocks for Activities and Services but still requires a device or emulator. ● Requires a separate project for tests. ● Tests and code are not kept together. ● Uses Junit and not Junit 4.
  • 6. What other Options? ● Robotium – Integration / Functional testing framework that tests from the UI. Not designed for Unit Testing. ● Calabash – Ruby/Cucumber type framework. More suited for Integration / Behavior tests. ● UI Automator – Extremely slow. Integration testing ● MonekyRunner – Integration Testing. ● All of these require a device or emulator to run.
  • 7. Unit Testing Goals ● Must be fast otherwise developers won't run them and won't write them. ● Should be written in the same language. Less learning curve. ● Should integrate with existing tools and IDEs. ● Must provide a fast feedback on the development cycle. ● Ideally integrate with code coverage tools and easy integration as part of the build.
  • 8. Rogaine for Android Unit Testing ● Junit 4 – Provides a more modern way to test. ● Mockito – Mocking Framework – Can mock concrete classes – Fluent style API for easy readability and setup – Can spy on existing methods and classes ● Robolectric – Provides a Mocking Framework and TestRunner for Android ● Dagger – Dependency Injection Framework
  • 9. Robolectric ● Mocking framework for the Android Platform. – Provides mock implementations for various Platform versions. – Allows Tests to run on the Java VM instead of a device or emulator. – Tests run locally – Feedback cycle is seconds instead of minutes. – Open Source project with large community – https://github.com/robolectric/robolectric
  • 10. Mockito ● Mocking Framework that does the heavy lifting for you. ● doReturn(true).when(mockView).isVisible() – When the method isVisible is called return true as the result. – Mock code never executes real implementations. ● Allows for issolation of the Class/Method under tests from it's dependencies. ● Helps focus testing to the result of the method and not testing of other frameworks.
  • 11. Mockito ● View spyView = spy(realView) ● doReturn(true).when(spyView).isVisible() – When the method isVisible is called return true as the result. – Mock code never executes real implementations. ● verify(spyView).isVisible() – Allows verification that a method is called without actually calling the underlying code. – Good for mocking out protected methods that otherwise aren't accessible.
  • 12. Real World Results ● Selenium Android Driver – extremely slow ● UI Automator – 20 tests – 8 minutes to execute. ● Robolectric, Mockito, plus RoboGuice – better, 600 tests, about 8 minutes to execute. ● Robolectric, Mockito, plus Dagger – 1700 tests, 3 minutes to execute. – Code Coverage using Jacoco – 91% ● Robolectric allows for integration with existing Technical Debt tools like Sonar.
  • 13.
  • 14. Test Examples ● Simple Junit 4 test ● Robolectric Activity Test ● Robolectric plus Mockito Test – (Views) ● https://github.com/NineWorlds/serenity- android/tree/master/serenity-app/src/test/java
  • 15. Making your Code Testable ● Avoid Anonymous Inner Classes – Hard to write unit tests for. – End up writing Integration Tests ● Try to write classes and methods that follow the Single Responsibility Principal. ● Listeners should be implemented in their own Classes, not in Activities! ● Use Dependency Injection – Allows for easy injection of Mocks for tests ● Activities should just be lifecycle managers
  • 16. Conclusion ● Robolectric does not replace Integration or Functional Tests. ● Robotium and UI Automator still have their places. ● DO NOT Use Robolectric as an Integration Test platform it is a Mocking Framework. ● Use Robolectric.buildActivity() sparingly. Has high startup overhead. ● Mockito can mock anything in the Android Framework, but may have large number of mocks.
  • 17. Conclusion ● Running tests locally is 10 times faster and productive than running tests on a device. ● The more tests the less likely you are to have critical bugs slip through. ● If you get a bug...WRITE A TEST FOR IT. ● Provides a regression test suite when multiple developers are working on the code. ● Lets you keep what little hair you may have left.