Top Banner
Dr. Droid or: How I Learned to Stop Worrying and Love the Unit-tests Android, Units, Robolectric
17
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: How I learned to stop worrying and love the unit tests

Dr. Droid or: How I Learned to Stop Worrying

and Love the Unit-testsAndroid, Units, Robolectric

Page 2: How I learned to stop worrying and love the unit tests

Who am I?

● Michael Pustovit

● Work at Stanfy as Android Engineer

● Moderator of KyivAndroidDevClub

Page 3: How I learned to stop worrying and love the unit tests

Our plan

1. What is unit-test

2. Unit testing in Android

3. Robolectric: why?

4. A bit of live-coding

Page 4: How I learned to stop worrying and love the unit tests

Unit tests

Unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.Wikipedia

Page 5: How I learned to stop worrying and love the unit tests

We use them in TDD

● TDD - test-driven development

● Software development process that relies on the repetition of a very short development cycle

Page 6: How I learned to stop worrying and love the unit tests
Page 7: How I learned to stop worrying and love the unit tests

Unit testing in Android

● Android initially has tools for unit testing

● Instrumentation tests (are executed on real

device or simulator)

● Really slow, too slow for TDD

Page 8: How I learned to stop worrying and love the unit tests

Why can’t we run tests on JVM?

● Android controls our program

● Our program use classes from Android OS

● Android = framework classes + set of services

● Execute all services on desktop = emulator

Page 9: How I learned to stop worrying and love the unit tests

But we can compile our program

● It’s because we have android.jar

● So we can run tests on JVM but...

● But android.jar is stub○ any method will throws RuntimeException(“Stub!”)○ not really… newest android.jar will throw

RuntimeException(“Method not mocked”)

Page 10: How I learned to stop worrying and love the unit tests

We’ll mock them all!

● We can mock android.jar○ android-gradle-plugin 1.1.0+ supports mock android.

jar● But there should be tons of mocks

Page 11: How I learned to stop worrying and love the unit tests
Page 12: How I learned to stop worrying and love the unit tests

Actually it’s what Robolectric does● Robolectric simulates Android

framework classes● Actually it executes Android

classes (or simulate theirs behaviour) on JVM

● As result, you can run your tests on JVM

Page 13: How I learned to stop worrying and love the unit tests

But how?!

● Robolectric intercepts class loading (javassist)

● For classes which can’t be executed on JVM Robolectric creates “shadows”

Page 14: How I learned to stop worrying and love the unit tests

Shadows

● Shadow mimics real class behaviour

● You know about shadow state (handy for

tests)

● You can create custom shadows

Page 16: How I learned to stop worrying and love the unit tests

Lets code!

https://github.com/lampapos/UnitTestSample

Page 17: How I learned to stop worrying and love the unit tests

Libs for testing

● AssertJ

● AssertJ Android

● Mockito

Cool assertions “assertThat().something()...”

Mocking framework