SlideShare a Scribd company logo
1 of 29
Download to read offline
Robolectric
Test Android on the JVM
HELLO!
I am Richard Chang
Android Engineer in HTC, Movial and VMFive
You can find me via chiel99 AT gmail.com
Outline
● Challenges of Android unit test
● Robolectric
● PowerMock
● JaCoCo
● Reference
Challenges of
Android unit test
1. java.lang.RuntimeException(“Stub!”)
Google removed the method body in android.jar
2. Mock everything
Mock framework is great!
But mocking every Android framework object exhausts
programmers
3. On Real Device or Emulator
Impact your Continueous Integration (CI) process
SLOW, SLOW, SLOW
Robolectric
What is Robolectric?
● An Android unit test framework that let you run your tests on
regular JVM
● Start from Xtreme Labs (Acquired by Pivotal Labs)
● Open source
Why Robolectric?
● Fast
● Run on JVM instead of Dalvik/ART
○ No dexing, packaging, signing, deploying...
● Well handled the resources/layout xmls emulation
○ findViewById(R.id.text)
● Can be collaborated with other mock frameworks
○ Mockito, PowerMock...
How it works?
● Shadow objects
○ Robolectric replaces Android classes so-called Shadows
● Robolectric intercepts the loading of Android classes during testing
Android
ImageView
Test code
Shadow
ImageView
mImgView.getImageBitmap getImageBitmap
return Bitmapreturn Bitmap
Shadows
How to use Robolectric?
● Build.gradle
○ dependencies: testCompile "org.robolectric:robolectric:3.0"
● Create test folder: PROJECT/app/src/test/java/...
How to use Robolectric?
● @RunWith(RobolectricGradleTestRunner.class)
● @Config(constants = BuildConfig.class)
● @Config(sdk = Build.VERSION_CODES.KITKAT)
● @Config(manifest = “some-path/AndroidManifest.xml”)
● @Config(qualifiers = “en-port-hdpi”)
● ./gradlew testDebug
● ./gradlew testDebug --tests=”*.TestClassName”
Demo
Drawbacks
● Can not follow the latest Android version
○ Robolectric 3.0 supports API 21 (Lollipop) - 2015 August
● Can not cover OEM implementation
○ Still need real devices for integration tests
● Still have bugs
○ Please report or improve it
PowerMock
What is PowerMock?
● Mock framework extends on EasyMock and Mockito
● To enable mocking on:
○ constructors
○ static methods
○ final classes and methods
○ private methods
● Sorry, can not run on Dalvik VM
○ Thanks again for Robolectric
How to use PowerMock?
● Build.gradle
dependencies {
testCompile "org.powermock:powermock-module-junit4:1.6.1"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.1"
testCompile "org.powermock:powermock-classloading-xstream:1.6.1"
testCompile "org.powermock:powermock-api-mockito:1.6.1"
}
How to use PowerMock?
● @PrepareForTest(ClassName.class)
● constructor: whenNew()
● static method: mockStatic() or stub()
● final or private methods: spy()
● private member: WhiteBox.setInternalState()
Demo
JaCoCo
A story about one
million dollars
What is Jacoco?
● Java Code Coverage Library
● Line and branch coverage
● Without injecting code inside your project
● Good integration with CI tools like Jenkins
● Benefits:
○ Confidence for Team
○ Happiness for Boss
○ Avoid writing duplicated test code
How to use JaCoCo?
● build.gradle
○ apply jacoco plugin
○ setup jacoco toolVersion
○ add JacocoReport task and depends on testDebug
○ setup report format (xml, html, csv)
● Run test with jacoco
○ ./gradlew clean jacocoTestReportTask
Demo
Thanks!
Any questions?
You can find the sample code on github:
https://github.com/chiel99/robolectricDemo
VMFive is hiring!
● Android Engineer
● Backend Engineer (Node.js or Python)
● Cloud Engineer in Operation
● Cloud Engineer in Test
● Taiwan Businese Development
● Data Scientist
● Campaign Testing
http://vmfive.com
Reference
● http://robolectric.org/
● https://github.com/robolectric/robolectric
● http://www.slideshare.net/joemoore1/droidconuk-tdd-android-
with-robolectric
● https://github.com/jayway/powermock
● http://eclemma.org/jacoco/index.html

More Related Content

What's hot

The Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies TutorialThe Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies TutorialAlan Richardson
 
