robolectric android taipei
TRANSCRIPT
HELLO!
I am Richard ChangAndroid Engineer in HTC, Movial and VMFiveYou can find me via chiel99 AT gmail.com
2. Mock everything
Mock framework is great!But mocking every Android framework object exhausts
programmers
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 codeShadow
ImageView
mImgView.getImageBitmap getImageBitmap
return Bitmapreturn Bitmap
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”
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
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()
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
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