Spock on Android
Andrew Reitz
Overview
Why Spock?
How to run it on Android
Extra Features
Moving Forward
Why Spock?
class MyFirstSpec extends Specification {
def "let's try this!"() {
expect:
Math.max(1, 2) == 3
}
}
Why Spock?
MyFirstSpec
- let's try this! FAILED
Condition not satisfied:
Math.max(1, 2) == 3
| |
2 false
at MyFirstSpec.let's try this!(Script1.groovy:7)
Why Spock?
class MathSpec extends Specification {
@Unroll
def "maximum of #a and #b should be #c" {
expect:
Math.max(a, b) == c
where:
a | b | c
1 | 3 | 3
7 | 4 | 4
0 | 0 | 0
}
}
Android
Testing Options
- Native Android - JUnit 3
- JVM - Robolectric
Android
Robolectric
- Fast
- Everything is mocked
- JUnit 4
- Slow to iterate
- Example
Android
My choice.
Android Tests (JUnit 3)
Scrafice speed for reliability and ease of setup.
Android
Making Android Unit Tests less painful
Use Groovy!
Get Spock to Android!
Steps
Include the Jar
Figure out why it's not running
Fix it
Release
Get Spock to Android!
Including the Spock Jar
- Failed to run JUnit 4
- Android Test Kit (Allows JUnit 4 to Run)
Get Spock to Android!
Still Failing
Android Test Kit would run OptimizeRunOrderSuite
Fix: Created custom test runner ignoring everything in the spock package.
Get Spock to Android!
IT WORKS!
class AndroidTestSpec extends Specification {
def "this should run on Android!"() {
given:
def a = 2
def b = 3
when:
def result = a + b
then:
result == 5
}
}
Issues
- Mocks
- Wild card chraracter _
-
thrown() does not work
- Compilation/install failures
Mocking
- Android uses different bytecode.
- Spock uses CGLib (bytecode generator)
- Automatic Getters/Setter on mocks don't work (PR)(java.bean)
Extensions
- @UseActivity(MyActivity) def myActivity
- @UseApplication(MyApplication) def myApplication
- @WithContext def context
Where Next?
- Add more Extensions
- Figure out and fix known dsl issues
- Move into SpockFramework?