Behaviour Testing and Continuous Integration with Drupal
Behaviour Testing and Continuous Integration with DrupalBehaviour Testing and Continuous Integration with Drupal
Behaviour Testing and Continuous Integration with Drupalsmithmilner
 
Introduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibusIntroduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibusDavide Fella
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksEndranNL
 
Efficient JavaScript Unit Testing, May 2012
Efficient JavaScript Unit Testing, May 2012Efficient JavaScript Unit Testing, May 2012
Efficient JavaScript Unit Testing, May 2012Hazem Saleh
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsClare Macrae
 
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and Assertions
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and AssertionsCore Java Programming Language (JSE) : Chapter VIII - Exceptions and Assertions
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and AssertionsWebStackAcademy
 
Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018
Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018
Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018Promet Source
 
Regexp in Android and Java
Regexp in Android and JavaRegexp in Android and Java
Regexp in Android and JavaKeishin Yokomaku
 
Gestión de infraestructura tomcat/Tom EE con tfactory
Gestión de infraestructura tomcat/Tom EE con tfactoryGestión de infraestructura tomcat/Tom EE con tfactory
Gestión de infraestructura tomcat/Tom EE con tfactoryGuatemala User Group
 
TDD with Python and App Engine
TDD with Python and App EngineTDD with Python and App Engine
TDD with Python and App EngineRicardo Bánffy
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Testing for Android: When, Where, and How to Successfully Use Test Automation
Testing for Android: When, Where, and How to Successfully Use Test AutomationTesting for Android: When, Where, and How to Successfully Use Test Automation
Testing for Android: When, Where, and How to Successfully Use Test AutomationTrent Peterson
 
The state of Jenkins pipelines or do I still need freestyle jobs
The state of Jenkins pipelines or do I still need freestyle jobsThe state of Jenkins pipelines or do I still need freestyle jobs
The state of Jenkins pipelines or do I still need freestyle jobsAndrey Devyatkin
 
Test Driven Development in Python
Test Driven Development in PythonTest Driven Development in Python
Test Driven Development in PythonAnoop Thomas Mathew
 
Bgoug 2019.11 building free, open-source, plsql products in cloud
Bgoug 2019.11   building free, open-source, plsql products in cloudBgoug 2019.11   building free, open-source, plsql products in cloud
Bgoug 2019.11 building free, open-source, plsql products in cloudJacek Gebal
 

What's hot (20)

The Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies TutorialThe Evil Tester's Guide to HTTP proxies Tutorial
The Evil Tester's Guide to HTTP proxies Tutorial
 
Gamedev-grade debugging
Gamedev-grade debuggingGamedev-grade debugging
Gamedev-grade debugging
 
Behaviour Testing and Continuous Integration with Drupal
Behaviour Testing and Continuous Integration with DrupalBehaviour Testing and Continuous Integration with Drupal
Behaviour Testing and Continuous Integration with Drupal
 
Introduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibusIntroduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibus
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworks
 
Efficient JavaScript Unit Testing, May 2012
Efficient JavaScript Unit Testing, May 2012Efficient JavaScript Unit Testing, May 2012
Efficient JavaScript Unit Testing, May 2012
 
Quickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop ApplicationsQuickly Testing Qt Desktop Applications
Quickly Testing Qt Desktop Applications
 
Spock pres
Spock presSpock pres
Spock pres
 
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and Assertions
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and AssertionsCore Java Programming Language (JSE) : Chapter VIII - Exceptions and Assertions
Core Java Programming Language (JSE) : Chapter VIII - Exceptions and Assertions
 
Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018
Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018
Unit test in drupal 8 by Pratomo Ardianto Drupalcamp Cebu 2018
 
Regexp in Android and Java
Regexp in Android and JavaRegexp in Android and Java
Regexp in Android and Java
 
Testing in go
Testing in goTesting in go
Testing in go
 
Gestión de infraestructura tomcat/Tom EE con tfactory
Gestión de infraestructura tomcat/Tom EE con tfactoryGestión de infraestructura tomcat/Tom EE con tfactory
Gestión de infraestructura tomcat/Tom EE con tfactory
 
JMockit
JMockitJMockit
JMockit
 
TDD with Python and App Engine
TDD with Python and App EngineTDD with Python and App Engine
TDD with Python and App Engine
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Testing for Android: When, Where, and How to Successfully Use Test Automation
Testing for Android: When, Where, and How to Successfully Use Test AutomationTesting for Android: When, Where, and How to Successfully Use Test Automation
Testing for Android: When, Where, and How to Successfully Use Test Automation
 
