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”
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()
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
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