Spock Testing
Framework
Amir Langer
langera@gmail.com
1
What did a test ever do for
us?
2
A Test is code that uses your
production code
3
A Test is code that uses your
production code
• Correctness Find problems, Facilitates change
4
A Test is code that uses your
production code
• Correctness Find problems, Facilitates change
• Design
5
A Test is code that uses your
production code
• Correctness Find problems, Facilitates change
• Design
• Documentation
6
It does not matter how intelligent you
are, if you guess and that guess
cannot be backed up by experimental
evidence, then it is still a guess.
Richard Feynman (1964)
Correctness
7
A Software system can be best designed if
the testing is interlaced with the design
instead of being used after the design
Alan J. Perlis (1968)
Design
8
Documentation
9
If JUnit was good enough for my
grandparents then why not for me?
• Written in Java: Statically typed, Rigid syntax, Hard
for DSLs
• Limited in its features, Requires “add-ons”:
Hamcrest, JAssert, JMock, Mockito, EasyMock,
PowerMock, JMockit, Cucumber, JBehave…
10
What is Spock?
• A Developer testing framework for the JVM (since 2008)
• Based on Groovy
• Succinct syntax and DSL built for tests
• Mocking/stubbing is already included
• BDD friendly
• Fully compatible with JUnit
11
Who uses Spock?
12
JUnit V Spock
Simple Assertion
Does (2+2)*3+5 = 16?
14
Assertion Failed
15
Assertion Failed
16
State Driven Tests
17
State Driven Tests
18
State Driven Tests
19
State Driven Tests
20
State Driven Tests
21
State Driven Tests
22
State Driven Tests
23
State Driven Tests
24
State Driven Tests
25
State Driven Tests
26
B
D
D
State Driven Tests
27
State Driven Tests
28
State Driven Tests
29 D
SL
B
uiltforTests
Data Driven Tests
30
Data Driven Tests
31
Data Driven Tests
32
Succinctsyntax
Data Driven Test Results
33
Interaction Based Tests
34
Mock
35
Stub
36
Stub
37
Mock & Stub
38
Mock & Stub
39
Order
40
Order
41
and more…
42
you can even amaze your
colleagues with this:
(_.._) * _._(*_) >> _
43
Control
44
Extensions (Built-in)
• @Ignore
• @IgnoreIf
• @IgnoreRest
• @Requires
• @Timeout
• @Unroll
• @Stepwise
• @Rule
• @FailsWith
• @AutoCleanup
• @ConfineMetaClass
• @Use
• @Issue
• @Title
• @Subject
• @Narrative
45
Extensions (External)
• spock-spring
• spock-guice
• spock-grails
• spock-tapestry
• …
46
Control
47
Control
48
Extensions (Spring)
49
Extensions (DIY)
50
Extensions (DIY)
51
Extensions (DIY)
52
Extensions (DIY)
Demo
53
extension idea borrowed from kenspie spock presentation
@ JavaOne 2014
Under the hood
• It’s Groovy!
• It uses Groovy AST Transformations
• It’s a JUnitRunner -> IDE Support, Build Tools
Support… anything that supports JUnit also
supports Spock
54
Wow!
• Cost of context switch - Legacy tests
• Cost of learning new DSL - “Legacy” developers
So, is there any reason why NOT to start using
Spock?
55
Use Spock
• Easier to write
• Easier to read
• Easier understanding of failures
• Promotes good structured tests
• Allows non technical people to be involved
• Easy to extend
56
http://spockframework.org/
http://docs.spockframework.org/
https://github.com/spockframework/spock
My code examples:
https://github.com/langera/spock_presentation
Alert Extension:
https://github.com/langera/AlertSpockExtension
Thank you
57

Spock Testing Framework