The state of Jenkins pipelines or do I still need freestyle jobs
The state of Jenkins pipelines or do I still need freestyle jobsThe state of Jenkins pipelines or do I still need freestyle jobs
The state of Jenkins pipelines or do I still need freestyle jobs
 
Test Driven Development in Python
Test Driven Development in PythonTest Driven Development in Python
Test Driven Development in Python
 
Bgoug 2019.11 building free, open-source, plsql products in cloud
Bgoug 2019.11   building free, open-source, plsql products in cloudBgoug 2019.11   building free, open-source, plsql products in cloud
Bgoug 2019.11 building free, open-source, plsql products in cloud
 

Similar to Robolectric android taipei

Unit Test Android Without Going Bald
Unit Test Android Without Going BaldUnit Test Android Without Going Bald
Unit Test Android Without Going BaldDavid Carver
 
The State of the Veil Framework
The State of the Veil FrameworkThe State of the Veil Framework
The State of the Veil FrameworkVeilFramework
 
Mockito 2.x Migration - Droidcon UK 2018
Mockito 2.x Migration - Droidcon UK 2018Mockito 2.x Migration - Droidcon UK 2018
Mockito 2.x Migration - Droidcon UK 2018Hazem Saleh
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Mutual Mobile
 
Eclipse Buildship JUG Hamburg
Eclipse Buildship JUG HamburgEclipse Buildship JUG Hamburg
Eclipse Buildship JUG Hamburgsimonscholz
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAhmed Ehab AbdulAziz
 
Micronaut: A new way to build microservices
Micronaut: A new way to build microservicesMicronaut: A new way to build microservices
Micronaut: A new way to build microservicesLuram Archanjo
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android ApplicationsRody Middelkoop
 
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been ToldDCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been ToldDocker, Inc.
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sampleprahalad_das_in
 
Mocking vtcc3 - en
Mocking   vtcc3 - enMocking   vtcc3 - en
Mocking vtcc3 - envgrondin
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etcYaron Karni
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplateStanislav Petrov
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS MeetupLINAGORA
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and DevelopmentOpersys inc.
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVMSylvain Wallez
 

Similar to Robolectric android taipei (20)

Unit Test Android Without Going Bald
Unit Test Android Without Going BaldUnit Test Android Without Going Bald
Unit Test Android Without Going Bald
 
Unit testing hippo
Unit testing hippoUnit testing hippo
Unit testing hippo
 
The State of the Veil Framework
The State of the Veil FrameworkThe State of the Veil Framework
The State of the Veil Framework
 
Mockito 2.x Migration - Droidcon UK 2018
Mockito 2.x Migration - Droidcon UK 2018Mockito 2.x Migration - Droidcon UK 2018
Mockito 2.x Migration - Droidcon UK 2018
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
 
Eclipse Buildship JUG Hamburg
Eclipse Buildship JUG HamburgEclipse Buildship JUG Hamburg
Eclipse Buildship JUG Hamburg
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDD
 
Micronaut: A new way to build microservices
Micronaut: A new way to build microservicesMicronaut: A new way to build microservices
Micronaut: A new way to build microservices
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been ToldDCSF19 Docker Containers & Java: What I Wish I Had Been Told
DCSF19 Docker Containers & Java: What I Wish I Had Been Told
 
Javascript training sample
Javascript training sampleJavascript training sample
Javascript training sample
 
Mocking vtcc3 - en
Mocking   vtcc3 - enMocking   vtcc3 - en
Mocking vtcc3 - en
 
Junit, mockito, etc
Junit, mockito, etcJunit, mockito, etc
Junit, mockito, etc
 
Creating a reasonable project boilerplate
Creating a reasonable project boilerplateCreating a reasonable project boilerplate
Creating a reasonable project boilerplate
 
Mock your way with Mockito
Mock your way with MockitoMock your way with Mockito
Mock your way with Mockito
 
Advanced Node.JS Meetup
Advanced Node.JS MeetupAdvanced Node.JS Meetup
Advanced Node.JS Meetup
 
Android Platform Debugging and Development
Android Platform Debugging and DevelopmentAndroid Platform Debugging and Development
Android Platform Debugging and Development
 
Robolectric v2
Robolectric v2Robolectric v2
Robolectric v2
 
Why Concurrency is hard ?
Why Concurrency is hard ?Why Concurrency is hard ?
Why Concurrency is hard ?
 
