How ANDROID TESTING
changed how we think about
death…
@fernando_cejas
Code samples:
https://github.com/android10/Inside_Android_Testing
https://github.com/android10/AndroidApplicationTestingSample
Second Edition
android10.org
Slide 2
Slide 2 text
Who am I… • Software Engineer
• GDG Barcelona Organizer
android10.org
• Android lover
• Geek
• Gintonic fan…
Slide 3
Slide 3 text
Agenda?
Slide 4
Slide 4 text
Why testing..WTF?
• Testing increases the level of confidence in
your code.
• Testing makes it possible to write new code,
and refactor existing code, without worrying
that you’ve broken existing functionality.
Slide 5
Slide 5 text
• Unit testing
• Integration Testing
• Functional and System Testing
1. Stress Testing
2. Performance Testing
3. Usability Testing
• Acceptance Testing
• Regression Testing
1. Smoke Testing
Types of tests…
Slide 6
Slide 6 text
Martin Fowler:
"...test-doubles ... preprogrammed with
expectation"
What is this mock thing?
Slide 7
Slide 7 text
Mockito is a mocking framework that tastes
really good.
Mockito doesn't give you
hangover because the tests
are very readable and they
produce clean verification
errors.
GIVE A WARM
WELCOME TO…
mockito
Slide 8
Slide 8 text
Mockito
Build-operate-check pattern
Slide 9
Slide 9 text
Mock initizalition
Slide 10
Slide 10 text
Initizalition using mockito test runner
Slide 11
Slide 11 text
Let’s verify some behaviour
Slide 12
Slide 12 text
Argument matchers
Slide 13
Slide 13 text
Verifying number of invocations / at
least x / never
Slide 14
Slide 14 text
Making sure interaction(s) never
happened on mock
Slide 15
Slide 15 text
Spying on real objects
Slide 16
Slide 16 text
Sounds familiar???
Slide 17
Slide 17 text
Allows you to run your android
code on JVM.
Allows loading of Android
Classes in pure Java
Projects.
TO THE RESCUE…
Slide 18
Slide 18 text
Shadow Objects!
Slide 19
Slide 19 text
View and Resource Loading…
Slide 20
Slide 20 text
Shadow Activity
Shadow ImageView
Slide 21
Slide 21 text
• Uses real Android SDK code
• Styles and themes supported
• System resources are available
• Performance improved
• Bugs fixed
Robolectric 2.0+
Slide 22
Slide 22 text
Perform click Sample
Slide 23
Slide 23 text
Started Activity test
Should have fragment test
Slide 24
Slide 24 text
Device Configuration Sample
Slide 25
Slide 25 text
Network sample
Robolectric acts as a proxy!!!
Slide 26
Slide 26 text
Creating your own shadows
Slide 27
Slide 27 text
Creating your own shadows
Slide 28
Slide 28 text
Creating your own shadows
Slide 29
Slide 29 text
• A library by square (FTW!)
• It gives a chainable (or “fluent”)
syntax for checking assertions.
• Makes tests easier to write (and
read!).
FEST Android
Slide 30
Slide 30 text
Sample: code to modify a bitmap
Slide 31
Slide 31 text
Assertions with FEST
Slide 32
Slide 32 text
Dependency injection is a software design
pattern that allows the removal of hard-coded
dependencies and makes it possible to change
them, whether at run-time or compile-time
Dependency what?
Slide 33
Slide 33 text
Dagger
Directed
Acyclic
Graph
Slide 34
Slide 34 text
Dagger Graph sample
Slide 35
Slide 35 text
Dagger sample
Slide 36
Slide 36 text
Declare dependencies
Slide 37
Slide 37 text
Satisfy dependencies
Slide 38
Slide 38 text
Build the graph
Slide 39
Slide 39 text
Modules
Slide 40
Slide 40 text
Spoon
• Acceptance tests (black box)
• Automation of test execution across multiple
devices
• Aggregation of the results
Slide 41
Slide 41 text
Spoon
• Aggregation of screenshots while your tests
are running
WRITE TESTS!!!
• Simplify your architecture
• Have a robust testing strategy
• Use continuous integration tools
• Mock/stub functionality
• Create your own test runner
Best practices and some advice…
Slide 44
Slide 44 text
DO NOT LET YOUR CODE TO BE BROKEN…
DO NOT BREAK EXISTING FUNCTIONLITY…