Top Banner
Robolectric Test Android on the JVM
29

Robolectric android taipei

Apr 13, 2017

Download

Engineering

Richard Chang
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Robolectric   android taipei

RobolectricTest Android on the JVM

Page 2: Robolectric   android taipei

HELLO!

I am Richard ChangAndroid Engineer in HTC, Movial and VMFiveYou can find me via chiel99 AT gmail.com

Page 3: Robolectric   android taipei

Outline● Challenges of Android unit test ● Robolectric● PowerMock● JaCoCo● Reference

Page 4: Robolectric   android taipei

Challenges of Android unit test

Page 5: Robolectric   android taipei

1. java.lang.RuntimeException(“Stub!”)

Google removed the method body in android.jar

Page 6: Robolectric   android taipei

2. Mock everything

Mock framework is great!But mocking every Android framework object exhausts

programmers

Page 7: Robolectric   android taipei

3. On Real Device or Emulator

Impact your Continueous Integration (CI) processSLOW, SLOW, SLOW

Page 8: Robolectric   android taipei

Robolectric

Page 9: Robolectric   android taipei

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

Page 10: Robolectric   android taipei

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...

Page 11: Robolectric   android taipei

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

Page 12: Robolectric   android taipei

Shadows

Page 13: Robolectric   android taipei

How to use Robolectric?

● Build.gradle○ dependencies: testCompile "org.robolectric:robolectric:3.0"

● Create test folder: PROJECT/app/src/test/java/...

Page 14: Robolectric   android taipei

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”

Page 15: Robolectric   android taipei

Demo

Page 16: Robolectric   android taipei

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

Page 17: Robolectric   android taipei

PowerMock

Page 18: Robolectric   android taipei

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

Page 19: Robolectric   android taipei

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"}

Page 20: Robolectric   android taipei

How to use PowerMock?

● @PrepareForTest(ClassName.class)● constructor: whenNew()● static method: mockStatic() or stub()● final or private methods: spy()● private member: WhiteBox.setInternalState()

Page 21: Robolectric   android taipei

Demo

Page 22: Robolectric   android taipei

JaCoCo

Page 23: Robolectric   android taipei

A story about one million dollars

Page 24: Robolectric   android taipei

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

Page 25: Robolectric   android taipei

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

Page 26: Robolectric   android taipei

Demo

Page 27: Robolectric   android taipei

Thanks!Any questions?

You can find the sample code on github:https://github.com/chiel99/robolectricDemo

Page 28: Robolectric   android taipei

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