Native Java with GraalVM
Native Java with GraalVMNative Java with GraalVM
Native Java with GraalVM
 

Recently uploaded

Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 

Recently uploaded (20)

Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 

Robolectric android taipei

  • 2. HELLO! I am Richard Chang Android Engineer in HTC, Movial and VMFive You can find me via chiel99 AT gmail.com
  • 3. Outline ● Challenges of Android unit test ● Robolectric ● PowerMock ● JaCoCo ● Reference
  • 6. 2. Mock everything Mock framework is great! But mocking every Android framework object exhausts programmers
  • 7. 3. On Real Device or Emulator Impact your Continueous Integration (CI) process SLOW, SLOW, SLOW
  • 9. What is Robolectric? ● An Android unit test framework that let you run your tests on regular JVM ● Start from Xtreme Labs (Acquired by Pivotal Labs) ● Open source
  • 10. Why Robolectric? ● Fast ● Run on JVM instead of Dalvik/ART ○ No dexing, packaging, signing, deploying... ● Well handled the resources/layout xmls emulation ○ findViewById(R.id.text) ● Can be collaborated with other mock frameworks ○ Mockito, PowerMock...
  • 11. How it works? ● Shadow objects ○ Robolectric replaces Android classes so-called Shadows ● Robolectric intercepts the loading of Android classes during testing Android ImageView Test code Shadow ImageView mImgView.getImageBitmap getImageBitmap return Bitmapreturn Bitmap
  • 13. How to use Robolectric? ● Build.gradle ○ dependencies: testCompile "org.robolectric:robolectric:3.0" ● Create test folder: PROJECT/app/src/test/java/...
  • 14. How to use Robolectric? ● @RunWith(RobolectricGradleTestRunner.class) ● @Config(constants = BuildConfig.class) ● @Config(sdk = Build.VERSION_CODES.KITKAT) ● @Config(manifest = “some-path/AndroidManifest.xml”) ● @Config(qualifiers = “en-port-hdpi”) ● ./gradlew testDebug ● ./gradlew testDebug --tests=”*.TestClassName”
  • 15. Demo
  • 16. Drawbacks ● Can not follow the latest Android version ○ Robolectric 3.0 supports API 21 (Lollipop) - 2015 August ● Can not cover OEM implementation ○ Still need real devices for integration tests ● Still have bugs ○ Please report or improve it
  • 18. What is PowerMock? ● Mock framework extends on EasyMock and Mockito ● To enable mocking on: ○ constructors ○ static methods ○ final classes and methods ○ private methods ● Sorry, can not run on Dalvik VM ○ Thanks again for Robolectric
  • 19. How to use PowerMock? ● Build.gradle dependencies { testCompile "org.powermock:powermock-module-junit4:1.6.1" testCompile "org.powermock:powermock-module-junit4-rule:1.6.1" testCompile "org.powermock:powermock-classloading-xstream:1.6.1" testCompile "org.powermock:powermock-api-mockito:1.6.1" }
  • 20. How to use PowerMock? ● @PrepareForTest(ClassName.class) ● constructor: whenNew() ● static method: mockStatic() or stub() ● final or private methods: spy() ● private member: WhiteBox.setInternalState()
  • 21. Demo
  • 23. A story about one million dollars
  • 24. What is Jacoco? ● Java Code Coverage Library ● Line and branch coverage ● Without injecting code inside your project ● Good integration with CI tools like Jenkins ● Benefits: ○ Confidence for Team ○ Happiness for Boss ○ Avoid writing duplicated test code
  • 25. How to use JaCoCo? ● build.gradle ○ apply jacoco plugin ○ setup jacoco toolVersion ○ add JacocoReport task and depends on testDebug ○ setup report format (xml, html, csv) ● Run test with jacoco ○ ./gradlew clean jacocoTestReportTask
  • 26. Demo
  • 27. Thanks! Any questions? You can find the sample code on github: https://github.com/chiel99/robolectricDemo
  • 28. VMFive is hiring! ● Android Engineer ● Backend Engineer (Node.js or Python) ● Cloud Engineer in Operation ● Cloud Engineer in Test ● Taiwan Businese Development ● Data Scientist ● Campaign Testing http://vmfive.com
  • 29. Reference ● http://robolectric.org/ ● https://github.com/robolectric/robolectric ● http://www.slideshare.net/joemoore1/droidconuk-tdd-android- with-robolectric ● https://github.com/jayway/powermock ● http://eclemma.org/jacoco/index.html